Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 7025

Deprecated: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 2162

Deprecated: Hook imagify_allow_picture_tags_for_webp is deprecated since version 2.2! Use imagify_allow_picture_tags_for_nextgen instead. in /www/collab365_296/public/wp-includes/functions.php on line 5758
How to send an email using PowerShell (SMTPClient and MailMessage) - Collab365
Deprecated: strstr(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1145

Deprecated: stripos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1152

Deprecated: stripos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1155

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1162

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1165

Deprecated: strstr(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1145

Deprecated: stripos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1152

Deprecated: stripos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1155

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1162

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1165

Warning: Undefined array key "url" in /www/collab365_296/public/wp-content/plugins/thrive-visual-editor/thrive-dashboard/inc/smart-site/classes/class-tvd-smart-shortcodes.php on line 85

2014-03-19

Send mail using PowerShell cmdLet.

generally use these cmdLet to verify outgoing email settings.

  1.  Using smtp address (Hardcoding the SMTP address)
     function send-notification($subject, $detail) {

    $message = New-Object System.Net.Mail.MailMessage
    $message.Subject = $subject
    $message.Body = $detail
    $message.To.Add("recipient1@domain.com")
    $message.To.Add("recipient2@domain.com")
    $message.Cc.Add("cc1@domain.com")
    $message.Bcc.Add("bcc1@domain.com")
    $message.From = "Admin@Domain.com" # we can use sent From as $headers.Add("from", "Admin@Domain.com") also
    
    $client = New-Object System.Net.Mail.SMTPClient -ArgumentList "192.168.0.1"   
    $client.Send($message)
}

$dbname = Get-SPContentDatabase -WebApplication http://servername:port
send-notification -subject "$dbname Attached" -detail "The content database $dbname has completed the database attach upgrade. Please review the logs in Central Administration as soon as possible."

2. Using Microsoft.SharePoint.Utilities.SPUtility

Updated:-

$web = Get-SPWeb -Site http://server:Port

$headers = New-Object System.Collections.Specialized.StringDictionary

        $headers.Add("to", "recipient1@domain.com")

        $headers.Add("to", "recipient2@domain.com")

        $headers.Add("cc", "cc1@domain.com")        

        $headers.Add("bcc", "bcc1@domain.com")        

        $headers.Add("from", "SpAdmin@domain.com")

        $headers.Add("subject", "Test Email Subject")

        $headers.Add("content-type", "text/html")

        $bodyText = "Hello how are you?"

        [Microsoft.SharePoint.Utilities.SPUtility]::SendEmail($web, $headers, $bodyText)

You can check the same here also

 

(Visited 1,449 times, 1 visits today)

About the author 

Ajeet Kumar Singh

Summit Bundle

Get 200+ hours of Microsoft 365 Training for 27$!

Master Office 365, Power Platform & SharePoint & Teams With 200+ Hours Of Training Videos and 108 Ebooks in the Collab365 Academy. This offer is insane and is only available for a limited period.