Question

The library ipworks provides some methods fetching the textmessage.

In the documentation I don't find how to read the attachements via ipworks library using imaps.

Was it helpful?

Solution

I work for /n software and we came across your question here. Based on the tags for this question it looks like you are probably using our .NET Edition and C# code so I will use C# code for my example.

Retrieving attachments from the Imaps component requires that you use the MessageParts property. This property contains a collection of the individual MIME parts from the downloaded email message. Typically the first two parts are going to be the HTML body of the email message (if applicable) and the plain-text body of the email message. Any attachments will be in the remaining MIME parts. You can use some code similar to the following to retrieve the attachments from the selected email message:

Imaps imap = new Imaps();

imap.OnSSLServerAuthentication += new Imaps.OnSSLServerAuthenticationHandler(delegate(object sender, ImapsSSLServerAuthenticationEventArgs e)
{
  //Since this is a test, just accept any certificate presented.
  e.Accept = true;
});

imap.MailServer = "your.mailserver.com";
imap.User = "user";
imap.Password = "password";
imap.Connect();
imap.Mailbox = "INBOX";
imap.SelectMailbox();
imap.MessageSet = "X"; //Replace "X" with the message number/id for which you wish to retrieve attachments.
imap.FetchMessageInfo();

for (int i = 0; i < imap.MessageParts.Count; i++)
{
  if (imap.MessageParts[i].Filename != "")
  {
    //The MessagePart Filename is not an empty-string so this is an attachment

    //Set LocalFile to the destination, in this case we are saving the attachment
    //in the C:\Test folder with its original filename.
    //Note: If LocalFile is set to an empty-string the attachment will be available
    //      through the MessageText property.
    imap.LocalFile = "C:\\Test\\" + imap.MessageParts[i].Filename;

    //Retrieve the actual attachment and save it to the location specified in LocalFile.
    imap.FetchMessagePart(imap.MessageParts[i].Id);
  }
}

imap.Disconnect();

Note that it is also possible that the individual MIME parts will be base64 encoded. If you wish to have our component automatically decode these parts then you will need to set the "AutoDecodeParts" property to "true". This should be done prior to calling the FetchMessageInfo method. Please see the example below:

imap.AutoDecodeParts = true;
imap.FetchMessageInfo();

There is also a possibility that the email message will contain nested MIME structures. This is a more complicated case which requires a recursive approach to deconstruct the nested MIME structures. Our MIME component (available in our IP*Works and IP*Works S/MIME products) can be very helpful for this.

If you need an example in another language, an example for handling nested MIME structures, or if you have any other questions, please feel free to contact us at support@nsoftware.com.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top