Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Powershell sending an email with attachment

Posted on 2011-03-07
2
Medium Priority
?
960 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The core idea of this article is to make you acquainted with the best way in which you can export Exchange mailbox to PST format.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
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.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Suggested Courses

609 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