?
Solved

Powershell sending an email with attachment

Posted on 2011-03-07
2
Medium Priority
?
957 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
[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
2 Comments
 
LVL 27

Accepted Solution

by:
KenMcF earned 2000 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

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

Question has a verified solution.

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

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
This article will help to fix the below error for MS Exchange server 2010 I. Out Of office not working II. Certificate error "name on the security certificate is invalid or does not match the name of the site" III. Make Internal URLs and External…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
Suggested Courses

762 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