danzuep / MailKitSimplified

Send and receive emails easily, fluently, with one line of code for each operation.
MIT License
80 stars 10 forks source link

Download Mime Message without Attachment #35

Closed faisalbwn closed 1 year ago

faisalbwn commented 1 year ago

Hi Daniel,

I am downloading the single mime message with the following code:

// Fetch email message against presisted uid MimeMessage mimeMessage = await _mailFolderReader.GetMimeMessageAsync(new UniqueId(uniqueId), cancellationToken);

Can you please guide how can I download mime message without attachments. I want to download attachment later on demand.

danzuep commented 1 year ago

This usage will most likely change, but let me know if this works for you in the latest RC:

private async Task<MimeMessage?> ReceiveMimeMessageAsync(UniqueId uniqueId, CancellationToken cancellationToken = default)
{
    var uids = new UniqueId[] { uniqueId };
    var mimeMessages = await _imapReceiver.ReadMail.GetMimeMessageEnvelopeBodyAsync(uids, cancellationToken);
    return mimeMessages.FirstOrDefault();
}
danzuep commented 1 year ago

Please test this in the latest RC (same usage as for IMessageSummary):

var reader = _imapReceiver.ReadMail.Range(start, end, continuous: true);
IList<MimeMessage> mimeMessages;
do
{
    mimeMessages = await reader.GetMimeMessagesEnvelopeBodyAsync(cancellationToken);
    foreach (var mimeMessage in mimeMessages)
    {
        await ProcessMessages(mimeMessage, cancellationToken);
    }
}
while (mimeMessages.Count > 0);
faisalbwn commented 1 year ago

Thanks Daniel for support. I tested the new method, it is getting email without attachments. But it is not fetching TextBody in some cases and in some cases it is not fetching the HtmlBody.

danzuep commented 1 year ago

Most emails don't have a TextBody, and some don't have an HtmlBody.

faisalbwn commented 1 year ago

You are right Daniel. But there is difference in both methods, for the same email _mailFolderReader.GetMimeMessageAsync is returning but _mailFolderReader.GetMimeMessageEnvelopeBodyAsync is not.

danzuep commented 1 year ago

Thanks for checking that, I won't have time to look now but I'll let you know when I have.

faisalbwn commented 1 year ago

Sure Daniel, Thanks.

danzuep commented 1 year ago

I've simplified GetMimeMessageEnvelopeBodyAsync to only return HTML if it exists, otherwise text. I found two other issues with it too, so even though I didn't have time to test it this morning the latest RC should work for you now.

faisalbwn commented 1 year ago

Thanks Daniel, i will test it thoroughly and share the results.

danzuep commented 1 year ago

Let me know if you find anything else 🙂