Click here to Skip to main content
15,886,664 members
Articles / Productivity Apps and Services / Sharepoint / SharePoint 2010
Tip/Trick

How to send email in SharePoint?

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
6 Aug 2013CPOL 47.9K   3   4
How to send email in SharePoint.

Background

I am not sure why but Microsoft does it every time. They give you a piece of functionality but leaves you struggling with a certain and so called 'known' limitation.

One such limitation is sending emails when using SharePoint. SharePoint provides an API called 'SPUtility.SendEmail' to send emails. But the implementation of this API seems to be incomplete. If you notice the method, it is impossible to send an email with an attachment or to change the from address of the sender (from address can be added as a header, thanks to Giacomo for pointing out). 

Image 1

Using the code 

To make thing simpler, you can use the following class to extend the functionality and use the good old MailMessage object to send email (without any extra infrastructure configuration). 

C#
public class SMTPHelper
{
    private readonly string _specifiedPickupDirectory = string.Empty;
    private readonly SmtpClient _smtpClient;

    public SMTPHelper(string specifiedPickupDirectory)
    {
        _specifiedPickupDirectory = specifiedPickupDirectory;

        _smtpClient = new SmtpClient();
        if (string.IsNullOrEmpty(_specifiedPickupDirectory))
        {
            //Get the Sharepoint SMTP information from the SPAdministrationWebApplication
            var host = SPAdministrationWebApplication.Local.OutboundMailServiceInstance.Server.Address;
            _smtpClient.Host = host;
        }
        else
        {
            _smtpClient.PickupDirectoryLocation = _specifiedPickupDirectory;
            _smtpClient.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
        }
    }

    public void SendEmail(MailMessage mailMessage)
    {
        _smtpClient.Send(mailMessage);
    }
}

The above code basically make use of the OutboundMailServiceInstance to retrive the SMTP server address configured within SharePoint. Generally, you can configure this address in Central Administration -> System Settings -> Configure outgoing e-mail settings.

Image 2

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Netherlands Netherlands

Read my personal blog at www.manasbhardwaj.net.


Comments and Discussions

 
AnswerMessage Closed Pin
24-Mar-22 13:47
Oskar Finch24-Mar-22 13:47 
QuestionThanks, but missing a piece Pin
B. Clay Shannon28-Jul-15 7:47
professionalB. Clay Shannon28-Jul-15 7:47 
QuestionSPUtility.SendEmail() Pin
Giacomo Pozzoni6-Aug-13 6:35
Giacomo Pozzoni6-Aug-13 6:35 
You can set the "from" parameter in SPUtility.SendEmail() using the overload http://msdn.microsoft.com/en-us/library/ms460489(v=office.14).aspx[^]
C#
public static bool SendEmail(
	SPWeb web,
	StringDictionary messageHeaders,
	string messageBody
)


In the Community Addiction of there's an example:
C#
StringDictionary headers = new StringDictionary();
headers.add("to",someone@somewhere.com);
headers.add("cc",someone.else@somewhere.com);
headers.add("bcc",somebody@somewhere.com);
headers.add("from",sender@somewhere.com);
headers.add("subject","SPUtility.SendEmail with headers test");

AnswerRe: SPUtility.SendEmail() Pin
Manas Bhardwaj6-Aug-13 7:40
professionalManas Bhardwaj6-Aug-13 7:40 
AnswerRe: SPUtility.SendEmail() Pin
Manas Bhardwaj6-Aug-13 7:44
professionalManas Bhardwaj6-Aug-13 7:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.