Powershell Script To Send Email Via Smtp With Attachment

Powershell Script To Send Email Via Smtp With Attachment – Windows PowerShell is mainly known as a command line shell that is used to solve some administrative tasks in Windows and applications that run in this operating system. At the same time, it is a scripting language that allows you to configure cmdlets – lightweight commands to perform specific tasks. And today we will talk

You only need to enter a sender and recipient email address, as well as specify the SMTP server you are going to use. Then copy and paste this script into your PowerShell and press Enter.

Powershell Script To Send Email Via Smtp With Attachment

Powershell Script To Send Email Via Smtp With Attachment

To check that the code works correctly, you can send a test email to a virtual inbox. For this, you need a suitable email testing solution, such as an email sandbox that belongs to the email delivery platform.

How To Use The Powershell Script

Email Sandbox gives you a virtual inbox (or up to 300 virtual inboxes with your highest plan) to which you can send test emails and then inspect and debug them. Thus, the solution creates a safe environment for you to find any issues related to your emails without the risk of spamming genuine recipients or ruining your domain reputation because you have to try. No need to use your personal inbox anymore.

Powershell Script To Send Email Via Smtp With Attachment

Email Sandbox features include HTML/CSS analysis, spam score checking and email content preview, insight into valuable technical information, domain blacklist reports, and more. This means that this trial solution will be useful, regardless of whether you are working with plain text or HTML emails, include images or attachments or not, etc.

Here, we have only defined the SMTP server. The recipient and sender addresses are not real, as you might have guessed. And that’s one of the benefits of using an email testing solution: you don’t need to handle an actual email to test your email workflow.

Powershell Script To Send Email Via Smtp With Attachment

How To Send An Email Using Windows Powershell

Once you copy and paste this script into PowerShell and press Enter, a window will appear asking for your virtual inbox’s SMTP credentials (username and password). You can find these credentials in your account under Email Sandbox → Inboxes → SMTP Settings.

Enter the credentials using the pop-up window and continue. After a while, the test email should appear in your virtual inbox.

Powershell Script To Send Email Via Smtp With Attachment

Alternatively, you can use a fake email, but this can be a bad testing exercise. Learn why using a dummy email to test just doesn’t work in our blog post.

Powershell: Alert Me When Disk Space Is Running Low On My Windows Servers (e Mail Notification)

Yes you can! Send-MailMessage allows you to enhance your email with many parameters including HTML content, priority, etc. Additionally, you can send emails to multiple recipients by specifying them in the relevant parameters. Here’s what you can use:

Powershell Script To Send Email Via Smtp With Attachment

The senders specified in the form parameter will be notified upon delivery of the email. Here are the options:

So let’s create a script that will send an email with HTML text and an attachment. This time, we will already define the parameters and then refer to them

Powershell Script To Send Email Via Smtp With Attachment

Create An Interactive Active Directory Html Report With Powershell

You can now see what this email looks like and analyze its HTML/CSS using Email Sandbox’s HTML Validation feature. This feature gives you information about important email client support for HTML elements and CSS rules used in your emails.

For any unsupported or partially supported element/rule, it will provide a list of email clients experiencing the problem, as well as a link to the line of code that contains the element/rule.

Powershell Script To Send Email Via Smtp With Attachment

The email delivery platform also includes an email sending solution called the Email API that you can use to send your emails to actual recipients from PowerShell or any other application.

Use Azure To Send Email With Sendgrid, Graph And Office 365

The email API provides throughput for sending up to ~10,000 emails/second but also more control over email delivery thanks to its actionable analytics features.

Powershell Script To Send Email Via Smtp With Attachment

Using actionable analytics features including shipping alerts, 60-day email logs, dashboards with critical metrics, and webhooks, you can find and fix shipping issues in no time.

Also, if you really want to dig deeper into your email performance, you can filter the performance statistics that the Email API shows you by email category, mailbox provider, or domain. .

Powershell Script To Send Email Via Smtp With Attachment

Unread Email Ews Powershell Module With Reply And Forward Counting

Other email API features include dedicated IPs, automatic IP warming, and blacklists, just to name a few.

First, add a domain to the email API and confirm → Submit the domain to your account, as explained in the video below.

Powershell Script To Send Email Via Smtp With Attachment

Next, to send with the email API in Windows PowerShell, enter the SMTP credentials provided by the Send solution in the script we used earlier.

Configure The Email Media Type

Finally, run the script, enter the username and password you are asked for, and your email should be sent successfully.

