PowerShell - Send-MailMessage with AWS SES - Intermittent - powershell

I've been using the below script to send email and sometimes it works, sometimes it does not. I'm not sure exactly why it is intermittent and with this error:
"ERROR: The SMTP server requires a secure connection or the client was not authenticated. The server response was: Authentication required"
This is the script I'm using:
$AWS_ACCESS_KEY = "KEYYYYYYYYYY"
$AWS_SECRET_KEY = "SECRET_KEYYYYYYYYYYYYYY"
$SECURE_KEY = $(ConvertTo-SecureString -AsPlainText -String $AWS_SECRET_KEY -Force)
$creds = $(New-Object System.Management.Automation.PSCredential ($AWS_ACCESS_KEY, $SECURE_KEY))
$smtp = "email-smtp.us-east-1.amazonaws.com"
$from = "noreply#domain.com"
$to = "to_you#domain.com"
$bcc = "its_me#domain.com"
$subject = "Hello There"
$body = "Hey Friend!<br><br>"
$body += "Body<br><br>"
$body += "Regards,<br>"
$body += "Bye<br>"
#Sending email
Sleep 2
Send-MailMessage -From $from -To $to -Bcc $bcc -Subject $subject -Body $body -BodyAsHtml -SmtpServer $smtp -Credential $creds -UseSsl -Port 587
What could be the issue? Has anyone seen this before? Any log in AWS console that I could look at?
Thanks!

I also faced a similar issue managed to resolve with the below codes.
$smtpServer = "email-smtp.us-east-1.amazonaws.com"
$smtpPort = 587
$username = "************"
$password = "***********************"
$from = "alerts#test.com"
$to = "recipient#domain.com"
$subject = "Amazon SES Test Email"
$body = "Body Amazon SES Test Email"
$cc = "xyz#domain.com"
$smtp = new-object Net.Mail.SmtpClient($smtpServer, $smtpPort)
$smtp.EnableSsl = $true
$smtp.Credentials = new-object Net.NetworkCredential($username, $password)
$msg = new-object Net.Mail.MailMessage
$msg.From = $from
$msg.To.Add($to)
$msg.CC.Add($cc)
$msg.Subject = $subject
$smtp.Send($msg)
$msg.Body = $body

Related

Need help - Multiple attachment in Powershell Script

How to change this script to attach 5 attachment and to send email :
Pls help in changing the script accordingly to accept more attachment and to send email
###########Define Variables########
$fromaddress = "donotreply#labtest.com"
$toaddress = "Aishwarya.Rawat#labtest.com"
$bccaddress = "Vikas.sukhija#labtest.com"
$CCaddress = "Mahesh.Sharma#labtest.com"
$Subject = "ACtion Required"
$body = get-content .\content.htm
$attachment = "C:\sendemail\test.txt"
$smtpserver = "smtp.labtest.com"
####################################
$message = new-object System.Net.Mail.MailMessage
$message.From = $fromaddress
$message.To.Add($toaddress)
$message.CC.Add($CCaddress)
$message.Bcc.Add($bccaddress)
$message.IsBodyHtml = $True
$message.Subject = $Subject
$attach = new-object Net.Mail.Attachment($attachment)
$message.Attachments.Add($attach)
$message.body = $body
$smtp = new-object Net.Mail.SmtpClient($smtpserver)
$smtp.Send($message)
According to your main question about modifying the code to attach multiple file
assuming that all your attachments exist under C:\sendemail\
we will modify the variable attachment to include all the files under C:\sendemail\ as follow
$attachment = (Get-ChildItem C:\sendemail\ -File).FullName
# or if you would like $attachment = #("C:\sendemail\file1.txt","C:\sendemail\file2.txt","C:\sendemail\file3.txt")
and then we will loop through the files of attachement to create Net.Mail.Attachment object and add it to the message like that:
foreach ($att in $attachment){
$attach = new-object Net.Mail.Attachment($att)
$message.Attachments.Add($attach)
}
so your final code will be something like that:
###########Define Variables########
$fromaddress = "donotreply#labtest.com"
$toaddress = "Aishwarya.Rawat#labtest.com"
$bccaddress = "Vikas.sukhija#labtest.com"
$CCaddress = "Mahesh.Sharma#labtest.com"
$Subject = "ACtion Required"
$body = get-content .\content.htm
$attachment = (Get-ChildItem C:\sendemail\ -File).FullName
$smtpserver = "smtp.labtest.com"
####################################
$message = new-object System.Net.Mail.MailMessage
$message.From = $fromaddress
$message.To.Add($toaddress)
$message.CC.Add($CCaddress)
$message.Bcc.Add($bccaddress)
$message.IsBodyHtml = $True
$message.Subject = $Subject
foreach ($att in $attachment){
$attach = new-object Net.Mail.Attachment($att)
$message.Attachments.Add($attach)
}
$message.body = $body
$smtp = new-object Net.Mail.SmtpClient($smtpserver)
$smtp.Send($message)
If you are not limited to Powershell V1, you could use the Send-MailMessage command
Send-MailMessage -From $fromaddress -to $toaddress -Cc $CCaddress -Bcc $bccaddress -SmtpServer $smtpserver -Body $body -Subject $Subject

Sending E-Mail with attachment in PowerShell

