2010-04-16 185 views

回答

5

這個功能對我來說效果很好。 。 。

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() 
} 
相關問題