tags:

views:

64

answers:

1

how to send an email with attachement using powershell v1?

+5  A: 

This function has worked well for me . . .

function send-emailwithattachment( [string] $subject, [string] $body, [object] $to, [Object] $attachment  )
{
    $from = "[email protected]"
    $domain  = "smtp-server.domain.com"

    $mail = new-object System.Net.Mail.MailMessage

    for($i=0; $i -lt $to.Length; $i++) {
        $mail.To.Add($to[$i]);
    }

    $mail.From = new-object System.Net.Mail.MailAddress($from)
    $mail.Subject = $subject
    $mail.Body = $body

    $attach = New-Object System.Net.Mail.Attachment($attachment)
    $mail.Attachments.Add($attach)

    $smtp = new-object System.Net.Mail.SmtpClient($domain)
    $smtp.Send($mail)

    $attach.Dispose()
    $mail.Dispose()
}
Brian