Send-mailmessage: Using Powershell to send email (2021)

In this post let’s understand how send-mailmessage works step-by-step. Send-mailmessage with multiple recipient sometimes become hectic as we make some silly mistake. I too had made very common mistake which I will discuss in this post, Make sure you don’t miss the ending.

Send-mailmessage : The working

If you have followed me for a long then you may have seen many scripts which are having send-mailmessage. So let’s understand what exactly is send-mailmessage.

Send-mailmessage is used to send email in Powershell. It generally uses SMTP(Simple mail transfer Protocol) server to send mail. Sending mail becomes too easy with this. Let’s check out the parameters we can use with this.

Parameters: Source Microsoft

-Attachments

Specifies the path and file names of files to be attached to the email message. You can use this parameter or pipe the paths and file names to Send-MailMessage.

TABLE 1
Type:String[]
Aliases:PsPath
Position:Named
Default value:None
Accept pipeline input:True
Accept wildcard characters:False
-Bcc

Specifies the email addresses that receive a copy of the mail but are not listed as recipients of the message. Enter names (optional) and the email address, such as Name <someone@fabrikam.com>.

TABLE 2
Type:String[]
Position:Named
Default value:None
Accept pipeline input:True
Accept wildcard characters:False
-Body

Specifies the content of the email message.

TABLE 3
Type:String
Position:2
Default value:None
Accept pipeline input:True
Accept wildcard characters:False
-BodyAsHtml

Specifies that the value of the Body parameter contains HTML.

TABLE 4
Type:SwitchParameter
Aliases:BAH
Position:Named
Default value:None
Accept pipeline input:True
Accept wildcard characters:False
 

Send-mailmessage: Multiple recipients

Take a scenario, you need to schedule a task using Powershell and results should be sent via mail to multiple recipients which sometimes makes it more difficult because it gives an error or it is not sent to one of the recipient. Let’s take a look at how do we achieve sending mail to multiple recipients.

				
					$recipients = 'support@powershellguru.com','yourname@domain.com'

send-mailaddress -from 'mymail@domain.com' -to $recipients -smtpserver 'smtpservername'


				
			

The above code will work if you provide the correct SMTP server name or IP address. So make sure you fill it correctly.

Send-mailmessage: SMTPserver

From above it should be clear now that we need the SMTP server name or IP address to send mail. But sometimes the default port 25 is not used by SMTP and can vary in different enviornment. For this purpose, we use -port parameter in which we can specify the port number which our SMTP server use.

				
					send-mailaddress -from 'mymail@domain.com' -to $recipients -smtpserver 'smtpservername' -port 440

				
			

Unable to delete unwanted software or software don't get cleaned up fully ?

Special Uninstaller is a great application to delete or uninstall software that is not required. The plus point is it clears every file and registry key associated with the application. It is available for Free and Premium with 24×7 support. Do try this application out.

Send-mailmessage: Body and BodyAsHtml

Ever had an urgency to send a HTML report and the HTML report should be shown in the mail itself then we need to use -body , -bodyashtml parameters. Let’s check out how we actually use them. Also -bodyashtml should be mentioned after -body or else you will encounter an error.

				
					send-mailaddress -from 'mymail@domain.com' -to $recipients -smtpserver 'smtpservername' -body "some html output" -bodyashtml

				
			

Send-mailmessage: Attachments

Maybe you decided to send the html report fetched as an attachment in the mail. Then -attachments parameter can be used to complete this task. Just we need to provide the location of the output file and it will be attached to the mail.

				
					send-mailaddress -from 'mymail@domain.com' -to $recipients -smtpserver 'smtpservername' -attachments "path"
				
			

Send-mailmessage: The other way to use

Don’t get tricked by this as there is another effective way of sending mail and I am gonna share it with you. It needs to create an object so stay sharp and check it out. As it is self explanatory as well.

				
					$smtpServer = "server name or IP" 
  $smtp = New-Object Net.Mail.SmtpClient($smtpServer) 
  $msg = New-Object Net.Mail.MailMessage 
  $msg.To.Add($user)
  #$msg.cc.add("") 
        $msg.From = "" 
  $msg.Subject = "Random subject name" 
        $msg.IsBodyHTML = $true 
        $msg.Body = get-content "path" 
  $smtp.Send($msg) 
        $body = "" 
				
			

Conclusion

Let’s be fair you didn’t know about the other way to send email using Powershell. I hope this whole post has filled some gaps in your understanding of this topic. I wish to see you very soon with a new interesting post. If you are not subscribed to this website you are missing a lot of interesting concepts. Feel free to comment down below and let me know if you have any issue.

Related Posts

Leave a Reply

Please disable your adblocker or whitelist this site!