Back to Tutorials list

Part 3 - Retrieve A Few Body Lines Along With The Headers

Summary: Demonstrates how to get a few lines of the body in addition to message headers when retrieving the list of messages from the POP3 server.

In some cases you may need to get somewhat more than just message headers. The contents of "From:" and "Subject:" fields do not often answer the question "what is the message about?"

Fortunately, the DownloadMessageHeaders method supports the optional parameter bodyLineCount which specifies how many lines of the body to retrieve in addition to the headers.

The larger bodyLineCount is, the bigger piece of the body is returned, and the connection produces lager traffic. If the bodyLineCount is larger than the actual number of body lines in a message, the entire message is returned. As a rule, 20-40 lines of the body describe the message quite enough.

Note: Avoid setting the bodyLineCount to very small values (less than 20). Many e-mail messages have some special (non-textual) information in the first 5-10 lines of the body while the actual body text starts after these lines.

Note: If only the message header was received from the mail server (not the entire message), MailMessage.Size property will contain the size of this header, not of the entire message. To get the size of the entire message, use SizeOnServer property.

Detailed information about parsing messages with incomplete body

For MailBee parser (the module which converts messages into MailMessage objects), there is no difference between entire and incomplete messages. The available content is fully parsed anyway. Thus, if the retrieved piece of the message contains any attachments, they will be parsed and added to MailMessage.Attachments but the size and the contents of certain attachments will be incorrect if they have not been downloaded completely. Those attachments which did not fall into the scope of the retrieved piece of the body section will not be appended to MailMessage.Attachments at all. In other words, MailBee puts into MailMessage object all the information available in the incomplete message, even if t he information on some part of the message is incomplete.

If you have one attachment (named "abc.gif", 1234 bytes in size) in the downloaded incomplete message, you can be sure that the full message contains at least one attachment (named "abc.gif") and the size of this attachment is not less than 1234 bytes.

If you have two attachments in the downloaded incomplete message, this means that the first attachment has been downloaded completely and its size and contents correspond to the real values. This is due to the fact that attachments are located in the message one after another, thus each new object guarantees that the previous object data was received completely (e.g. if you have 6 attachments in the incomplete message, this means at least 5 of them have been downloaded completely).

Downloading text body

MailMessage object has two properties which allow a user to get or set the plain-text body or HTML body of the message. To get the plain-text of the message, use the BodyPlainText property. If there is no plain-text body in the message, you will get an empty string by default. To get HTML body, use BodyHtmlText property (you will get an empty string if the message does not have an HTML body).

If your application can display only plain-text displaying, it's possible to convert the HTML-formatted message into the plain text. MailMessage class has Parser property which sets the options which affect how the MailMessage object is being parsed. This property allows you to perform the following operations: automatic generating of plain-text and HTML bodies, saving of the HTML body into a file, etc.

With regard to incomplete messages, conversion of plain-text into HTML or vice versa can be useful even for messages having both versions of the bodies. This is because the incomplete message might have the plain-text body partially available while the HTML body (which usually follows the plain-text body in the message source) might not fall into the downloaded range of the message source. Another case is when the message is HTML-only but you need to display its preview as plain-text (for instance, in a tool-tip).

Sample code description

The sample code below gets the header and the 25 first body lines of the message source of each message and writes the first line of the plain-text body of each message into the console.

Before using MailBee.NET Objects, make sure the correct license key is specified (see "Sales, Licensing, and Support" and "Using MailBee.NET Objects in Your Projects" sections).

Code example:

[C#]
Pop3 pop = new Pop3();

//Connect to POP3 server
pop.Connect("mail.domain.com");
pop.Login("jdoe", "secret");

// Download all messages incompletely
MailMessageCollection msgs = pop.DownloadMessageHeaders(1, -1, 25);

foreach (MailMessage msg in msgs)
{
        msg.Parser.HtmlToPlainMode = HtmlToPlainAutoConvert.IfNoPlain;
        int n = msg.BodyPlainText.IndexOf('\n');
        if (n > 0)
        {  
                Console.WriteLine("Body Preview: " + strLine.Substring(0, n));
        }
        else
        {
                Console.WriteLine("Body Preview: " + msg.BodyPlainText;
        }
}

// Disconnect from POP3 server
pop.Disconnect();
[VB.NET]
Dim pop As New Pop3

' Connect to POP3 server
pop.Connect("mail.domain.com")
pop.Login("jdoe", "secret")

' Download all messages incompletely
Dim msgs As MailMessageCollection = pop.DownloadMessageHeaders(1, -1, 25)

Dim n As Integer
Dim msg As MailMessage

For Each msg In msgs
        msg.Parser.HtmlToPlainMode = HtmlToPlainAutoConvert.IfNoPlain
        Dim strLine As String = msg.BodyPlainText
        n = msg.BodyPlainText.IndexOf(vbLf)
        If n > 0 Then
                Console.WriteLine("Body Preview: " + strLine.Substring(0, n))
        Else
                Console.WriteLine("Body Preview: " + msg.BodyPlainText)
        End If
Next

' Disconnect from POP3 server
pop.Disconnect()
Back to Tutorials list