G
G
Gennady2016-02-24 14:51:18
PowerShell
Gennady, 2016-02-24 14:51:18

Why is an email not being sent from PowerShell?

Good afternoon!
There is this script:

$serverSmtp = "smtp.yandex.ru" 
$port = 465
$From = "[email protected]" 
$To = "[email protected]" 
$subject = "Письмо"
$user = "mylogin"
$pass = "mypassword"


#Создаем экземпляр класса
$mes = New-Object System.Net.Mail.MailMessage

#Формируем данные для отправки
$mes.From = $from
$mes.To.Add($to) 
$mes.Subject = $subject 
$mes.IsBodyHTML = $true 
$mes.Body = "<h1>Тестовое письмо</h1>"

#Создаем экземпляр класса подключения к SMTP серверу 
$smtp = New-Object Net.Mail.SmtpClient($serverSmtp, $port)

#Сервер использует SSL 
$smtp.EnableSSL = $true 

#Создаем экземпляр класса для авторизации на сервере яндекса
$smtp.Credentials = New-Object System.Net.NetworkCredential($user, $pass);

#Отправляем письмо
$smtp.Send($mes)

Letters are not sent :( a couple of letters slipped through, reached. At the same time, the script hangs for a long time and gives the following error:
Исключение при вызове "Send" с "1" аргументами: "Время ожидания операции истекло."
строка:30 знак:1
+ $smtp.Send($mes)
+ ~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : SmtpException

Tell me what's wrong with the script? The meaning of the phrase "The operation timed out" is clear to me. It is not clear why several times the letters left and no longer go away. And it's not clear how to debug this thing and understand where the problem is

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Lynx White, 2016-03-03
@genana40

Try changing the port to 587. Port 465 for SSL is marked as Deprecated, perhaps Yandex cut it out.

A
azarij, 2016-02-24
@azarij

and through another service (gmail, mail.ru, etc) tried the same procedure?
given that the same script has successfully sent soap before, the problem may not be in the script.
you can try to increase the timeout:
$smtp.timeout = 1000000
but I doubt it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question