Solved

How to email multiple attachments in Powershell

Posted on 2013-06-18
1
7,654 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 69

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
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…

733 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