Send-MailMessage Not Working on my local Computer

This is my first post so we will see how it goes!!!

I am having trouble getting PowerShell sending a test email to my Yahoo email account.

I am using the correct entries for my email account, password, and smtp server.

I either get the script containing my code to go through with no errors, or I get a “Failure Sending Mail” error message.

Can anyone provide and iron clad script that will deliver a simple test message to my Yahoo email account?

PS I have already read the Microsoft KB articles on this subject but to no avail. I tend to think this is less of a PowerShell problem and some other issue on my local computer. I have also tried my Gmail account with the same results.

Please help.

God bless you all!!!

How should we know what’s wrong when we don’t know what you tried? Please show your code (format it as code using the code tag button on the editor windows).
Thanks
If you get error messages you should post these as well (formatted as code as well). A lot of times there’s already a hint or even the solution for the error inside the error message.

Good Point Olaf.

Apologies as this is my first post in here. That’s no excuse and I appreciate your patience.

 

#First Method

$MailArgs = @{
    From       = 'nal2us2@yahoo.com'
    To         = 'nal2us2@yahoo.com'
    Subject    = 'Overnight Error Report'
    Body       = 'Email for Overnight Error Notification'
    Attachments= 'C:\error.htm'
    SmtpServer = 'smtp.mail.yahoo.com'
    Port       = 587
    UseSsl     = $true
    Credential = New-Object pscredential 'nal2us2@yahoo.com',$('Aaaa5636SecureC!!' |ConvertTo-SecureString -AsPlainText -Force)
}
Send-MailMessage @MailArgs

#Second Method

$SMTPServer = "smtp.mail.yahoo.com"
$SMTPPort = "587"
$Username = "nal2us2@yahoo.com"
$Password = "Aaaa5636SecureC!!"

$to = "nal2us2@yahoo.com"
$cc = "nal2us2@yahoo.com"
$subject = "Email for Overnight Error Notification"
$body = "Overnight Error Activity for Test Server"
$attachment = "C:\error.htm"

$message = New-Object System.Net.Mail.MailMessage
$message.subject = $subject
$message.body = $body
$message.to.add($to)
$message.cc.add($cc)
$message.from = $username
$message.attachments.add($attachment)

$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
$smtp.EnableSSL = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
$smtp.send($message)
write-host "Mail Sent"

#Third and Final Method

[System.Reflection.Assembly]::LoadWithPartialName("System.Net")
[System.Reflection.Assembly]::LoadWithPartialName("System.Net.Mail")    [System.Reflection.Assembly]::LoadWithPartialName("System.Net.Mail.MailMessage")

$mail = New-Object System.Net.Mail.MailMessage
$mail.From =  New-Object System.Net.Mail.MailAddress("");
$mail.To.Add("");
$mail.Subject = "Place Subject of email here";
$mail.Body = "Place body content here";
$smtp = New-Object System.Net.Mail.SmtpClient("smtp.mail.yahoo.com");
$smtp.Port = "587";
$smtp.Credentials = New-Object System.Net.NetworkCredential("", "");
$smtp.EnableSsl = "true";
$smtp.Send($mail);

Apologies AGAIN Olaf!!!

 

I forgot to include the error messages.

Here they are in a separate email.

Error Messages are as Follows:

Method #1

Send-MailMessage : Unable to read data from the transport connection: net_io_connectionclosed.
At line:13 char:1
+ Send-MailMessage @MailArgs
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMes
sage], SmtpException
+ FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage

Method #2

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

Method #3

At line:2 char:73
+ ... t.Mail") [System.Reflection.Assembly]::LoadWithPartialName("System ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unexpected token '[System.Reflection.Assembly]::LoadWithPartialName' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken

hmmm … nowadays you secure your mail accounts or accounts in general a lot of times with a two factor authentication. Did you do that for your Yahoo account? (at least I did :wink: ) … if you did so you should create a so called “App - Password” to be used with apps not able to use Yahoo’s login with 2FA. I just tested it and it works just as expected …

$Params = @{
From = ‘MailAlias@yahoo.com’
To = ‘MailAlias@yahoo.com’
Subject = ‘Test’
Body = ‘Email for Overnight Error Notification’
SmtpServer = ‘smtp.mail.yahoo.com
UseSsl = $true
Credential = New-Object System.Management.Automation.PSCredential -ArgumentList ‘MailAlias@yahoo.com’, $(‘SuperRandomAppPassword’ | ConvertTo-SecureString -AsPlainText -Force)
}

Send-MailMessage @Params

can you walk me through checking for 2 factor authentication as well as setting up the “App Password” you talked about so that I can get this script working please?

 

Olaf, I confirmed that Yahoo 2 Factor Authentication is active.

 

I just need to know how to apply that Yahoo App password. Should I apply it to the password field one of those scripts that I showed you?

 

OLAF!!! YOU RULE!!!

 

GOT IT WORKING!!!

 

 

A Million Thank yous my friend!!!