How can i sent mail with attachment from SharePoint?

Customer sometimes wanna send an email with their documents as attachment. If the documents are stored in a a SharePoint library, there is a small difference than if they are stored in a folder on the server. So this piece of code might help you to send the mail including the attached file from SharePoint library. The important step is to use the WebClient and Download Data method.

WebClient webClient = new WebClient();
webClient.Credentials = CredentialCache.DefaultNetworkCredentials;

fileUrl = "your SharePoint File Path absolute";
byte[] data = webClient.DownloadData(fileUrl);
MemoryStream fs = new MemoryStream(data);

fileName = "Your filename.xx";

attachment = new Attachment(fs, fileName);

Hope it helps you.

The article or information provided here represents completely my own personal view & thought. It is recommended to test the content or scripts of the site in the lab, before making use in the production environment & use it completely at your own risk. The articles, scripts, suggestions or tricks published on the site are provided AS-IS with no warranties or guarantees and confers no rights.

Karsten Schneider
About Karsten Schneider 302 Articles
He is Consultant for SharePoint Solutions for collaboration, communication and business processes. He provides his customers applications based on standard features as well as development or combining the power of several microsoft tools like Dynamics CRM. In his free time he tries to collect tipps and worthy experience in this blog.

Be the first to comment

Leave a Reply

Your email address will not be published.