Important alert: (current site time 7/16/2013 7:31:34 AM EDT)
 

article

Access & Read Email Messages from MS SharePoint Document Library

Email
Submitted on: 2/21/2012 12:00:58 PM
By: aspose_seo 
Level: Intermediate
User Rating: By 1 Users
Compatibility: C#, VB.NET
Views: 2711
 
     This technical tip shows how to read email messages from Microsoft SharePoint document library. For accessing the files from SharePoint document library, SharePoint SDK must be installed on the system, which provides the necessary API for logging in and accessing files from the document library.

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				Aspose.Email for .NET can help .NET applications to read email messages from Microsoft SharePoint document library. For accessing the files from SharePoint document library, SharePoint SDK must be installed on the system, which provides the necessary API for logging in and accessing files from the document library. In the below code snippet, we will assume that an Outlook Message file (.msg) is already stored in the SharedDocument folder of SharePoint Document Library. We will utilize SharePoint SDK to get the message file in a stream and pass this stream to an instance of Aspose.Email's MailMessage class. MailMessage class will load the stream and parse the Outlook Message file. After that you can easily access the properties of MailMessage class e.g. Subject , TextBody , HtmlBody| etc and utilize that information in your Visual Studio project.
[C#]
// private members
private SPSite site = null;
private SPWeb web = null;
/// 
/// This Method is called throug Delegate elevatedGetSite which is definend in button2_Click
/// 
private void EstablishSharepoint()
{
site = new SPSite("http://localhost/Site1");
web = site.OpenWeb();
}
/// 
/// click event of a windows form button
/// 
/// 
/// 
private void button2_Click(object sender, EventArgs e)
{
SPSecurity.CodeToRunElevated elevatedGetSite = new SPSecurity.CodeToRunElevated(EstablishSharepoint);
SPSecurity.RunWithElevatedPrivileges(elevatedGetSite);
// path to the msg file stored in Shared Documents folder
SPFile msgFile = web.GetFile("Shared Documents/Test.msg");
// Read the file into a Memory Stream.
MemoryStream fileStream = new MemoryStream();
byte[] currentFileContent = msgFile.OpenBinary();
fileStream.Write(currentFileContent, 0, currentFileContent.Length);
fileStream.Position = 0;
// Create an instance of MailMessage class
// and pass the memory stream of .msg file to it
MailMessage msg = MailMessage.Load(fileStream, MessageFormat.Msg);
fileStream.Close();
fileStream.Dispose();
// access public properties of MailMessage class
Console.WriteLine("Subject: " + msg.Subject);
Console.WriteLine("From: " + msg.From.ToString());
Console.WriteLine("Text Body: " + msg.TextBody);
}
[VB.NET] 
Private site As SPSite = Nothing
Private web As SPWeb = Nothing
''' 
''' This Method is called throug Delegate elevatedGetSite which is definend in button2_Click
''' 
Private Sub EstablishSharepoint()
site = New SPSite("http://localhost/Site1")
web = site.OpenWeb()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim elevatedGetSite As New SPSecurity.CodeToRunElevated(AddressOf EstablishSharepoint)
SPSecurity.RunWithElevatedPrivileges(elevatedGetSite)
' path to the msg file stored in Shared Documents folder
Dim msgFile As SPFile = web.GetFile("Shared Documents/Test.msg")
' Read the file into a Memory Stream.
Dim fileStream As New MemoryStream()
Dim currentFileContent As Byte() = msgFile.OpenBinary()
fileStream.Write(currentFileContent, 0, currentFileContent.Length)
fileStream.Position = 0
' Create an instance of MailMessage class
' and pass the memory stream of .msg file to it
Dim msg As MailMessage = MailMessage.Load(fileStream, MessageFormat.Msg)
fileStream.Close()
fileStream.Dispose()
' access public properties of MailMessage class
Console.WriteLine("Subject: " & msg.Subject)
Console.WriteLine("From: " & msg.From.ToString())
Console.WriteLine("Text Body: " & msg.TextBody)
End Sub 
Overview: Aspose.Email for .NET
Aspose.Email for .NET is a set of components allowing developers to easily implement email functionality within their ASP.NET web applications, web services & Windows applications. It Supports Outlook PST, EML, MSG & MHT formats. It allows developers to work with SMTP, POP3, FTP & MS Exchange servers. It supports mail merge, iCalendar, customized header & body, header information, embedded files, Twitter & many more. It makes it easy to work with HTML or plain text emails & their attachments.
More about Aspose.Email for Java 
Homepage of Aspose.Email for .NET
Download Aspose.Email for .NET
More Technical Tips by Aspose.Email for .NET 
Contact Information
Suite 119, 272 Victoria Avenue
Chatswood, NSW, 2067
Australia
Aspose - Your File Format Experts 
sales@aspose.com
Phone: 888.277.6734
Fax: 866.810.9465


Other 16 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.