开发者

how to send an email with attachement using powershell v1?

how to send an email wi开发者_StackOverflow中文版th attachement using powershell v1?


This function has worked well for me . . .

function send-emailwithattachment( [string] $subject, [string] $body, [object] $to, [Object] $attachment  )
{
    $from = "sender.address@domain.com"
    $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()
}


Try using this simple code which will help you in sending email from a defined path : $FilesPath

    $smtpServer = "<smtprelay>"
    $msg = new-object Net.Mail.MailMessage

    #Change port number for SSL to 587
    $smtp = New-Object Net.Mail.SmtpClient($SmtpServer, 25) 

    #Uncomment Next line for SSL  
    #$smtp.EnableSsl = $true

    #From Address
    $msg.From = ""
    #To Address, Copy the below line for multiple recipients
    $msg.To.Add("")
    $msg.Cc.Add("")


    #Message Subject
    $msg.Subject = "Test Subject"

    $FilesPath = "C:\testfile.txt"

    $attachment = New-Object System.Net.Mail.Attachment –ArgumentList $FilesPath
    $msg.Attachments.Add($attachment)


    $smtp.Send($msg)
    $msg.Dispose();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