36

I'd like to send email from PowerShell, so I use this command:

$EmailFrom = "[email protected]" $EmailTo = "[email protected]" $Subject = "today date" $Body = "TODAY SYSTEM DATE=01/04/2016 SYSTEM TIME=11:32:05.50" $SMTPServer = "smtp.mail.yahoo.com" $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587) $SMTPClient.EnableSsl = $true $SMTPClient.Credentials = New-Object System.Net.NetworkCredential("[email protected]", "password") $SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body) 

This command didn't work for Yahoo mail or Outlook mail, but works for my Gmail. Is there anything wrong that I have done?

3
  • 7
    Any reason not to use Send-MailMessage? Commented Apr 1, 2016 at 12:03
  • I am just learning PS, i didnt know that command is exist Commented Apr 1, 2016 at 14:53
  • 5
    Just want to note that Send-MailMessage is now deprecated. Commented Jan 11, 2022 at 13:40

4 Answers 4

63

The following code snippet for sending SMTP e-mail through Gmail works for me:

$Username = "MyUserName"; $Password = "MyPassword"; $path = "C:\attachment.txt"; function Send-ToEmail([string]$email, [string]$attachmentpath){ $message = new-object Net.Mail.MailMessage; $message.From = "[email protected]"; $message.To.Add($email); $message.Subject = "subject text here..."; $message.Body = "body text here..."; $attachment = New-Object Net.Mail.Attachment($attachmentpath); $message.Attachments.Add($attachment); $smtp = new-object Net.Mail.SmtpClient("smtp.gmail.com", "587"); $smtp.EnableSSL = $true; $smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password); $smtp.send($message); write-host "Mail Sent" ; $attachment.Dispose(); } Send-ToEmail -email "[email protected]" -attachmentpath $path; 
Sign up to request clarification or add additional context in comments.

3 Comments

By the way, you need to allow access for less secure apps in your gmail account settings. It works with gmail at least.
Hi, Im trying to use Sean's solution in a email.ps1 file and running it from the command line with this PowerShell.exe -NoProfile -ExecutionPolicy Bypass C:\email.ps1 but I keep getting an error about authentication. I'm using the correct password, for the username I tried my gmail user name with @gmail.com and without. Hope you can help
How do I add more than 1 attachment?
13

I use this:

Send-MailMessage -To [email protected] -from [email protected] -Subject 'hi' -SmtpServer 10.1.1.1 

3 Comments

How could you set a specific time for it , example using the code above - send an email at 4:00 pm for example
FYI this command is obsolete because it cannot guarantee secure connections. See this page for more information: learn.microsoft.com/en-us/powershell/module/…
Thank you. Although Send-MailMessage is obsolete, i still run it in Windows 10. This is the command I used to send from an outlook email to another. Send-MailMessage -To [email protected] -from [email protected] -Subject 'hi' -SmtpServer smtp.office365.com -UseSsl -Credential MyWindowLoginAccount
1

You can simply use the Gmail smtp.

Following is The powershell code to send a gmail message with an Attachment:

 $Message = new-object Net.Mail.MailMessage $smtp = new-object Net.Mail.SmtpClient("smtp.gmail.com", 587) $smtp.Credentials = New-Object System.Net.NetworkCredential("[email protected]", "password"); $smtp.EnableSsl = $true $smtp.Timeout = 400000 $Message.From = "[email protected]" $Message.To.Add("[email protected]") $Message.Attachments.Add("C:\foo\attach.txt") $smtp.Send($Message) 

On the sender Google Account ([email protected]),

Make sure you have Turned ON Access for less-secure apps option, from google Account Security Dashboard.

Finally, Save this Script As mail.ps1

To invoke the above Script Simple run below on Command Prompt or batch file:

 Powershell.exe -executionpolicy remotesigned -File mail.ps1 

By Default, For sending Large Attachments Timeout is Around 100 seconds or so. In this script, it is increased to Around 5 or 6 minutes

1 Comment

how is this more secure than Send-MailMessage?
1

Sometimes you may need to set the EnableSsl to false (in this case the message will be sent unencrypted over the network)

3 Comments

the parameter does note exist
Well it worked for me. That's why I wrote this. If none of these solutions work for you find another article.
Context is everything, for this is a valid answer, as shown here: SMTPClient Failure During Send Concerning Arguments.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.