Blog

How to send email with attachments using PowerShell

You can send emails from using Power-Shell. You can include attachments as well.

Warning: If your smtp provider allows anonymous authentication you can send email ending domain.

eg: send email from support@apple.com

$SmtpClient = New-Object system.net.mail.smtpClient
$SmtpClient.host = "smtp.client.com"
$MailMessage = New-Object system.net.mail.mailmessage
$MailMessage.from = "support@apple.com"
$MailMessage.To.add("appmail@mycompany.com")
$MailMessage.IsBodyHtml = 0
$MailMessage.Subject = "Test"
$MailMessage.Body = "Testing testing email testing :)"
$file = "c:\testfile.xls"
$att = new-object Net.Mail.Attachment($file)
$MailMessage.Attachments.Add($att)
$SmtpClient.Send($MailMessage)
$att.Dispose()

Leave a Reply

Your email address will not be published. Required fields are marked *