Menu Close

How To Send Emails With Attachment From Powershell (2021)

Today I’m going to show you on how to send emails with attachment from Powershell. Many organizations have SMTP servers to trigger emails from an application server to recipients. What admins normally do when a user reports an issue that emails aren’t being delivered from a server.

They’d most likely request them to try the telnet test to validate the mail flow. Of course, telnet works to verify mails but it doesn’t support emails with attachment and its a lengthy process to follow. Therefore, I would suggest that try sending an email using the PowerShell script as it is the easiest and effective method to perform.

$fromaddress = This is the sender email address
$toaddress = This is the recipient email address
$Subject = Subject of an email
$body = Make an HTML for an email body
$attachment = This is the location of an attachment file
$smtpserver = “SMTP SERVER NAME HERE”

Here is the full script on how to send emails with an attachment from PowerShell

#####Define Variables#####

$fromaddress = “admin@techieberry.com”
$toaddress = “user@techieberry.com”
$Subject = “Email Attachment Test”
$body = get-content “C:\users\desktop\content.html”
$attachment = “C:\users\desktop\test123.txt”
$smtpserver = “SMTP SERVER NAME HERE”

##################################

$message = new-object System.Net.Mail.MailMessage
$message.From = $fromaddress
$message.To.Add($toaddress)
$message.IsBodyHtml = $True
$message.Subject = $Subject
$attach = new-object Net.Mail.Attachment($attachment)
$message.Attachments.Add($attach)
$message.body = $body
$smtp = new-object Net.Mail.SmtpClient($smtpserver)
$smtp.Send($message)

####################################################

How to execute the script?

  • Make sure you installed a latest Windows Powershell
  • Copy the script
  • Paste it on a notepad file
  • Save it as filename.ps1
  • Access “Windows PowerShell
  • Go to the location of a saved file and execute them.
  • If you face any error in above steps, right-click the .ps1 file, Click “Edit” and press F5 key to execute the same.

Screenshot of the script is below.

how to send emails with attachment from PowerShell

As you can see, this is easy to follow and more importantly, emails receive faster when they send it from the PowerShell.

Also Read: How To Encrypting Office 365 Emails Effectively?

Want to improve your Exchange Online experience for better productivity? Check out the tips and tricks mentioned here.

Now I’d like to hear from you:

Did I Miss Anything? Or maybe you have a question about something that I covered.

Either way, I’d like to hear from you. So go ahead and leave a comment below.

Related Posts

7 Comments

  1. Francisco

    Hi.

    I keep getting this error message.

    STARTTLS is required to send mail [CP2P152CA0044.LAMP152.PROD.OUTLOOK.COM]”

    And where goes the credentials?

    • techieberry

      Thank you for the question. You could take these into consideration:

      1. Please check and turn off the firewall or any other scanning software temporarily.

      2. Make sure the sending server’s IP is not on an SMTP block list.

      3. Verify that the sending and receiving server is configured to use TLS.

      4. Check if the receiving server is configured to only respond to SMTP (not ESMTP) commands. TLS is part of the ESMTP protocol.

  2. Sachin Tak

    Exception calling “Send” with “1” argument(s): “Error in processing. The server response was: 5.7.3 STARTTLS is required to send
    mail [BMXPR01CA0059.INDPRD01.PROD.OUTLOOK.COM]”

    • techieberry

      Thank you for the question. You could take these into consideration:
      1. Please check and turn off the firewall or any other scanning software temporarily.
      2. Make sure the sending server’s IP is not on an SMTP block list.
      3. Verify that the sending and receiving server is configured to use TLS.
      4. Check if the receiving server is configured to only respond to SMTP (not ESMTP) commands. TLS is part of the ESMTP protocol.

  3. Mohit

    Hi

    I created a powershell task with the above code and executed on the Azure DevOps pipeline. It seems it didn’t run and displayed a message as:

    SendEmail

    Evaluating: succeeded()
    Result: False

    Would you be able to suggest what I could be missing?

    Another point was, I was not sure how would I get the html body content from Azure DevOps, so I had commented this line on the script.

  4. Mohit

    I created a .ps1 file and copied the above code. I am running the file locally on my system and I’m getting this error: \Documents\SendEmail.ps1 cannot be loaded because running scripts is disabled on this system.
    For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.
    + CategoryInfo : SecurityError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnauthorizedAccess

Leave a Reply

Your email address will not be published. Required fields are marked *