MailKit save Attachments

前端 未结 1 846
面向向阳花
面向向阳花 2020-12-20 11:12

I\'m try save attachments from message

foreach(MimeKit.MimeEntity at message.Attachments) 
{
    at.WriteTo(\"nameFile\");
}

File saved, bu

相关标签:
1条回答
  • 2020-12-20 11:49

    You are saving the entire MIME object (including the headers). What you need to do is save the content.

    foreach (var attachment in message.Attachments) {
        using (var stream = File.Create ("fileName")) {
            if (attachment is MessagePart) {
                var part = (MessagePart) attachment;
    
                part.Message.WriteTo (stream);
            } else {
                var part = (MimePart) attachment;
    
                part.Content.DecodeTo (stream);
            }
        }
    }
    

    Hope that helps.

    0 讨论(0)
提交回复
热议问题