One 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.
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);
July 17, 2008 at 6:40 am
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…
November 27, 2008 at 11:28 am
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
November 28, 2008 at 1:12 pm
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.
July 6, 2010 at 4:05 pm
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
July 6, 2010 at 4:57 pm
@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.
July 6, 2010 at 6:21 pm
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.
August 30, 2010 at 8:59 pm
I would like to see the attached file in my lotus notes client, any pointers?
September 13, 2011 at 9:19 am
Hi,
this has been really helpful, thanks.
August 21, 2012 at 4:06 pm
Thank you! Great example!
April 25, 2013 at 3:32 pm
Excellent walk-through, very helpful for working out how to send an e-mail on SharePoint from lists!
July 25, 2014 at 2:54 pm
Thank you so much!! This helped solve a problem I have been working on for two days!!