Solved

How to email multiple attachments in Powershell

Posted on 2013-06-18
1
6,482 Views
Last Modified: 2013-06-18
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
Comment
Question by:namerg
1 Comment
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
ID: 39257120
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
Synchronize a new Active Directory domain with an existing Office 365 tenant
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now