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.
@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();
}
}
}
}
}
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.
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.