Solved

Powershell sending an email with attachment

Posted on 2011-03-07
2
950 Views
Last Modified: 2012-10-24
Trying to script powershell to send an email with an attachment. I have tired all sorts of items with no luck.  Any ideas on how to add an attachment? I have it sending an email with the script below:

$mail = New-Object System.Net.Mail.MailMessage

$mail.From = "emailaddress“;

$mail.To.Add("emailaddress");

$mail.Subject = "test";

$mail.Body = "test";

$smtp = New-Object System.Net.Mail.SmtpClient("192.168.xxx.xxx");

#*$smtp.Credentials = New-Object System.Net.NetworkCredential("userid", "password");

$smtp.Send($mail);


Thanks in Advance
0
Comment
Question by:awgit
2 Comments
 
LVL 27

Accepted Solution

by:
KenMcF earned 500 total points
Comment Utility
Add these three lines to your code. You could also try using the send-mailmessage cmdlet in V2. it is a lot easier to use. Also look at this question I was helping with for other examples.

$emailattachment = "C:\mailatt.txt"
$attachment = New-Object System.Net.Mail.Attachment($emailattachment, 'text/plain')  
$mail.Attachments.Add($attachment)  


http://www.experts-exchange.com/Software/Server_Software/File_Servers/Active_Directory/Q_26850860.html
0
 

Author Closing Comment

by:awgit
Comment Utility
yea that worked!!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Synchronize a new Active Directory domain with an existing Office 365 tenant
Following basic email etiquette rules will help you write a professional email and achieve a good, lasting impression with your contacts.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This video discusses moving either the default database or any database to a new volume.

763 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

14 Experts available now in Live!

Get 1:1 Help Now