为什么附件不发送此代码?

时间:2019-07-11 15:00:34

标签: powershell

为什么附件不发送此代码?我如何重新制作此代码以将电子邮件发送给许多收件人?

$EmailTo = "vitaly9oleg@gmail.com"
$EmailFrom = "adm@forceauto.ru"
$Subject = "first letter" 
$Body = "Text of the letter" 
$SMTPServer = "mail.forceauto.ru" 
$filenameAndPath = "C:\1.txt"
$SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
$attachment = New-Object System.Net.Mail.Attachment($filenameAndPath)
$SMTPMessage.Attachments.Add($attachment)
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25) 
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("adm@forceauto.ru", "xxxx"); 
$SMTPClient.Send($EmailFrom,$EmailTo,$Subject,$Body,$attachment)

1 个答案:

答案 0 :(得分:5)

我不确定如何处理您的问题,但是也许我仍然可以为您提供帮助:

从3.0开始,在PowerShell中,您有一个名为Send-MailMessage的cmdlet。

Send-MailMessage -From 'jesfer <jesfer@gmail.com>' -To 'tesrere<tesrere@gmail.com>' -Subject "first letter" -Body "Text of the letter" -SmtpServer "smtp.gmail.com" -UseSsl

关于凭据,它也是一个参数。但是使用它,我会建议我们给大家。

编辑:带有凭据

$credentials = new-object Management.Automation.PSCredential “jesfer@gmail.com”, (“s57u5t4” | ConvertTo-SecureString -AsPlainText -Force)
Send-MailMessage -From 'jesfer <jesfer@gmail.com>' -To 'tesrere<tesrere@gmail.com>' -Subject "first letter" -Body "Text of the letter" -SmtpServer "smtp.gmail.com" -UseSsl -Credential $credentials

编辑2:使用工作端口

$credentials = new-object Management.Automation.PSCredential “adm@forceauto.ru”, (“xxxxx” | ConvertTo-SecureString -AsPlainText -Force)
Send-MailMessage -From 'adm <adm@forceauto.ru>' -To 'vitaly9oleg<vitaly9oleg@gmail.com>' -Subject "first letter" -Body "Text of the letter" -SmtpServer "mail.forceauto.ru" -Port 465 -UseSsl -Credential $credentials

编辑3:带有附件

$credentials = new-object Management.Automation.PSCredential “adm@forceauto.ru”, (“xxxxx” | ConvertTo-SecureString -AsPlainText -Force) 
Send-MailMessage -From 'adm <adm@forceauto.ru>' -To 'vitaly9oleg<vitaly9oleg@gmail.com>' -Subject "first letter" -Body "Text of the letter" -Attachments "C:\1.txt" -SmtpServer "mail.forceauto.ru" -Port 465 -UseSsl -Credential $credentials