Solved

How to email multiple attachments in Powershell

Posted on 2013-06-18
1
8,145 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 70

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A procedure for exporting installed hotfix details of remote computers using powershell
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

688 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