当前位置:网站首页>Shell uses. Net objects to send mail

Shell uses. Net objects to send mail

2020-11-08 11:26:24 I'm sorry.

There are many ways to send mail , I am used to windows powershell Self contained Send-MailMessage You can send email , This use .Net To send mail , And you need to insert local images into HTML The file of , It's important to note that the captured images name Need and HTML Medium cid:name Agreement , The reference codes are as follows :

$EmailAddress = 'zhangsan@contoso.com'
$subject = 'Test Use Net Send Mail'
$SmtpServer = "mail.contoso.com"
$htmlbody = @'
<body>
    <div>
        <img src="cid:telphone.jpg" style="display:inline-block">
    </div>
    <span>This is test mail, use .NET send mail</span>
    <div>
        <img src="cid:home.png" style="display:inline-block">
    </div>
</body>
'@
$MailMessage = New-Object System.Net.Mail.Mailmessage
$imagepath = 'D:\script\images'
$files = Get-ChildItem $imagepath
foreach ($file in $files)
{
    $Attachment = New-Object Net.Mail.Attachment("$imagepath\$file")
    $Attachment.ContentDisposition.Inline = $True
    $Attachment.ContentDisposition.DispositionType = "Inline"
    $Attachment.ContentType.MediaType = "image/png"
    $Attachment.ContentId = $file.ToString() # file name must be equal inert into html image cid: name
    $MailMessage.Attachments.Add($Attachment)
}
$MailMessage.To.Add($EmailAddress)
$MailMessage.from = 'zhangsan@contoso.com'
$MailMessage.Subject = $subject
$MailMessage.Body = $htmlbody
$MailMessage.IsBodyHTML = $true
$MailMessage.BodyEncoding = [System.Text.Encoding]::UTF8
$MailMessage.Priority = "High"

$SmtpClient = New-Object Net.Mail.SmtpClient($SmtpServer)
$SmtpClient.UseDefaultCredentials = $false
#$SmtpClient.Credentials = New-Object System.Net.NetworkCredential("wang5@contoso.com", "123456")
$SmtpClient.Send($MailMessage)
$Attachment.dispose()

版权声明
本文为[I'm sorry.]所创,转载请带上原文链接,感谢