How to send an e-mail with attachment from SharePoint

Oct. 2nd | Posted by 11 comments

How to send an e-mail with attachment from SharePointOne of the ways to send an e-mail from SharePoint as developer, is to make use of the “SPUtility.SendEmail” classes. But unfortunately I did not find any possibility to include an attachment with the help of these classes.

To be able to send attachment I decided to make use of the normal “System.Net.Mail” classes to send a message. Instead of hard coding the SMTP information into the code, I wanted to use the SMTP settings that are configured with the SharePoint Central Administration. These settings are found in the “SPAdministrationWebApplication” class.

Because SharePoint stores it’s files into the content database, we must do a http request to retrieve the attachment. Using the “System.Net.WebClient” is one way to do it and one of the advantages is that you can supply it with the credentials of the user that is trying to send the mail.

You will receive an unauthorized message if you don’t supply a correct set of credentials. It’s probably not a good idea to hardcode the credentials as it will be possible for users to get access to files they are not supposed to. Supplying the WebClient with the “CredentialCache.DefaultNetworkCredentials” worked fine for me on a standard SharePoint environment.

Below is a brief example on how you can send the mail with attachment from code. If you prefer to use the user’s e-mail address instead of the configured SMTP user you can retrieve it with SPControl.GetContextWeb(Context).CurrentUser.Email.

//Get the Sharepoint SMTP information from the SPAdministrationWebApplication
string smtpServer = SPAdministrationWebApplication.Local.OutboundMailServiceInstance
string smtpFrom = SPAdministrationWebApplication.Local.OutboundMailSenderAddress;

//Create the mail message and supply it with from and to info
MailMessage mailMessage = new MailMessage(smtpFrom, insert_receiver);

//Set the subject and body of the message
mailMessage.Subject = insert_subject;
mailMessage.Body = insert_body;

//Download the content of the file with a WebClient
WebClient webClient = new WebClient();

//Supply the WebClient with the network credentials of our user
webClient.Credentials = CredentialCache.DefaultNetworkCredentials;

//Download the byte array of the file
byte[] data = webClient.DownloadData(insert_ attachment_url);

//Dump the byte array in a memory stream because
//we can write it to our attachment
MemoryStream memoryStreamOfFile = new MemoryStream(data);

//Add the attachment
mailMessage.Attachments.Add(new System.Net.Mail.Attachment(memoryStreamOfFile, insert_filename_attachment, insert_content_type));

//Create the SMTP client object and send the message
SmtpClient smtpClient = new SmtpClient(smtpServer);

11 comments Add a comment

  1. Surya

    That was an Excellent information.but instead of using memorystream or webclient.. We can directly use

    smtpclient.Credentials = new NetworkCredential();
    smtpclient.Credentials = CredentialCache.DefaultNetworkCredentials;

    For the file attachment you can read from SPFile property of OpenBinaryStream() function..

    But thanks for the above information…

  2. Utkarsh

    I am a newbie in sharepoint and infopath. I have a question,

    We’ll have to implement the above code in the

    public void CTRL4_5_Clicked(object sender, ClickedEventArgs e){}

    for the submit button in infopath form


    We’ll have to make a custom web part for this and call that webpart after SUBMITTING the FORM.

    I have tried the 1st option, the form gave an error that it cannot load Microsoft.Sharepoint assembly.

    Help will be appreciated.


  3. Edwin Vriethoff

    Hi Utkarsh,

    Normally this should work, but it will only work on a computer that has SharePoint installed. This basically means you can only use it with InfoPath Forms Services.

    If you are using a windows client InfoPath form, the Sharepoint DLL will not be found. In that case you should, for example, create a webservice to send the e-mail.

  4. Doron


    I am very newbie at sharepoint but I need to add this option to our sharepoint.

    Is it possible to direct where I need to add those lines ?


    • Edwin Vriethoff

      @Doron, you can use the code everywhere inside a class which references the SharePoint library. (Webparts, Sharepoint webpages, Jobs etc). Just search the net for code examples about how to write those.

  5. Doron

    Hi Edwin,

    Thank you for your answer but I didn`t understand it.

    I work with C# and I saw some examples but I don`t know how to develop outside the server.

    I only want to understand where I need to write the code you put above ?
    where can I find my class ?


  6. CG

    I would like to see the attached file in my lotus notes client, any pointers?

  7. Tomek

    this has been really helpful, thanks.

  8. Daniel

    Thank you! Great example!

  9. Kelly

    Excellent walk-through, very helpful for working out how to send an e-mail on SharePoint from lists!

  10. Cheryl

    Thank you so much!! This helped solve a problem I have been working on for two days!!

Add a Comment

reset all fields


I work at

I work at

SharePoint related

Flickr stream