S
S
squidw2019-01-24 10:13:05
PowerShell
squidw, 2019-01-24 10:13:05

How to send mail from cmd/powershell script?

I have a cmd file that performs a series of sequential actions, at the end of the script I want to add sending mail to check the success of the script. How can this be implemented? I found an option with cmd, but everyone describes the use of telnet, which, as far as I understand, is not safe and requires additional activation of the "telnet client" component, as far as I understand. I found another option with powershell, but I have an error when checking, besides, the question is how to call this * .ps1 from cmd?
ps error itself:

$EmailFrom = "моя почта"
$EmailTo = "почта получателя"
$Subject = "Test powershell mail from scheduler"
$Body = "Text"
$SMTPServer = "smtp.yandex.ru"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("логин, "пароль")
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)

Исключение при вызове "Send" с "4" аргументами: "Неправильная последовательность команд. Отклик сервера: 5.5.4 Error: send A
UTH command first."
F:\scripts\mail.ps1:9 знак:1
+ $SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : SmtpException

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
res2001, 2019-01-24
@squidw

You cannot send mail from cmd using built-in tools.
The telnet option works, but the native Windows telnet client (which must also be installed from the components) does not support any automation. You can send a letter manually with it, but not automatically.
You can find any other telnet client that can be controlled using command line parameters or in another way, or find a console mail client, which are also available. But all these are third-party tools that are not part of Windows.

M
Mikhail Lyalin, 2019-01-24
@mr_jok

forum.ru-board.com/topic.cgi?forum=8&topic=51370

A
azarij, 2019-01-24
@azarij

and if the port is changed to 465 or 25?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question