MailKit icon indicating copy to clipboard operation
MailKit copied to clipboard

Attachments are corrupt

Open atiqi36 opened this issue 10 years ago • 3 comments

Hi, First of all thanks for the project. it's really really helpful. I love it.

I have a question about attachments. I have seen all of your replies so trust me I have spent a full day on this problem but still can't get my head around. The way I have setup is that I download emails but not attachments, instead just save their names and disposition. Then when customer open the email, he can see the list of attachments and just click on them to download, however they are downloading as corrupted. Here is the download code.

        public MemoryStream GetAttachement(string fileName, string messageId)
        {
            HeaderSearchQuery searchCondition = SearchQuery.HeaderContains("Message-Id", messageId);
            Client.Inbox.Open(FolderAccess.ReadOnly);
            IList<UniqueId> uids = Client.Inbox.Search(searchCondition);

            foreach (UniqueId uniqueId in uids)
            {
                MimeMessage message = Client.Inbox.GetMessage(uniqueId);
                if (message.Attachments.Count() > 0)
                {
                    var attachement = message.Attachments.Where(x => x.ContentDisposition.FileName.Equals(fileName, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
                    if (attachement != null)
                    {
                        var stream = new MemoryStream();
                        attachement.WriteTo(stream);
                        attachement.WriteTo(System.Web.HttpContext.Current.Server.MapPath("~/") + fileName);
                        return stream;
                    }
                }
                //check for inline attachements
                var inlineattachement = message.BodyParts.Where(x => x.ContentType.MediaType.Equals("image", StringComparison.InvariantCultureIgnoreCase) && x.ContentType.Name.Equals(fileName, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
                if (inlineattachement != null)
                {
                    var stream = new MemoryStream();
                    inlineattachement.WriteTo(stream);
                    inlineattachement.WriteTo(System.Web.HttpContext.Current.Server.MapPath("~/") + fileName);
                    return stream;
                }
            }

            return null;
        }

As a test, I am writing to both stream and saving to file but nothing is working. I have the latest version, so any help will be greatly appreciated. Thanks in advance.

atiqi36 avatar Mar 30 '16 16:03 atiqi36

You are writing out the MIME container of the attachment and not the decoded attachment data.

What you need to do is this:

var stream = new MemoryStream();
if (attachment is MessagePart) {
    var rfc822 = (MessagePart) attachment;
    rfc822.Message.WriteTo (stream);
    rfc822.Message.WriteTo (System.Web.HttpContext.Current.Server.MapPath("~/") + fileName);
} else {
    var part = (MimePart) attachment;
    part.ContentObject.DecodeTo (stream);
    part.ContentObject.DecodeTo (System.Web.HttpContext.Current.Server.MapPath("~/") + fileName);
}
return stream;

(Note: For the inlineattachment logic, you don't need to check for MessagePart because that can't happen, image parts can only ever be MimeParts)

jstedfast avatar Mar 30 '16 18:03 jstedfast

Hi, thanks for quick reply but this is not working either. Exactly same problem. Any thoughts?

atiqi36 avatar Mar 30 '16 19:03 atiqi36

It's working, my fault this time ;) Thanks for the help.

atiqi36 avatar Mar 30 '16 19:03 atiqi36