Saturday, July 25, 2009

How to Send Email from SharePoint object model by accessing Email Configuration from Central Admin

Here is example code for sending email from SharePoint object model, main part of code would be “SPAdministrationWebApplication.Local.OutboundMailServiceInstance.Server.Address”, it will return mail server address which is assigned from SharePoint central administration site as per web application

Sample Code:

private void SendEmail()
SmtpClient client = LoadSmtpInformation();
catch (Exception Ex)

private SmtpClient LoadSmtpInformation()

string smtpServer = SPAdministrationWebApplication.Local.OutboundMailServiceInstance.Server.Address;
SmtpClient client = new SmtpClient(smtpServer);
client.UseDefaultCredentials = true;
return client;

private MailMessage BuildMailMessage()

MailMessage message = new MailMessage();

message.From = new MailAddress(SPContext.Current.Web.CurrentUser.Email);
message.To.Add(new MailAddress(User.Email));
message.IsBodyHtml = true;
message.Body = “Email Message”;

message.Subject = "Subject of Email";
return message;


1 comment:

  1. Thank you very much disha for sharing such a useful and handy code, it really helped me.

    Actually I was facing challenges while sending email from 'visual studio workflow' send email activity, somehow visual studio workflow is ignoring my 'FROM' field value and as per my requirements I need to send email on behalf of different sender name, so finally I thought to use traditional object model classes to send email. That’s why I have found your blog.

    Sanket Shah