SharePoint: Send Mail by using Powershell

Powershell has great functionalities. One of those is the sendmail function. We used it like a timer job for reminding people to do something special. We built a small script which checks certain list items in SharePoint and fetches the title, the create date and the author. If the item is older than 30 days, the author gets a reminder to do a task on the item.

The powershell is stored as scheduled task in windows. But i’d like to share the sendmail function with you.



$to = "you@mailadress.com"
$subject = 'Subject Text'
$body = 'Body Text'

#SMTP server name (which is configured in SharePoint)
$smtpServer = "SMTPServer"
#Creating a Mail object
$msg = new-object Net.Mail.MailMessage
#Creating SMTP server object
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
#Email structure
$msg.From = "yoursender@mailadress.com"
$msg.To.Add($to.Email)
$msg.subject = $subject
$msg.body = $body
$msg.isBodyHTML = $true
#Sending email
$smtp.Send($msg)


That’s it. Now you can put some values from the list into the body or subject or even the receiver address (to). Enjoy it.

The article or information provided here represents completely my own personal view & thought. It is recommended to test the content or scripts of the site in the lab, before making use in the production environment & use it completely at your own risk. The articles, scripts, suggestions or tricks published on the site are provided AS-IS with no warranties or guarantees and confers no rights.

Karsten Pohnke About Karsten Pohnke
He is Consultant for SharePoint Solutions for collaboration, communication and business processes. He provides his customers applications based on standard features as well as development or combining the power of several microsoft tools like Dynamics CRM. In his free time he tries to collect tipps and worthy experience in this blog.

2 comments on “SharePoint: Send Mail by using Powershell

  1. Georg Begerow

    With this code you can even extract the configured mailserver from sharepoint, so no need to configure it twice:

    $SPGlobalAdmin = New-Object Microsoft.SharePoint.Administration.SPGlobalAdmin
    $smtpServer = $SPGlobalAdmin.OutboundSmtpServer

Submit comment

Allowed HTML tags: <a href="http://google.com">google</a> <strong>bold</strong> <em>emphasized</em> <code>code</code> <blockquote>
quote
</blockquote>

Please fill in the captcha: * Time limit is exhausted. Please reload CAPTCHA.