D
D
dwordf2021-09-30 12:13:33
PowerShell
dwordf, 2021-09-30 12:13:33

Sending an email via Powershell. Where is the mistake?

$EmailFrom = 'почта откуда отправляю@yandex.ru'
$EmailTo = 'куда@gmail.com'
$Subject = "‪C:\1\1.png"
$Body = "Test_1"
$SMTPServer = 'smtp.gmail.com'
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 465)
$SMTPClient.Credentials = New-Object Net.NetworkCredential("почта", "пароль");
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)


Error
Exception when calling "Send" with "4" arguments: "Failed to send e-mail message."
string:8 character: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

1 answer(s)
M
MaxKozlov, 2021-09-30
@dwordf

Port 465 - smtps. it is not supported by .net
https://docs.microsoft.com/en-us/dotnet/api/system...
here they write that StartTLS is used, which means you need to use a different port - 587
and starttls: You
$SMTPClient.EnableSsl = $true
may need more

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

and here is a big discussion with examples of what else you need to enable in your Google account (access to less secure app)
https://stackoverflow.com/questions/32260/sending-...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question