I'm sending a report out via email in Powershell and I would like to add a CC, but everything I've tried thusfar isnt working.
heres my sendmail code...
$ExportedReport = "$WorkFolder\InactiveUserReport.csv"
$EmailTo = "myemail#domain.com"
$EmailFrom = "reportsemail#domain.com"
$Subject = "REPORT BLAH BLAH"
$Body = "blah blah blah."
$SMTPServer = "mysmtpserver"
$SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
$attachment = New-Object System.Net.Mail.Attachment($ExportedReport)
$SMTPMessage.Attachments.Add($attachment)
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)
$SMTPClient.EnableSsl = $false
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("myusername", "mypassword");
$SMTPClient.Send($SMTPMessage)
Any help on how I can modify this to add a CC would be wonderful!
Your MailMessage has a CC property.
Use the following:
$SMTPMessage.cc.Add("cc.address#domain.tld")
Related
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
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.
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.
I am trying to send a message with PowerShell but I can not send the message. The error says:
The server SMTP requires a connection safe or the user didn't authenticate.
I don't know where is my error and I don't know what more I can try.
This is my code:
$EmailTo = "prueba#gmail.com"
$EmailFrom = "pacopepe#gmail.com"
$Subject = "Test"
$Body = "Test Body"
$SMTPServer = "smtp.gmail.com"
$SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("pacopepe#gmail.com", "pacopaco");
$SMTPClient.Send($SMTPMessage)
Note: In credentials, I put $emailfrom and the password of his mail.
This works, but...
$EmailFrom = "vsphere#nasa.gov"
$EmailTo = "userwithOLDsnapshot#nasa.gov"
$Subject = "Notification"
$Body = "this is a notification for your snapshot"
$SMTPServer = "smtp.gmail.com"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("username", "password");
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
Is there anyway of doing this with a credential store? Embedding creds in scripts in clear text is a no no.
The following will create an encrypted string. You can save this output to some file and read it in later:
$securePWord=Read-Host "Enter some password" -AsSecureString
This will convert a SecureString into usable text:
$pWord=[System.Runtime.InteropServices.marshal]::PtrToStringAuto([System.Runtime.InteropServices.marshal]::SecureStringToBSTR($securePWord))