Solved

Powershell sending an email with attachment

Posted on 2011-03-07
2
954 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

A brief introduction to what I consider to be the best editor for PowerShell.
In-place Upgrading Dirsync to Azure AD Connect
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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…

827 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