0

I am trying to send the mail from PowerShell.

$EmailFrom = "[email protected]" $EmailTo = "[email protected]" $Subject = "Subject" $Body = "Body" $filenameAndPath = "C:\Desktop\EE.txt" $SMTPServer = "smtp.gmail.com" $SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom, $EmailTo, $Subject, $Body) $attachment = New-Object System.Net.Mail.Attachment($filenameAndPath) $SMTPMessage.Attachments.Add($attachment) $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587) $SMTPClient.EnableSsl = $true $SMTPClient.Credentials = New-Object System.Net.NetworkCredential("[email protected]", "password"); $SMTPClient.Send($SMTPMessage) 

When I run this code I get the following exception:

 Exception calling "Send" with "1" argument(s): "Failure sending mail." At line:13 char:1 + $SMTPClient.Send($SMTPMessage) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : SmtpException 

How can I make $SMTPClient.Send() work correctly?

3 Answers 3

1

Is Send-MailMessage not an option for you?

You could do the following:

$EmailFrom = "[email protected]" $EmailTo = "[email protected]" $Subject = "Subject" $Body = "Body" $filenameAndPath = "C:\Desktop\EE.txt" $SMTPServer = "smtp.gmail.com" Send-MailMessage -From $EmailFrom -To $EmailTo -Subject $Subject -body $Body -Attachments $filenameAndPath -SmtpServer $SMTPServer 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for response. i am new to powershell. As you said i proceeded further but no luck i received the below error. Please help me.
Thanks for response. As you said i proceeded further but no luck i received the below error. Please help me. Send-MailMessage : The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. r76sm7795282pfb.69 - gsmtp At line:8 char:1 + Send-MailMessage -From $EmailFrom -To $EmailTo -Subject $Subject -bod ... + CategoryInfo : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpException + FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage
0

Would this example work?

############################################################################## $From = "[email protected]" $To = "[email protected]" $Cc = "[email protected]" $Attachment = "C:\temp\Some random file.txt" $Subject = "Email Subject" $Body = "Insert body text here" $SMTPServer = "smtp.gmail.com" $SMTPPort = "587" Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject ` -Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl ` -Credential (Get-Credential) -Attachments $Attachment ############################################################################## 

Notice that it asks for credentials and also specifies to UseSSL. It's from https://www.pdq.com/blog/powershell-send-mailmessage-gmail/

Comments

0

I have found when using Powershell script to send emails in Gmail you have to first log into the Gmail account and allow the location/IP address of the sending PC to send Emails. From the same PC running the script log into Gmail and there should be a security email. Click allow and then test.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.