Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
739 views
in Technique[技术] by (71.8m points)

c# - MailKit save Attachments

I'm try save attachments from message

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

File saved, but when I open I get the error the file is corrupted or too large The size of this file is 88 kb, but size of the file should be equal to 55 kb.

I think that in all recorded message file.

How do I only record the attachment?

MailKit v1.2.0.0 MimeKit 1.2.0.0

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...