How to send an e-mail with attachment from SharePoint

October 2, 2007   11:52


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
.Server.Address;
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);
smtpClient.Send(mailMessage);

Tags: , , , , , , .





8 Comments »

  1. 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…

    Comment by Surya — July 17, 2008 @ 6:40 am

  2. Hi,
    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

    OR

    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.

    Thanks

    Comment by Utkarsh — November 27, 2008 @ 11:28 am

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

    Comment by Edwin Vriethoff — November 28, 2008 @ 1:12 pm

  4. Hi,

    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 ?

    Thanks,
    Doron

    Comment by Doron — July 6, 2010 @ 4:05 pm

  5. @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.

    Comment by Edwin Vriethoff — July 6, 2010 @ 4:57 pm

  6. 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 ?

    Thanks.

    Comment by Doron — July 6, 2010 @ 6:21 pm

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

    Comment by CG — August 30, 2010 @ 8:59 pm

  8. Hi,
    this has been really helpful, thanks.

    Comment by Tomek — September 13, 2011 @ 9:19 am

RSS feed for comments on this post. TrackBack URL

Leave a comment





The content expressed in this blog are those of Edwin Vriethoff and do not represent his employer's view in anyway. The contents of this blog has been carefully put together, but Edwin Vriethoff is not responsible in any way for any direct or indirect harm caused by individuals or organizations using the content of this blog in any way.