Home > Code > C# > Access & Read Email Messages from MS SharePoint Document Library

Access & Read Email Messages from MS SharePoint Document Library

by sher azam   on Feb 20, 2012   Category: C#   |  Views: 1751    |  Points: 25   |  Gold 


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

/// <summary>
/// This Method is called throug Delegate elevatedGetSite which is definend in button2_Click
/// </summary>
private void EstablishSharepoint()
{
site = new SPSite("http://localhost/Site1");
web = site.OpenWeb();
}

/// <summary>
/// click event of a windows form button
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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 members
Private site As SPSite = Nothing
Private web As SPWeb = Nothing

''' <summary>
''' This Method is called throug Delegate elevatedGetSite which is definend in button2_Click
''' </summary>
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 .NET

- Homepage of Aspose.Email for .NET: http://www.aspose.com/categories/.net-components/aspose.email-for-.net/default.aspx

- Download Aspose.Email for .NET: http://www.aspose.com/community/files/51/.net-components/aspose.email-for-.net/default.aspx

- More Technical Tips by Aspose.Email for .NET: http://docs.aspose.com/display/emailnet/knowledge+base

Contact Information
Aspose Pty Ltd, Suite 163,
79 Longueville Road
Lane Cove, NSW, 2066
Australia
http://www.aspose.com/
sales@aspose.com
Phone: 888.277.6734
Fax: 866.810.9465



Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts