Home > Code > C# > How to Read Email Messages from Microsoft SharePoint Document Library

How to Read Email Messages from Microsoft SharePoint Document Library

by sher azam   on Dec 17, 2013   Category: C#   |  Views: 2036    |  Points: 25   |  Gold 


This technical tip explains how to read email messages from a Microsoft SharePoint document library. To access files in a SharePoint document library, the SharePoint SDK must be installed on the system. The SDK provides the API necessary to log in and access files from the document library. The programming samples below assume that a Microsoft Outlook Message file (.msg) is already stored in the SharedDocument folder of the SharePoint Document Library. The SharePoint SDK is used to get the message file into a stream and then pass this stream to an instance of Aspose.Email's MailMessage class. The MailMessage class loads the stream and parses the Outlook message file. After that, access the MailMessage class' properties, for example, subject, text body, HTML body etc. to use the information in a Visual Studio project.

Reading Email Messages from SharePoint

//[C#]

// Private members
private SPSite site = null;
private SPWeb web = null;

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

/// <summary>
/// Click the 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 the MailMessage class
// and pass the memory stream of the MSG file to it
MailMessage msg = MailMessage.Load(fileStream, MessageFormat.Msg);
fileStream.Close();
fileStream.Dispose();

// Access the MailMessage class' public properties
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 through the 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)

' The path to the MSG file stored in the 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 the MailMessage class
' and pass the memory stream of the MSG file to it
Dim msg As MailMessage = MailMessage.Load(fileStream, MessageFormat.Msg)
fileStream.Close()
fileStream.Dispose()

' Access the MailMessage class' public properties
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/.net/email-component.aspx

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


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