danzuep / MailKitSimplified

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

Download Mime Message without Attachment #35

Closed faisalbwn closed 9 months ago

faisalbwn commented 10 months 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 10 months 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 10 months 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 10 months 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 10 months ago

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

faisalbwn commented 10 months 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 10 months ago

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

faisalbwn commented 10 months ago

Sure Daniel, Thanks.

danzuep commented 10 months 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 10 months ago

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

danzuep commented 9 months ago

Let me know if you find anything else 🙂