如何使用powershell脚本发送包含二进制文件的电子邮件?以下是我失败的最佳尝试
$to = 'andy.boo@boo.com'
$subject = 'boo'
$file = 'inf.doc'
$from = $to
$filenameAndPath = (Resolve-Path .\$file).ToString()
[void][Reflection.Assembly]::LoadWithPartialName('System.Net') | out-null
$message = New-Object System.Net.Mail.MailMessage($from, $to, $subject, $subject)
$attachment = New-Object System.Net.Mail.Attachment($filenameAndPath, 'text/plain')
$message.Attachments.Add($attachment)
$smtpClient = New-Object System.Net.Mail.SmtpClient
$smtpClient.host = 'smtp.boo.com'
$smtpClient.Send($message)
使用“1”参数调用“发送”的异常:“发送邮件失败”。 在C:\ email.ps1:15 char:17 + $ smtpClient.Send<<<< ($消息) + CategoryInfo:NotSpecified:(:) [],MethodInvocationException + FullyQualifiedErrorId:DotNetMethodException
答案 0 :(得分:4)
什么版本的PowerShell?如果您使用的是2.0版,请自行解决问题,只需使用Send-MailMessage cmdlet即可。
如果版本1.0:
$msg = new-object system.net.mail.MailMessage
$SMTPClient = new-object system.net.mail.smtpClient
$SMTPClient.host = "smtp server"
$msg.From = "Sender"
$msg.To.Add("Recipient")
$msg.Attachments.Add('<fullPathToFile')
$msg.Subject = "subject"
$msg.Body = "MessageBody"
$SMTPClient.Send($Msg)
答案 1 :(得分:0)
发现这个......好多了
Send-MailMessage -smtpServer smtp.doe.com -from 'joe@doe.com' -to 'jane@doe.com' -subject 'Testing' -attachment foo.txt