• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 9674
  • Last Modified:

How to email multiple attachments in Powershell

I have the following code, and I am wondering how to add multiple attachments, should I create another variable like $emailattachment2 = "c:\attachment2" , $att2 = new-object Net.Mail.Attachment($emailattachment) and       $msg.Attachments.Add($att2)  ?
# Mail variables #
$enablemail="yes"
$emailattachment = "C:\scripts\ceridian\06-12-2013_ADMisMatch.csv"
$smtpServer = "ux3" 
$mailfrom = "AD_Ceridian_Mismatch  <AD_Ceridian_Mismatcht>"
#$mailto = "Me"
$replyto = "Me"

if ($enablemail -match "yes") 
{ 
	$msg = new-object Net.Mail.MailMessage
	$att1 = new-object Net.Mail.Attachment($emailattachment)
	$smtp = new-object Net.Mail.SmtpClient($smtpServer) 
	$msg.From = $mailfrom
	$msg.To.Add($mailto) 
	$msg.ReplyTo = $replyto
	$msg.Subject = "AD Ceridian Mismatch"
	$msg.Body = "This is an automated email.AD Mismatch as an attachment. Thanks"
	$msg.Attachments.Add($att1)
	$smtp.Send($msg)
}

Open in new window

Thanks 4 ur help
0
namerg
Asked:
namerg
1 Solution
 
QlemoC++ DeveloperCommented:
Exactly that. But you could also use the Send-MailMessage cmdlet, which is much better to use than above:
Send-MailMessage -From $mailfrom -To $mailto -SmtpServer $smtpserver -Subject "AD Ceridian Mismatch" -Body "This is an automated email.AD Mismatch as an attachment. Thanks" -Attachments $emailattachment,$emailattachment

Open in new window

I have attached the same attachment twice for demonstration purpose.
0

Featured Post

Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now