Download parts of email message

Sometimes you know you’ll receive large size emails, but you only need to access some email parts, without downloading entire email messages from IMAP server.

Mail.dll .NET IMAP library allows you to download only needed parts of the specified message.

First you need to download structure of the email message. There are two methods for that: GetBodyStructureByUID and GetBodyStructureByNumber.

You can also use GetMessageInfoByUID or GetMessageInfoByNumber methods. Both methods return more information about most common email fields, such as: subject, from, to and other headers, and also include BodyStructure in their response.

BodyStructure class contains information about plain text, html, and all attachments that were added to the message. It does not contain any data though. That’s the reason why, downloading it is very fast.

To download text parts of the email (like HTML or plain text) you can use: GetTextByUID or GetTextByNumber methods of Imap class.

To download binary attachments use GetDataByUID or GetDataByNumber methods of Imap class.

Here’s the full sample for this feature:

// C#

using(Imap imap = new Imap())
{
	imap.ConnectSSL("imap.server.com");
	imap.UseBestLogin("user", "password");

	imap.SelectInbox();
	List<long> uidList = imap.Search(Flag.Unseen);
	foreach (long uid in uidList)
	{
		// Get the structure of the email
		BodyStructure structure = imap.GetBodyStructureByUID(uid);

		// Download only text and html parts
		string text, html;

		if (structure.Text != null)
		    text = imap.GetTextByUID(structure.Text);
		if (structure.Html != null)
		    html = imap.GetTextByUID(structure.Html);

		Console.WriteLine(text);
		Console.WriteLine(html);

		// Show all attachments' names
		foreach(MimeStructure attachment in structure.Attachments)
		{
			Console.WriteLine(attachment.SafeFileName);
			// You can also download entire attachment
			byte[] bytes = imap.GetDataByUID(attachment);
		}
	}
	imap.Close();
}
' VB.NET

Using imap As New Imap()
	imap.ConnectSSL("imap.server.com")
	imap.UseBestLogin("user", "password")

	imap.SelectInbox()
	Dim uidList As List(Of Long) = imap.Search(Flag.Unseen)
	For Each uid As Long In uidList
		' Get the structure of the email
		Dim struct As BodyStructure = imap.GetBodyStructureByUID(uid)

		' Download only text and html parts
		Dim text As String, html As String

		If [structure].Text IsNot Nothing Then
		    text = imap.GetTextByUID([structure].Text)
		End If
		If [structure].Html IsNot Nothing Then
		    html = imap.GetTextByUID([structure].Html)
		End If

		Console.WriteLine(text)
		Console.WriteLine(html)

		' Show all attachments' names
		For Each attachment As MimeStructure In struct.Attachments
			Console.WriteLine(attachment.SafeFileName)
			' You can also download entire attachment
			Dim bytes As Byte() = imap.GetDataByUID(attachment)
		Next
	Next
	imap.Close()
End Using

Tags:     

Questions?

Consider using our Q&A forum for asking questions.

8 Responses to “Download parts of email message”

  1. Get email information from IMAP (fast) Says:

    […] You can read more about downloading email parts (single attachment) here […]

  2. Franz Says:

    How do you do this in POP3?

  3. Limilabs support Says:

    @Franz You can’t do it using POP3 protocol.

    POP3 is much simpler than IMAP and does not have all advanced features IMAP has.

    You can find POP3 vs IMAP comparison here: http://www.limilabs.com/blog/pop3-vs-imap

  4. Save all attachments to disk using IMAP | Blog | Limilabs Says:

    […] protocol provides very useful features in regard to working with attachments. You can download only parts of email message, which in conjunction with getting basic email information without downloading entire message can […]

  5. DEV Says:

    My company is planning to buy mail.dll from your company. We have developed a scheduler to download mail from our mail server using trail mail.dll. But in this we observed an issue, when the scheduler is trying to download mails more that 2 mb with attachment, it is unable to retrieve entire data hence throwing error.

    Error found in log:

    ERROR 2012-12-21 13:37:51,255 229359ms LogManager Error – Error in downloading email. Error: Tried to read 5754735 bytes, only 2423542 received.
    ERROR 2012-12-21 13:37:51,255 229359ms LogManager Error – Error occured hence moving on to fetch next email
    ERROR 2012-12-21 13:41:59,755 477859ms LogManager Error – Error in downloading email. Error: Invalid server response: response should have a single root object:
    (FLAGS ())
    688e7f189e854863 OK Fetch completed.

    Contact No: +919769813202

  6. Limilabs support Says:

    @DEV,

    This error usually means that the connection was interrupted.

    Please make sure that your server doesn’t have message size limit set.
    Also make sure that your antivirus software/firewall is disabled.

    As for the second error (‘single root object’) could you please send us Mail.dll logs?

  7. DEV Says:

    At present we read mails from microsoft outlook and process them. Outlook downloads all these mails hence it is clear that there are no download limit. Where I will find maill.dll logs as it does not create any logs of its own?

  8. Limilabs support Says:

    @DEV,

    Outlook is most likely not using IMAP.
    Check the antivirus software, in most cases it is responsible for premature connection termination.

    Here’s how to enable logging:
    http://www.limilabs.com/blog/logging-in-mail-dll