Cause of a Powershell Scripting Error for sending email with attachment

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
   ion
    + FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage
100questionsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tahir QureshiSystem AnalystCommented:
you have to share your script Plus what command you are using
plus check smtp port is open or not
MichelangeloConsultantCommented:
Check the SMTP server address: is it correct?is Port 25 reachable from where the script is running? As Tahir advised, provide more details.
100questionsAuthor Commented:
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 = 'email1@gmail.com'
$password = 'password'
$sendMailArgs = @{
      To =                  'email2@gmail.com'
      From =                  $username
      Cc =                  'email3@gmail.com'
    Bcc =           'email1@hotmail.com'
      Subject =            'File as Attached'
      Body =                  'The file is attached'
      Attachments =      'C:\Users\User1\Desktop\Folder\File.pdf'
      SmtpServer =      'smtp.gmail.com'
      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"
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
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?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
100questionsAuthor Commented:
Outlook works fine.
And I even renamed the attachment and it still did not work.
Jose Gabriel Ortega CastroCEO Faru Bonon IT - EE Solution ExpertCommented:
I've done it 2 ways:

$username = 'jortega928@gmail.com'
$password = 'xxx'
 

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

$msg.From = $username
$msg.To.Add("jortega@faboit.com") 
$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"
$msg.Attachments.Add("Z:\WORKING_DIR\file1.csv")

try{
$SMTP.Credentials = New-Object System.Net.NetworkCredential("$username", "$password"); 
$smtp.Send($msg)
}
catch{
    $er = $_.Exception.Message
    Write-Error $_.Exception.Message
}

Open in new window


(Other way):
try{
    $From = "jortega928@gmail.com"
    $To = "jortega@faboit.com"
    $Cc = "ceo@faboit.com"
    $Attachment = "Z:\WORKING_DIR\file1.csv"
    $Subject = "Email Subject"
    $Body = "Insert body text here"
    $SMTPServer = "smtp.gmail.com"
    $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
}
catch{
    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.
https://myaccount.google.com/lesssecureapps

If you ask me I'd use the second since I wouldn't need to hardcode my password on the script ;)
100questionsAuthor Commented:
The smtp server was rejecting.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.