Wednesday 19 March 2014

Send mail Using PowerShell

There are couple of ways to send mail using PowerShell cmdLet.
I 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
$web = Get-SPWeb -Site http://q3gn0470:2222
$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)

2 comments :

  1. I was wondering if you ever thought of changing the page layout of your site?
    Its very well written; I love what youve got to
    say. But maybe you could a little more in the way of content
    so people could connect with it better. Youve got an awful
    lot of text for only having 1 or 2 pictures.
    Maybe you could space it out better?

    Here is my page: slow pitch

    ReplyDelete
  2. Hi! I realize this is kind of off-topic however I had to ask.
    Does building a well-established blog like yours
    require a massive amount work? I am completely new to writing a blog but I do write in my diary daily.

    I'd like to start a blog so I can share my experience and feelings online.
    Please let me know if you have any recommendations or tips
    for new aspiring blog owners. Thankyou!

    Feel free to surf to my homepage ... citywide home loans reviews denver

    ReplyDelete