TheCodeBuzz

Best Practices for Software Development

  • Home
  • Series
    • .NET Core
    • Design Pattern
    • Code Smell
    • Angular
    • React
    • Vue.js
    • IoT
  • About

PowerShell Send Email using SMTP

Home >Automation>PowerShell Send Email using SMTP

PowerShell Send Email using SMTP

PowerShell- Send Email using SMTP

Today in this example, We will implement PowerShell Send Email using SMTP functionality. To make this even simpler I have used my own Gmail( modified below) for sending an email and testing purposes which worked very well after some initial travail.

We will Send-MailMessage cmdlet to send an email message with attachments using the SMTP server and client.

Today in this article, we will cover below aspects,

  • Send-MailMessage cmdlet to send email
  • Powershell- Send an email with attachment

Send-MailMessage cmdlet to send email

The Send-MailMessage cmdlet sends an email message using PowerShell conveniently.

You must specify a Simple Mail Transfer Protocol (SMTP) server details using Net.Mail.SmtpClient.Below I have used Gmail SMTP server details.

Below is the code added to send email in PowerShell

    $message = new-object Net.Mail.MailMessage;
    $message.From = "go.digital.development@gmail.com";
    $message.To.Add($email);
    $message.Subject = "Sending email from PowerShell";
    $message.Body = "Hi There";
    $smtp = new-object Net.Mail.SmtpClient("smtp.gmail.com", "587");
    $smtp.EnableSSL = $true;
    $smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
    $smtp.send($message);

Powershell- Send an email with attachment

If you want to send an email with the attachments, Please specify the attachments as below,

 $attachment = New-Object Net.Mail.Attachment($attachmentpath);
 $message.Attachments.Add($attachment);

To make use of GMAIL to send emails to other domains, you need to use some settings to make it work.

I also got an email from Gmail saying “Critical security alert for your linked Google Account.”

  • I had to enable “Allow less secure apps: ON” to make send email API work properly.

Once configured successfully you shall see an email in your mailbox.

PowerShell Send Email smtp

Note: As per Microsoft The Send-MailMessage cmdlet is obsolete. This cmdlet does not guarantee secure connections to SMTP servers. For more details see here

References :

  • Making a PowerShell POST request with body

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



facebookShare on Facebook
TwitterPost on X
FollowFollow us
PinterestSave

Post navigation

System.Text.Json – Dictionary Serialize and Deserialize
How to Define an environment variable on Azure Cloud
get-free-ebook-micro-services-guideline
blank

©TheCodeBuzz 2024. All rights reserved. Privacy Policy.

To serve the best User Experience on the website, we use essential cookies. By clicking “Continue”, Please give consent to our privacy policy.
SettingsContinue to Site
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT
RSS
Follow by Email
Facebook
fb-share-icon
X (Twitter)
Post on X
LinkedIn
Share