We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Fetch Email using IMAP

Hi Guys


I am new to Aspose, I want to fetch emails and save them without any attachments? How do I go about it.

I am able to fetch email but they are coming with the attachments, I want to do it separately.

Hi Brian,


Thank you for using Aspose.Email.

You can use the following code sample that ignores fetching the message attachments. Setting the second flag of FetchMessage to “true” will ignore fetching message attachments. Please let us know if we can be of any additional help to you in this regard.

Sample Code:

ImapClient client = new
ImapClient();<o:p></o:p>

client.Host = "exchange.domain.com";

client.Username = "username";

client.Password = "password";

client.SelectFolder("Inbox");

ImapMessageInfoCollection coll = client.ListMessages();

foreach (ImapMessageInfo msgInfo in coll)

{

MailMessage msg = client.FetchMessage(msgInfo.SequenceNumber, true);

Console.WriteLine(msg.Attachments.Count);

msg.Save("a.eml", MailMessageSaveType.EmlFormat);

}

Hi Kashif


I got it working, I have another question, I need to save the attachments of the email in a on my hard drive but skip the signatures, eg company logo or etc.

Regards
Brian Honde.

Hi Brian,


You can save the regular attachments from the fetched message as shown in the following code sample. Please note that signatures such as company logo etc. embedded in a message’s body are treated as LinkedResources whihc are different than regular attachments. Let us know if we can be of any additional help to you in this regard.

Sample Code:

ImapClient client = GetAsposeImapClient1();<o:p></o:p>

client.SelectFolder("Inbox");

ImapMessageInfoCollection coll = client.ListMessages();

foreach (ImapMessageInfo msgInfo in coll)

{

MailMessage msg = client.FetchMessage(msgInfo.SequenceNumber);

Console.WriteLine(msg.Attachments.Count);

//Save message attachments to disc

foreach (Attachment att in msg.Attachments)

att.Save(att.Name);

}

Hi


Your solution worked. I have one more question, I’m using exchange and when I download attachments I also want to skip the Anti Virus scan reports that get attached to the email in these formats ATxxxx.txt or ATxxxxx.html.

How can I get rid of them?

Regards
Brian

Hi Brian,


I would like to share that Aspose.Email has features to retrieve the message attachments info only rather than the complete attachments to achieve the performance and reduce load on the system. You may please try the following sample code which retrieves the attachment info initially and then based upon particular criteria, the attachment can be downloaded.

// Connect to Exchange Server
IEWSClient client = GetAsposeEWSClient();

// Get list of messages
ExchangeMessageInfoCollection messagesInfo = client.ListMessages(client.MailboxInfo.InboxUri,10, ExchangeListMessagesOptions.FetchAttachmentInformation);
Console.WriteLine(“Imap: " + messagesInfo.Count + " message(s) found.”);
foreach(ExchangeMessageInfo info in messagesInfo)
{
if(info.HasAttachments)
{
ExchangeAttachmentInfoCollection AttchInfoColl = info.Attachments;
foreach (ExchangeAttachmentInfo AttchInfo in AttchInfoColl)
{
Console.WriteLine(AttchInfo.AttachmentUri);
Console.WriteLine(AttchInfo.Name);
Console.WriteLine(AttchInfo.Size);
if(!AttchInfo.Name.Contains(“Some Criteria etc.”))
{
//Download the attachment
Aspose.Email.Mail.Attachment attch = client.FetchAttachment(AttchInfo.AttachmentUri);
}
}
}
}

I am afraid to share that this feature is currently not available with the ImapClient. In this case you have to filter unwanted downloaded attachments yourself based upon some criteria like name, extension etc.

Please feel free to write us back if you have any other query in this regard.