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
Sending email programmatically in SharePoint - 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

2013-07-05

If you ever need to send email from a custom SharePoint application, you probably want to utilize the capabilities already provided by SharePoint. By doing this, you keep your email settings consistent and easy to manage.

Make sure your outgoing mail settings are configured.

First you will need to be sure that outgoing mail settings are configured for SharePoint. After that you can easily send email from your custom SharePoint application using SharePoint’s native mail capabilities. You can do this in Central Administration. SharePoint alerts and other email notifications also use these settings, so unless you have a fresh SharePoint installation, this is most likely already configured.

Verify that outgoing email settings are configured.

Before attempting to send an email from your custom code, you can easily check to be sure that the outgoing email settings are configured. If it is not configured you can handle this properly and control the message shown to the user.

if(!SPUtility.IsEmailServerSet(SPContext.Current.Web)) throw new SPException(“Outgoing E-Mail Settings are not configured!”);

 

SPUtility.SendEmail

Perhaps the easiest way to send an email using SharePoint settings is by using the SPUtility.SendEmail method. Add a reference to the Microsoft.SharePoint.Utilities namespace and your ready to go. This method will return a Boolean indicating if the email was successfully sent.

If trying to use SPUtility.SendEmail from a timer job or workflow you may have issues. The reason is that the method relies on SPContext which doesn’t exist in timer jobs and workflows. In this case you may want to use the System.Net.Mail option for sending email from SharePoint.

System.Net.Mail

The System.Net.Mail namespace provides an easy way to send email using SMTP. Fortunately, if SharePoint Outgoing Email Settings are configured, we already know the SMTP settings, we just need to know how to get them into our code.

string replyTo = SPContext.Current.Site.WebApplication.OutboundMailReplyToAddress;
string smtpAddress = SPContext.Current.Site.WebApplication.OutboundMailServiceInstance.Server.Address;

MailMessage message = new MailMessage(replyTo, "emailto@someone.com", "Subject of the email", "Body of the email");

message.IsBodyHtml = true;

SmtpClient smtp = new SmtpClient(smtpAddress);smtp.Send(message);

The SmtpClient.Send method will throw an exception if there is an issue sending the email unlike SPUtility.SendEmail which returns a Boolean.

Keep in mind that the email may be sent successfully from SharePoint but there is still a chance of it getting caught up somewhere else on it’s way to the destination.

If you receive errors when sending email using these methods but everything seems to be configured correctly, check to see if your network antivirus or firewall may be blocking the application from sending the email. (I learned this from experience!)

(Visited 660 times, 1 visits today)

About the author 

Eric Gregorich

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.