发送电子邮件时Powershell超时

时间:2015-04-10 07:09:25

标签: email powershell smtp smtpclient

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

$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

之前有没有人遇到过这个问题?任何建议或想法都会非常感激。

1 个答案:

答案 0 :(得分:0)

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

$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

相关问题