Click here to Skip to main content
13,591,259 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

35.4K views
3 bookmarked
Posted 6 Aug 2013
Licenced CPOL

How to send email in SharePoint?

, 6 Aug 2013
Rate this:
Please Sign up or sign in to vote.
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). 

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). 

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.

License

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

Share

About the Author

Manas Bhardwaj
Architect
Netherlands Netherlands

Read my personal blog at www.manasbhardwaj.net.


You may also be interested in...

Comments and Discussions

 
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
memberGiacomo Pozzoni6-Aug-13 6:35 
AnswerRe: SPUtility.SendEmail() Pin
Manas Bhardwaj6-Aug-13 7:40
mvpManas Bhardwaj6-Aug-13 7:40 
AnswerRe: SPUtility.SendEmail() Pin
Manas Bhardwaj6-Aug-13 7:44
mvpManas 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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.180618.1 | Last Updated 6 Aug 2013
Article Copyright 2013 by Manas Bhardwaj
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid