Need help - Multiple attachment in Powershell Script - powershell

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

Related

PowerShell - Send-MailMessage with AWS SES - Intermittent

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

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

Powershell script to email with attachment from WebRequest Response

I have this command which spits out a pdf file and you can see that it is saving in my C: drive:
$DownloadReportResponse = Invoke-WebRequest -Method Post -Uri $DownloadReportUrl -ContentType "application/json" -Headers $headers -Body $DownloadReportRequestBody -OutFile "C:\Neoload\TestResult-($DateForTestResult).pdf" -TransferEncoding "deflate"
Is there way to script this so it can send the file to certain email address as well?
I have this example script to send email, where would I assign the file so it is attached in the email?
###########Define Variables########
$fromaddress = "myGmail#gmail.com"
$toaddress = "myGmail#gmail.com"
#$CCaddress = "myGmail#gmail.com"
$Subject = "ACtion Required"
$body = get-content .\content.htm
$attachment = $DownloadReportResponse
$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)
You can use the build in Send-MailMessage command. Just make sure that the attachment is a path to your file
$EmailSettings=#{
SMTPServer="smtp.labtest.com"
From="myGmail#gmail.com"
To="myGmail#gmail.com"
Subject="ACtion Required"
Body=(get-content .\content.htm )
Attachments="C:\Neoload\TestResult-($DateForTestResult).pdf"
BodyAsHtml=$true
}
Send-MailMessage #EmailSettings

Powershell Send Email With Images

I am trying to embed an image into an email to be sent out via Powershell.
Below is the code I have:
$Attachment = New-Object Net.Mail.Attachment($LocalLocation)
$Attachment.ContentDisposition.Inline = $True
$Attachment.ContentDisposition.DispositionType = "Inline"
$Attachment.ContentType.MediaType = "image/png"
$MailMessage = New-Object Net.Mail.MailMessage
$MailMessage.To.Add($emailTo)
$MailMessage.From = $MyEmail
$MailMessage.Subject = "Test Email"
$MailMessage.IsBodyHtml = $True
$MailMessage.Attachments.Add($Attachment)
$MailMessage.Body = "
<html>
<head></head>
<body>
<img src='CID:$($Attachment.ContentId)' />
</body>
</html>"
$SmtpClient = New-Object Net.Mail.SmtpClient("123.0.0.1",25 )
$SmtpClient.Send($MailMessage)
I get sent an email but there is just an empty box in the message. The $LocalLocation is the link to my image.
I am using Powershell 3
$SendTo = "Sender Mail ID"
$SMTPServer = "SMTP Server"
$EmailFrom = “Reciever Mail ID”
$EmailSubject = “Email including images in HTML”
$Image = "Image File"
$Message = new-object Net.Mail.MailMessage
Add-PSSnapin Microsoft.Exchange.Management.Powershell.Admin -erroraction silentlyContinue
$att = new-object Net.Mail.Attachment($Image)
$att.ContentId = "att"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$body = '<img src="cid:att" />'
$Message.From = $EmailFrom
$Message.To.Add($SendTo)
$Message.Subject = $EmailSubject
$Message.Body = $body
$Message.IsBodyHTML = $true
$Message.Attachments.Add($att)
$smtp.Send($Message)
$att.Dispose()
Hope this HEpls.