Access message attachments
All the message attachments are stored in MailMessage.Attachments collection. This collection also lists the embedded images and other files which are contained in the message.
Indexing of attachments in the collection is zero based. For example, the code below prints the name of the first attachment:
Console.WriteLine("Attachment name is " + msg.Attachments[0].Name);
Console.WriteLine("Attachment name is " & msg.Attachments(0).Name)
To access attachments, you need to download the entire message from the server (not just headers).
You can check whether the message has attachments as follows:
Pop3 pop = new Pop3();
// Download entire message
MailMessage msg = pop.DownloadEntireMessage(1);
if (msg.HasAttachments)
{
// The message has at least one attachment
}
Dim pop As New Pop3()
' Download entire message
Dim msg As MailMessage = pop.DownloadEntireMessage(1)
if (msg.HasAttachments) Then
' The message has at least one attachment
End If
In some cases, you can use MailMessage.HasAttachments property even if only message header has been downloaded. See Determine if the message has attachments without downloading the entire message for details.