以下是错误信息:
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
你没有指定端口(默认是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 计算机
应用:邮件
生产后使用此密码即可。
亲测有效!
你这Send方法怎么调用的都没有贴出来。
关于发送邮件有一篇文章可以参考:PowerShell使用Hotmail账号发送邮件
$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)