How to send email using PowerShell

I would like to send an 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 does not work for Yahoo mail or Outlook, but it works for my Gmail mail. Is something wrong with what I did?

+14
powershell smtp
source share
3 answers

The 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; 
+28
source share

I use this:

 Send-MailMessage -To hi@abc.com -from hi2@abc.com -Subject 'hi' -SmtpServer 10.1.1.1 
+8
source share

Sometimes you may need to set EnableSs1 to false.

-one
source share

All Articles