0

I am trying to run a PowerShell script from windows server 2012 R2 (PowerShell version 4) that sends out an E-mail. I am able to telnet to smtp.gmail.com port 587. However, The e-mail is not being sent out.

Below is the code snippet and the exception.

$RPT_PWD='userpass'
$Attachment = $args[2]
$EmailTo = $args[1]
$Subject = $args[0]
$EmailFrom = "usertest@gmail.com" 
$SmtpServer = "smtp.gmail.com"
$SmtpPort = "587"
$SMTPClient = New-Object System.Net.Mail.SmtpClient($SmtpServer, $SmtpPort) 
$SMTPClient.EnableSsl = $true 
$SMTPClient.Credentials = New-Object 
System.Net.NetworkCredential($EmailFrom,$RPT_PWD); # credentials to be used
$objMessage = New-Object System.Net.Mail.MailMessage
$Body = "Email Body" 
$objMessage = New-Object System.Net.Mail.MailMessage –ArgumentList 
$EmailFrom, $EmailTo, $Subject, $Body
$objMessage.Attachments.Add($Attachment)
$SMTPClient.Send($objMessage)


Exception calling "Send" with "1" argument(s): "Failure sending mail."
At E:\Scripts\DailySystemProcessCheck\EmailTest\mailx_ssl.ps1:133 char:2
+     $SMTPClient.Send($objMessage)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SmtpException

Also,I also tried using send-mailmessage and it didn't work either.

$Credentials = New-Object System.Management.Automation.PSCredential -
ArgumentList $EmailFrom, $($RPT_PWD | ConvertTo-SecureString -AsPlainText -Force)

send-mailmessage -from $EmailFrom -to $EmailTo -subject $Subject -body $Body 
-Attachment $Attachment -smtpServer smtp.gmail.com -Credential $Credentials 
-UseSsl -Port 587

~~~~

send-mailmessage : The client and server cannot communicate, because they do 
not possess a common algorithm
At E:\Scripts\DailySystemProcessCheck\EmailTest\mailx_ssl.ps1:131 char:2
+     send-mailmessage -from $EmailFrom -to $EmailTo -subject $Subject -body 
$Body -A ...
+    
+ CategoryInfo          : InvalidOperation: 
(System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpExcept
ion
+ FullyQualifiedErrorId : 
SmtpException,Microsoft.PowerShell.Commands.SendMailMessage

I tried to run the script using "Powershell --Version 2.0" and now I don't see SMTP Exception, rather I see another one.

Send : Exception calling "Send" with "1" argument(s): "A from address must 
be specified."
At E:\Scripts\DailySystemProcessCheck\EmailTest\mailx_ssl.ps1:124 char:18
+     $SMTPClient.Send <<<< ($objMessage)
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException

Can anyone please advise? Thanks in advance

pdp
  • 1
  • 1
  • I would look here: https://stackoverflow.com/questions/28457975/common-algorithm-issue-while-trying-to-send-an-email-using-c-sharp – Christopher Jun 09 '17 at 19:16
  • You should use send-emailmessage. Check your TLS settings on client and server. See for example [this](https://stackoverflow.com/questions/34991417/the-client-and-server-cannot-communicate-because-they-do-not-possess-a-common-a) SO question. – Jeroen Heier Jun 09 '17 at 19:31

0 Answers0