用powershell4.0发送邮件失败,求解答


PowerShell交流中心分类: Powershell基础用powershell4.0发送邮件失败,求解答
0
541509124 asked 9年 ago

以下是错误信息:
Exception calling “Send” with “1” argument(s): “Failure sending mail.”
At C:\Users\liyuz\Desktop\无标题1.ps1:22 char:1
+ $smtp.Send($mail)
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SmtpException

2 Answers
0
Best Answer
Mooser Lee 管理员 answered 9年 ago

你没有指定端口(默认是25),gmail应当使用587
推荐配置:

Host                    : smtp.gmail.com
Port                    : 587
UseDefaultCredentials   : False
EnableSsl               : True

按照这样配置,并且确保你的密码正确,应当能够发送成功。
如果遇到错误:
Exception calling “Send” with “1” argument(s): “The SMTP server requires a secure connection
or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
Learn more at”
那可能是你之前使用过goagent,给谷歌账号启用过两步验证。此时登陆gmail邮箱的密码不是你谷歌账号本身的密码。而是类似app使用的密码。
在谷歌两步验证应用专用密码选择生产:
设备:windows 计算机
应用:邮件
生产后使用此密码即可。
亲测有效!
 
 
 

0
Mooser Lee 管理员 answered 9年 ago

你这Send方法怎么调用的都没有贴出来。
关于发送邮件有一篇文章可以参考:PowerShell使用Hotmail账号发送邮件 

541509124 replied 9年 ago

$smtpServer = “smtp.gmail.com”$smtpUser = “520li5415@gmail.com”$smtpPassword = “xxxxx”#create the mail message$mail = New-Object System.Net.Mail.MailMessage#set the addresses$MailAddress=”520li5415@gmail.com”$MailtoAddress=”541509124@qq.com”$mail.From = New-Object System.Net.Mail.MailAddress($MailAddress)$mail.To.Add($MailtoAddress)#set the content$mail.Subject = “Hello PowerShell”;$mail.Priority = “High”$mail.Body = “Hello Powershell” $filename=”C:\1.txt”$attachment = new-Object System.Net.Mail.Attachment($filename)$mail.Attachments.Add($attachment)#send the message$smtp = New-Object System.Net.Mail.SmtpClient -argumentList $smtpServer$smtp.Credentials = New-Object System.Net.NetworkCredential -argumentList $smtpUser,$smtpPassword$smtp.Send($mail)