serverstackqns
serverstackqns

Reputation: 590

Powershell timeout when sending email

I am trying to send a mail using powershell by making use of gmail smtp server. Here's my code snippet.

$sender = "[email protected]"
$recipient = "[email protected]"
$subject = "test"
$body = "test text"
$username = "[email protected]"
$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);

Everytime I recieve a timeout, even if I have fixed the timeout value to 3 minutes(180 seconds). To be more precise, the error is

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

Have anyone experienced this issue before? Any suggestions or ideas would be really appreciated.

Upvotes: 2

Views: 9782

Answers (1)

serverstackqns
serverstackqns

Reputation: 590

Posting that it could save someone's time too..

$param = @{
SmtpServer = 'smtp.gmail.com'
Port = 587
UseSsl = $true
Credential  = '[email protected]'
From = '[email protected]'
To = '[email protected]'
Subject = 'test'
Body = "testtext"
}
Send-MailMessage @param

PS here.

Upvotes: 3

Related Questions