?
Solved

How to email multiple attachments in Powershell

Posted on 2013-06-18
1
Medium Priority
?
8,636 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 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

777 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