如何使用Gmail作为SMTP服务器发送邮件

时间:2019-05-15 11:11:01

标签: powershell

我正在使用以下代码通过PowerShell发送邮件:

$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
$Username = "user@gmail.com"
$Password = "pass"
$to = "mail"   
$subject = "TRans LOG"
$body = "TRANS log info"   
$message = New-Object System.Net.Mail.MailMessage
$message.Subject = $subject
$message.Body = $body
$message.To.Add($to)
#$message.Cc.Add($cc)
$message.From = $username   
$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
$smtp.EnableSSL = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
$smtp.Send($message)
Write-Host "Mail Sent successfully using GMAIL"

但是我得到一个错误:

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.
+ $smtp.Send($message)

我在做什么错了?

0 个答案:

没有答案