发送电子邮件时 Powershell 超时

Powershell timeout when sending email

我正在尝试通过 gmail smtp 服务器使用 powershell 发送邮件。这是我的代码片段。

$sender = "user1@domain.com"
$recipient = "user2@domain.com"
$subject = "test"
$body = "test text"
$username = "user1@domain.com"
$password = "password"

$sc = new-object Net.Mail.SmtpClient("smtp.gmail.com", 465);
$sc.EnableSsl = $true;
$cred = New-Object System.Net.NetworkCredential($username,$password);
$sc.Credentials = $cred;
$emsg = new-Object System.Net.Mail.MailMessage($sender, $recipient, $subject, $body);
$sc.Timeout = 180000
$sc.Send($emsg);

每次我收到超时,即使我已将超时值固定为 3 分钟(180 秒)。更准确地说,错误是

Exception calling "Send" with "1" argument(s): "The operation has timed out."
At line:15 char:1
+ $sc.Send($emsg);
+ ~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SmtpException

以前有人遇到过这个问题吗?任何建议或想法将不胜感激。

发帖说它可以节省某人的时间...

$param = @{
SmtpServer = 'smtp.gmail.com'
Port = 587
UseSsl = $true
Credential  = 'user1@domain.com'
From = 'user1@domain.com'
To = 'user2@domain.com'
Subject = 'test'
Body = "testtext"
}
Send-MailMessage @param

PS here.