Back to Tutorials list

Part 2 - Detecting Attachments

Summary: Highlights detecting whether a message has any attachments when only message headers are available.

In the majority of all e-mail applications it is common to show a paper-clip for messages with attachments.

The POP3 protocol (unlike IMAP4) does not provide the mechanism to preview attachments. Fortunately, most of the messages with attachments have some indirect information about attachments in their headers. This allows MailBee to detect attachments in the message even if only the message header is available.

MailMessage.HasAttachments property returns true if the message has one or more attachments. If only message headers (not the entire message) were received from the mail server, this property may not be 100% accurate. The developer should download the entire message to be sure if it really has any attachments. This is because the message header contains only indirect information about attachments which might be wrong. Also, it's not possible to determine the number of attachments, their sizes or filenames.

Sample code description

For each message in the mailbox the sample code below reports whether attachments are present.

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 headers for all messages
MailMessageCollection msgs = pop.DownloadMessageHeaders();

// Check each message in mailbox
foreach (MailMessage msg in msgs)
{
if (msg.HasAttachments)
  Console.WriteLine ("Message #" + msg.IndexOnServer + " has attachment(s)");
else
  Console.WriteLine ("Message #" + msg.IndexOnServer + " has no attachment(s)");
}

// 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 headers for all messages
Dim msgs As MailMessageCollection = pop.DownloadMessageHeaders()

' Check each message in mailbox
Dim msg As MailMessage
For Each msg In msgs
        If msg.HasAttachments Then
                Console.WriteLine("Message #" & msg.IndexOnServer & " has attachment(s)")
        Else
                Console.WriteLine("Message #" & msg.IndexOnServer & " has no attachment(s)")
        End If
Next

pop.Disconnect()
Back to Tutorials list