DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

David has posted 85 posts at DZone. View Full User Profile

How to Read Email Messages from Microsoft SharePoint Document Library

12.17.2013
| 2425 views |
  • submit to reddit
//[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

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.