35

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

$EmailFrom = "customer@yahoo.com"
$EmailTo = "receiver@ymail.com"  
$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("customer@yahoo.com", "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?

Andrew Myers
  • 2,754
  • 5
  • 32
  • 40
Jessica
  • 461
  • 1
  • 4
  • 7

4 Answers4

58

Following code snippet really 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 = "YourName@gmail.com";
    $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 "reciever@gmail.com" -attachmentpath $path;
Sean Wei
  • 7,433
  • 1
  • 19
  • 39
IgrDi
  • 625
  • 6
  • 4
  • 2
    By the way, you need to allow access for less secure apps in your gmail account settings. It works with gmail at least. – IgrDi Apr 01 '16 at 11:59
  • 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 – Kurt L. Dec 19 '18 at 07:22
  • 2
    Okay it was not the code it was Google, I had to login with my gmail account and find "Allow less secure apps:" from here https://myaccount.google.com/security#activity to turn on "Allow less secure apps:" and received my mail! Thanks! – Kurt L. Dec 19 '18 at 09:47
  • Hi,How do I add CC to this? I tried many combinations of `$message.Cco.Add("emailaddress")` but can't get it working – Kurt L. Dec 20 '18 at 19:14
  • How do I add more than 1 attachment? – Ricardo Bohner Dec 11 '19 at 01:09
  • Can this be adapted for corporate emails? – Princy Jul 17 '23 at 14:10
12

I use this:

Send-MailMessage -To hi@abc.com -from hi2@abc.com -Subject 'hi' -SmtpServer 10.1.1.1
Mark Varnas
  • 721
  • 9
  • 10
  • How could you set a specific time for it , example using the code above - send an email at 4:00 pm for example – user 9191 Jun 12 '19 at 19:56
  • 7
    FYI this command is obsolete because it cannot guarantee secure connections. See this page for more information: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/send-mailmessage?view=powershell-7 – Jacob Degeling Jul 01 '20 at 00:42
1

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

safepost
  • 135
  • 12
JSkyS
  • 413
  • 6
  • 14
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("From@gmail.com", "password"); 
    $smtp.EnableSsl = $true 
    $smtp.Timeout = 400000  
    $Message.From = "From@gmail.com" 
    $Message.To.Add("To@gmail.com") 
    $Message.Attachments.Add("C:\foo\attach.txt") 
    $smtp.Send($Message)

On the sender Google Account (From@gmail.com),

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

A5H1Q
  • 544
  • 7
  • 15