I want to send a CSV file in this e-mail.
I can send the email with no problem, but I tried a lot to send attachment with it, but at the end the e-mail was sent without the attachment.
function sendmail($Body) {
$Smtp = New-Object System.Net.Mail.SmtpClient
$MailMessage = New-Object System.Net.Mail.MailMessage
$Smtp.Host = "smtp-server"
$MailMessage.From = "sender#example.com"
$MailMessage.To.Add("recipient#example.org")
$MailMessage.Subject = "Hello"
$MailMessage.Body = $Body
$smtp.Port = 25
$smtp.EnableSsl = $false
$Smtp.Send($MailMessage)
}
$Body = "12334"
sendmail $Body
The e-mail just have to look like this:
Hello, all the information are in the file Example.csv
Send-MailMessage has a direct way of handling the attachments. Below is the sample.
Send-MailMessage -From "User01 <user01#example.com>" `
-To "User02 <user02#example.com>", `
"User03 <user03#example.com>" `
-Subject "Sending the Attachment" `
-Body "Forgot to send the attachment. Sending now." `
-Attachment "data.csv" -SmtpServer smtp.fabrikam.com
OR, you should use System.Net.Mail.MailMessage, then you have to use:
$EmailFrom = "<user#domain.tld>"
$EmailTo = "<user#domain.tld>"
$EmailSubject = "<email subject"
$SMTPServer = "smtphost.domain.tld"
$SMTPAuthUsername = "username"
$SMTPAuthPassword = "password"
$emailattachment = "<full path to attachment file.csv>"
function send_email {
$mailmessage = New-Object System.Net.Mail.MailMessage
$mailmessage.From = ($emailfrom)
$mailmessage.To.Add($emailto)
$mailmessage.Subject = $emailsubject
$mailmessage.Body = $emailbody
$attachment = New-Object System.Net.Mail.Attachment($emailattachment, 'text/plain')
$mailmessage.Attachments.Add($attachment)
#$mailmessage.IsBodyHTML = $true
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("$SMTPAuthUsername", "$SMTPAuthPassword")
$SMTPClient.Send($mailmessage)
}
Refer THIS if required.

Sending Email Issue

While trying to attach the .CSV file and run the code, i get the error as "Exception calling "Send" with "1" argument(s): "Unable to send to all recipients."
I have tried with .txt file ($attachment = "\path1\test.txt") and the same code is executed without any error (but I have not received any mail)
$date_value = Get-Date -format yyyy_MM_dd
$file_Name = 'test_'+ $date_value.ToString() + '.csv'
$attachment = "\\path1\test.CSV"
$smtpserver = "server.com"
$size=((Get-item $attachment ).length) -as [Int]
$from = "mail.com"
[string[]]$To= "xyz#xyz.com"
$CC="xyz#xyz.com"
$Subject = "Test"
$body=#"
Hi,
Test
Regards,
Team
"#
$message = new-object System.Net.Mail.MailMessage ( $From , $To )
$message.CC.Add($cc)
$message.Subject = $Subject
$attach = new-object Net.Mail.Attachment($attachment)
$message.Attachments.Add($attach)
$message.body = $body
$smtp = new-object Net.Mail.SmtpClient($smtpserver)
if ($size -gt 0){
$smtp.Send($message)
}
$attach.Dispose()
$message.Dispose()
$smtp.Dispose()
Expectation: .CSV file should get attached and send in the email
Have you tried the Send-MailMessage cmdlet with the -Attachment option? Its really simple.
Send-MailMessage -To $To -From $from -Cc $CC -Subject $Subject -Attachments $attachment -Body $body -SmtpServer $smtpserver

Email sending using PowerShell

I am trying to send the mail from PowerShell.
$EmailFrom = "xxxxxx#gmail.com"
$EmailTo = "xxxxx#gmail.com"
$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("xxxx#gmail.com", "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?
Is Send-MailMessage not an option for you?
You could do the following:
$EmailFrom = "xxxxxx#gmail.com"
$EmailTo = "xxxxx#gmail.com"
$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
Would this example work?
##############################################################################
$From = "YourEmail#gmail.com"
$To = "AnotherEmail#YourDomain.com"
$Cc = "YourBoss#YourDomain.com"
$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/
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.

How to send an email from yahoo SMTP server with PowerShell?

How to send an email from yahoo SMTP server with PowerShell v3? Authentication is required.
Send-MailMessage has a -Credential parameter that takes a pscredential object. I would use a hashtable to store and splat the connection arguments:
$MailArgs = #{
From = 'mindaugas#yahoo.com'
To = 'someone#domain.com'
Subject = 'A subject line'
Body = 'Mail message content goes here!'
SmtpServer = 'smtp.mail.yahoo.com'
Port = 587
UseSsl = $true
Credential = New-Object pscredential 'mindaugas#yahoo.com',$('P#ssW0rd!' |ConvertTo-SecureString -AsPlainText -Force)
}
Send-MailMessage #MailArgs
in case somebody looking for google smtp using MailMessage
[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("XXXX#gmail.com");
$mail.To.Add("XXX#XXXX.com");
$mail.Subject = "Place Subject of email here";
$mail.Body = "Place body content here";
$smtp = New-Object System.Net.Mail.SmtpClient("smtp.gmail.com");
$smtp.Port = "587";
$smtp.Credentials = New-Object System.Net.NetworkCredential("XXXXX#gmail.com", "password");
$smtp.EnableSsl = "true";
$smtp.Send($mail);
This finally worked for me for SBCGlobal.net since the secure mail key at Yahoo:
$from = "name#sbcglobal.net"
$secpass = ConvertTo-SecureString "<SecureMailKeyPassword>" -AsPlainText -Force
$mycred = New-Object System.Management.Automation.PSCredential ($from, $secpass)
Send-MailMessage -SmtpServer 'smtp.mail.yahoo.com' -UseSsl -Port 587 -Credential $mycred -To <name#sbcglobal.net> -From <name#sbcglobal.net> -Subject '<Subject>' -Body '<Message>'