Menu Close

How To Send Emails With Attachment From PowerShell (2024)

Let’s explore a simple and efficient way on how to send emails with attachment from PowerShell. In many organizations, SMTP servers play a crucial role in delivering emails from application servers to recipients. When users face issues with undelivered emails, admins often suggest the telnet test to validate mail flow. However, this process can be a bit lengthy and doesn’t support attachments.

So, here’s a friendlier alternative: why not give the PowerShell script a shot? It’s a breeze to use and can be a quick and effective solution for sending emails. Let’s dive in!

$fromaddress = Here’s the email address you’ll be sending from
$toaddress = Here’s the email address of the recipient
$subject = Choose a compelling subject for your email
$body = Craft an HTML format for the email body
$attachment = Locate the file you’d like to attach
$smtpserver = Specify the SMTP server name

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

Behold the complete script on how to send emails with 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)

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

To Run the Script:

  • Make sure you’ve got the latest Windows PowerShell installed.
  • Copy the script and paste it into a notepad file, saving it as filename.ps1.
  • Open “Windows PowerShell,”
  • Navigate to the saved file, and execute it.

If you encounter any errors, right-click the .ps1 file, choose Edit, and press F5 to run the script. Check out the screenshot below for reference.

how to send emails with attachment from PowerShell

Sending emails with attachments through PowerShell is not only easy but also ensures faster delivery. Plus, it’s a breeze to follow!

Looking to enhance your Exchange Online experience for better productivity? Explore the tips and tricks shared in the article.

Now, over to you:

Did I cover everything, or is there something you’d like to ask or add? Feel free to leave a comment below!

Related Posts

11 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

    • techieberry

      Create a notepad file and mention the body of the email and save it in html format. For example, test.html
      The contents available on the notepad file are contents of the email.

  5. Pingback:How to Use Power Automate to Send Emails From Excel (2022)

  6. Soundar

    I tried about code it was working fine. I need to send email to multiple recipients.

    I tried two times given my email id’s but getting only one time email.

    I don’t see anywhere in the script for password and port number mentioned.

    Pls assist on this.

Leave a Reply

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