Solved

Powershell sending an email with attachment

Posted on 2011-03-07
2
951 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
ID: 35061538
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
ID: 35061974
yea that worked!!
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Scam emails are a huge burden for many businesses. Spotting one is not always easy. Follow our tips to identify if an email you receive is a scam.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The viewer will learn how to count occurrences of each item in an array.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

896 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

11 Experts available now in Live!

Get 1:1 Help Now