Tuesday, June 4, 2013

powershell send email with attachment

function sendEmailWithReport()
{
    ###########Define Variables########
       
    $fromaddress = "fromaddress@gmail.com"
    $toaddress = "toaddress@gmail.com"
    $Subject = "sample subject"
    $body = "sample body"
    $attachment = "attachment path"
  
    $smtpserver = "smtp server name"
   
    ####################################
   
    $message = new-object System.Net.Mail.MailMessage
    $message.From = $fromaddress
    $message.To.Add($toaddress)
    $message.IsBodyHtml = $True
    $message.Subject = $Subject
    $attach = new-object Net.Mail.Attachment($attachment)
    $message.Attachments.Add($attach)
    $message.body = $body
    $smtp = new-object Net.Mail.SmtpClient($smtpserver)
    $smtp.Send($message)
    $attach.Dispose()

}




No comments:

Post a Comment