Powershell Script To Send Email Via Smtp With Attachment

If you want to send emails through Gmail’s SMTP server, just add smtp.gmail.com as your SMTP server.

We tried and filled the credentials in the popup window. But the server responded with an error: 551 Authentication Required. To fix this issue, you need to enable access to less secure apps in the security tab of your Google account. We did this and PowerShell successfully sent our message to the destination. Learn more about SMTP response codes and commands in our dedicated blog post.

Powershell Script To Send Email Via Smtp With Attachment

Configure Anonymous Smtp Relay In Exchange Server

Not everyone is a Gmail user. Therefore, the following data may be correct if you use another SMTP host.

EASendMail is an SMTP component that supports protocols such as SMTP, ESMTP (Exchange Web Service), EWS (Exchange Web Service), and WebDAV (Web Distributed Authorization and Versioning). The component is primarily used to send emails by COM/.NET/.NET Core applications and applications built with:

Powershell Script To Send Email Via Smtp With Attachment

Also, you can use EASendMail to send emails from PowerShell. Here’s a guide to what you might need in the event you need it:

How To Send Emails From Powershell

In the code above, we used the email sandbox SMTP credentials, and that’s why the email ended up there. Of course, you can change this according to your needs.

Powershell Script To Send Email Via Smtp With Attachment

EASendMail’s SMTP component is useful for sending large numbers of emails. For this, you don’t need to code many themes. All you need is the EASendMail service that can automatically send multiple-threaded messages to a background queue. Take a look at the script below to mass mail a message with an HTML template:

The Send-MailMessage cmdlet is the most common option for sending emails from PowerShell. But it was not always like that. It became available starting with PowerShell 3.0 and was based on the System.Net.Mail API. It is a namespace that contains classes for sending electronic messages to an SMTP server. Delivery is done by

Powershell Script To Send Email Via Smtp With Attachment

Send Sms Notifications With Powershell

The System.Net.Mail namespace is also used to create and send messages in ASP.NET C# which we also blogged about. And that’s all for today. We hope this blog post will enhance your skills in using PowerShell. Follow our blog and discover useful information for your projects. Let’s use the PowerShell console to send email messages with attachments. First, open a PowerShell window from the Start menu. Type PowerShell and click Apply.

The following script allows you to send an email message using PowerShell. Use the FROM and TO fields to specify the sender and recipient, as well as the subject, body, and attachment arguments to create the email message. A PowerShell script to send email using an SMTP server requires an SMTP server, username, and password.

Powershell Script To Send Email Via Smtp With Attachment

Just copy/paste the script into the PowerShell window you opened in the first step. But remember that you need to configure some command line arguments and switches. Otherwise, the script will not work in your environment.

Chocolatey Software Docs

If you don’t know what values ​​to use for SMTP server and port, see this page for SMTP settings for popular email providers.

Powershell Script To Send Email Via Smtp With Attachment

If you look at the example above, you will see backslashes at the end of each line. This is PowerShell’s way of splitting a long command into multiple lines. If you want to use the command as a single line, copy/paste the following code:

As you may have noticed, we are using a built-in PowerShell function Send-MailMessage. In the PowerShell world, such functions are called cmdlets. Here is an official PowerShell email document.

Powershell Script To Send Email Via Smtp With Attachment

Converting Unattended Powershell Script To Modern Authentication To Send Email

Below is a slightly weird section that sets the username and password for the SMTP server. PowerShell email sending credentials require a PSCCredential object, which we create using a combination of commands that ultimately instantiate a PSCredential object from the System.Management.Automation namespace.

* The PSC Credential class expects us to provide a password or secure string, but what we want to pass is a plain text string, so we need to change it.

Powershell Script To Send Email Via Smtp With Attachment

With a little change in the above script we can send bulk emails in HTML format. It’s easy! Just add

Sharepoint Online: Powershell To Send Email

Note that the body text contains HTML tags. Text in italics and bold. As you can imagine, this will appear in an italic font or as text that is thicker than the surrounding text. Here is some HTML code:

Powershell Script To Send Email Via Smtp With Attachment

You can also add other HTML tags like A, BR, DIV, FONT, H1, IMG and more. Randeep Bhoyan has compiled a list of allowed HTML tags in the email body.

But of course you have to give it a try, because email clients and the Internet are rapidly evolving and things that work.

Powershell Script To Send Email Via Smtp With Attachment

Powershell Script To Generate Office 365 Groups Reports

Similar Posts