Cause of a Powershell Scripting Error for sending email with attachment

Member_2_5306354 used Ask the Experts™
What would cause this type of error in a Powershell script?

Send-MailMessage : The operation has timed out.
At C:\Users\Name\Desktop\Folder\script.ps1:17 char:1
+ Send-MailMessage @sendMailArgs
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpExcept
    + FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Tahir QureshiSystem Analyst

you have to share your script Plus what command you are using
plus check smtp port is open or not
MichelangeloSystem Administrator / Postmaster

Check the SMTP server address: is it correct?is Port 25 reachable from where the script is running? As Tahir advised, provide more details.


The port is open, since I am able to use the following script however it's successful only when I use it without the attachment.

$username = ''
$password = 'password'
$sendMailArgs = @{
      To =                  ''
      From =                  $username
      Cc =                  ''
    Bcc =           ''
      Subject =            'File as Attached'
      Body =                  'The file is attached'
      Attachments =      'C:\Users\User1\Desktop\Folder\File.pdf'
      SmtpServer =      ''
      Port =                  587
      UseSsl =            $true
      Credential =      New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, (ConvertTo-SecureString $password -AsPlaintext -Force)
Send-MailMessage @sendMailArgs
Write-Host "Mail Sent"
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
Then it seems that your SMTP server rejects the attachment.
Have you tried to send the same mail using e.g. Outlook?
Do you have access to the server log?
Does it work with a different attachment?


Outlook works fine.
And I even renamed the attachment and it still did not work.
Jose Gabriel Ortega CastroTop Rated Freelancer on MS Technologies
Awarded 2018
Distinguished Expert 2018

I've done it 2 ways:

$username = ''
$password = 'xxx'

$smtp = new-object Net.Mail.SmtpClient('') 
$smtp.EnableSsl = $true 
$msg = new-object Net.Mail.MailMessage  

$msg.From = $username
$msg.BodyEncoding = [system.Text.Encoding]::Unicode 
$msg.SubjectEncoding = [system.Text.Encoding]::Unicode 
$msg.IsBodyHTML = $true  
$msg.Subject = "File is Attached" 
$msg.Body = "The file is attached"

$SMTP.Credentials = New-Object System.Net.NetworkCredential("$username", "$password"); 
    $er = $_.Exception.Message
    Write-Error $_.Exception.Message

Open in new window

(Other way):
    $From = ""
    $To = ""
    $Cc = ""
    $Attachment = "Z:\WORKING_DIR\file1.csv"
    $Subject = "Email Subject"
    $Body = "Insert body text here"
    $SMTPServer = ""
    $SMTPPort = "587"
    Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject `
    -Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl `
    -Credential (Get-Credential -UserName $From -Message "Insert the password for $From") -Attachments $Attachment
    Write-Error $_.Exception.Message

Open in new window

I get the following "The SMTP server requires a secure connection or the client was not authenticated.
error 5.5.1 (Authentication required).

This is because I have disabled this  (Less secure app) when I enabled it works.

If you ask me I'd use the second since I wouldn't need to hardcode my password on the script ;)


The smtp server was rejecting.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial