How to read or download the attachments from mail box using aspose IMAP api

Hey,

I have gone through the aspose documentation and it worked for me on some basic examples using IMAP.

I could able to send the mail message to mailbox and now i am looking for the example to read or download the attachment from mail box using aspose IMAP api.

Could anyone suggest me on the above query.

Thanks,
Dhivakar.T

@divat,

Thank you for contacting Aspose support team.

You may please give a try to the following sample code and share the feedback.

ImapClient client = new ImapClient("mail.domain.com", 143);
client.SecurityOptions = Aspose.Email.Clients.SecurityOptions.Auto;
client.Username = "username";
client.Password = "password";
client.SelectFolder(ImapFolderInfo.InBox);
ImapMessageInfoCollection list = client.ListMessages();
MailMessage message;
for (int i = 1; i <= list.Count; i++)
{
    message = client.FetchMessage(i);
    foreach (Attachment attachment in message.Attachments)
    {
        attachment.Save(attachment.Name);
    }
}

@kashif.iqbal Works. But it’s not reading all the attachment from mailbox and i would like to know how to save the file attachment in desired file path.

One more thing while i’m copying the file from source to destination i’m getting FileAlreadyExistsException.

Here is the source code i have used to save the fetched attachment.

ImapClient client = new ImapClient();
client.setHost(“[host.domain.com](http://host.domain.com/)”);
client.setPort(993);
client.setUsername(“username”);
client.setPassword(“password”);
client.setSecurityOptions(SecurityOptions.Auto);
client.selectFolder(ImapFolderInfo.IN_BOX);

	ImapMessageInfoCollection list = client.listMessages();
	
	System.out.println(list.size());
	
	MailMessage message;
	
	for(int i = 1; i <= list.size(); i++){
		message = client.fetchMessage(i);
		for (Attachment a : message.getAttachments()) {
			File f1 = new File(a.getName());
			//System.out.println(a.getName());
			
			if (!f1.exists()) {
				String filePath = f1.getAbsolutePath();
				filePath = filePath.replaceAll("[\\\\/:*?\"<>|]", "");
				
				System.out.println("filePath " + filePath);
				
				InputStream is = a.getContentStream();

				final Path destination = Paths.get(filePath);
				try (final InputStream in = is;) {
					Files.copy(in, destination);
				}catch(FileAlreadyExistsException e) {
				    //destination file already exists
					System.out.println("File already exists exception...");
				} catch (IOException e) {
				    //something else went wrong
				    e.printStackTrace();
				}
			}
                       }
                   } 
              }

@divat,

Thank you for providing the feedback.

You may please use following sample code to fetch attachment only.

ImapClient client = new ImapClient("mail.domain.com", 143);
client.setSecurityOptions(SecurityOptions.Auto);
client.setUsername("username");
client.setPassword("password");
client.selectFolder(ImapFolderInfo.IN_BOX);
ImapMessageInfoCollection list = client.listMessages();
for(ImapMessageInfo info : list)
{
    Attachment att = client.fetchAttachment(info.getSequenceNumber(), "RequiredAttachmentName");
}

Regarding the exception “file already exists”, if multiple attachment names are same, then this exception will be raised. You may please use some GUID or timestamp etc. along with the attachment name to make it unique before saving on disc.

@kashif.iqbal
Thanks for your support. I can able to download attachment type like .jpg,.doc, etc But I am unable to download .msg, .eml,.pst, .ost. oht attachments.

Could you please help me out on this.

@divat,

Thank you for providing feedback.

You may please check your sample MSG file using some mail client or message viewer and check the attachments name. If these are proper attachments and contain valid names, use those names to fetch attachments. We have tried few sample MSG files and issue is not re-produced. If still issue is there, please send us the sample message file which contains such attachments which are not fetched using Aspose.Email. We will analyze the message and provide assistance accordingly.