• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

TECHDIIP

  • Tech
  • Windows
  • Powershell
  • Scripts
  • News
  • Games

How to send an email from Powershell

Send emails from Powershell script, yes we can send any data as an email format using a Powershell script, if one wants to send the script output as an email then here are the steps how to do it. When you have a written script for a specific task and you want to send the output as an email report to the recipients then here are simple steps to accomplish it. In Powershell there is a command Send-MailMessage sends an email message from Powershell, all you need to do mention the simple mail transfer protocol (SNMP) server and the other parameters From, To, the subject is also required, see some of the examples. Send-MailMessage syntax Send-MailMessage [-To] <string[]> [-Subject] <string> [[-Body] <string>] [[-SmtpServer] <string>] -From <string>.

Example 1

Send-MailMessage -From 'john<john@abc.com>' -To 'raj<raj@techieseek.com>' -Subject 'breaking news'

Example 2: Send an attachment

Send-MailMessage -From 'john<john@abc.com>' -To 'raj<raj@techieseek.com>' -Subject 'breaking news' -Body "sending the attachment" -Attachments .\data.csv -Priority High -Smthttps://techdiip.com/wp-admin/post-new.php#pServer 'smtp.abc.com'
Parameters Explained. Attachments<String[]>   We have to specify the path of the attachment and file name. Bcc<String[]> This parameter is used for Email addresses that receive a copy of an email message but does not appear as a recipient. Body <String_>This parameter is used for writing the Content of the email message BodyAsHtml It indicates that the content is in HTML format. Cc<String[]>This parameter used for sending CC Email addresses that receive a copy of an email message Credential  This parameter is used for Specifying a user account that has permission to send message from a specified email address. DeliveryNotificationOption From  This parameter used for Email addresses from which the mail is sent Port:  This parameter is used for specifying the Alternate port on the SMTP server. Priority   This parameter is used for defining the priority of the email message. Acceptable values: Normal, High, Low. SmtpServer    This parameter is used for specifying the Name of the SMTP server that sends the email message. Subject     This parameter used for specifying the Subject of the email message. To:  This parameter used for specifying  Email addresses to which the mail is sent. UseSsl  This parameter Uses the Secure Sockets Layer (SSL) protocol to establish a connection to the remote  computer to send mail DeliveryNotificationOption:  Specifies the delivery notification options for the email message. Multiple values can be specified. You can specify multiple values. The acceptable values for this parameter are as follows: None: This option for No notification. OnSuccess: This option to Notify if the delivery is successful. OnFailure: This option to Notify if the delivery is unsuccessful. Delay:  This option to Notify if the delivery is delayed. Never: This option for Never notify. Encoding This parameter is used for Specifying  the type of encoding for the target file and the acceptable values for this parameter are as follows: ASCII, BigEndianUnicode, OEM, Unicode, UTF7, UTF8, UTF8BOM, UTF8NoBOM and UTF32. How to send text file data as email body text using Powershell script.
$businesstxt_File = "c:\businessnews.txt"

function Send_textfile_mail
          {
   #specifying the Email settings

$EmailFrom = "raja@abc.com"
$EmailTo = "techierun@abc.com"
$Txt_Body = Get-Content $businesstxt_File -RAW
$Body = $Body_Custom + $Txt_Body
$Subject = "Business text file"
$SMTPServer = "smtpserver.domain.com"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)
$SMTPClient.EnableSsl = $false
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
       }
$Body_Custom = "Please see the business text file information : "

#calling the function to work!

Send_textfile_mail
Here are the are top SMTP email providers SMTP addresses and these will be useful when you want to try out Powershell script using a free hosting provider.

The Email Service Provider Name

Name of the SMTP server

Port Number

Connection

Gmail

smtp.gmail.com

587

TLS

25

TLS

465

SSL

Outlook.com

smtp-mail.outlook.com

587

TLS

25

Office 365

smtp.office365.com

587

TLS

25

Windows Live Hotmail

smtp.live.com

587

TLS

25

465

SSL

Yahoo mail

smtp.mail.yahoo.com

587

TLS

25 TLS
Thank you for reading this article, if you have any questions please let us know. Thank you for visiting my site, for any scripts in these articles you are testing please make sure you have tested this script in our lower environment before you run in production.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

Recent Posts

  • Fixed : Page fault in nonpaged area 2022
  • Solved: Ethernet doesn’t have a valid IP configuration 2022
  • 5 Best Free Youtube Video to MP3 Converters in 2022
  • Fixed : Audio renderer error 2022
  • Fixed: Discord search not working 2022
  • About Us
  • CONTACT US
  • Disclaimer
  • Privacy Policy

Copyright © 2022 techdiip.com