Share via

Error when running script for automated emails in Windows Server 2016

Anonymous
2021-12-27T16:04:19+00:00

This is on Windows Server 2016. Running on PowerShell version 5.1.14393.4583. It was working before. No changes were made to the script or the server. Suddenly the script throws the following error message:

Exception calling "Send" with "1" argument(s): "Failure sending mail."

At line:1 char:1

+ $smtp.send($message)

+ ~~~~~~~~~~~~~~~~~~~~

**+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException** 

**+ FullyQualifiedErrorId : SmtpException** 

Exception calling "Send" with "1" argument(s): "Failure sending mail."

At line:1 char:1

+ $smtp.send($message)

+ ~~~~~~~~~~~~~~~~~~~~

**+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException** 

**+ FullyQualifiedErrorId : SmtpException**  

====================Here's the script (edited for privacy)===================================

Internal

$date = Get-Date -format "yyyy-MM-dd"

$Path = "c:\folder"

$file = Get-ChildItem -Path $Path -File | Sort CreationTime | Select -Last 1

$fileName = $file -replace ".xlsx", ""

write-host $fileName

$SMTPServer = "smtp.office365.com"

$Username = "******@email.com"

$Password = "password"

$to = "@email.com,@email.com,******@email.com"

#$to = "******@email.com"

#$cc = "******@email.com"

$bcc = "******@email.com"

$date = Get-Date -format "yyyy-MM-dd"

$Subject = $fileName

$attachment = "c:\folder" + $file

$message = New-Object System.Net.Mail.MailMessage

$message.subject = $subject

##$message.body = $body

$message.to.add($to)

#$message.cc.add($cc)

$message.bcc.add($bcc)

$message.from = $username

$message.attachments.add($attachment)

$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer);

$smtp.EnableSSL = $true

$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);

$smtp.send($message)

=======================================================

Please advise.

Thank you.

SOLVED: https://office365itpros.com/2021/01/14/powershell-scripts-fail-exchange-online-tls12/

Windows for home | Other | Apps

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

4 answers

Sort by: Most helpful
  1. Anonymous
    2022-03-02T20:48:25+00:00

    Was this answer helpful?

    3 people found this answer helpful.
    0 comments No comments
  2. Anonymous
    2022-03-02T21:15:47+00:00

    That worked! Thank you!

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2022-03-02T20:40:42+00:00

    This is also happening to a client of mine, no changes on any side and we are getting the error.

    Any updates or link to the thread on the suggested site?

    Was this answer helpful?

    0 comments No comments
  4. DaveM121 891.6K Reputation points Independent Advisor
    2021-12-27T16:36:55+00:00

    Hi LR_KIT

    I am Dave, I will help you with this.

    I apologize, Community is just a consumer forum, due to the scope of your question (Server 2016) can you please post this question to our sister forum on Microsoft Q&A in the Server 2016 section (linked below)

    Over there you will have access to a host of Server 2016 and IT Pro experts and will get a knowledgeable and quick answer to this question.

    https://docs.microsoft.com/en-us/answers/topics...

    Was this answer helpful?

    0 comments No comments