jstedfast / MimeKit

A .NET MIME creation and parser library with support for S/MIME, PGP, DKIM, TNEF and Unix mbox spools.
http://www.mimekit.net
MIT License
1.84k stars 373 forks source link

How do I decrypt attachments in S/MIME messages and save them #1055

Closed sofm13 closed 4 months ago

sofm13 commented 4 months ago

I am sorry for not understanding the document. S/MIME encryption is used in the email attachment, and "smime-type" parameter set to "signed-data", how should I save the attachment after decryption? The following code is used.

var message = await inbox.GetMessageAsync(uniqueId); var entity = Decrypt(message); var attachments = new List(); var multiparts = new List(); using (var iter = new MimeIterator(message)) { // collect our list of attachments and their parent multiparts while (iter.MoveNext()) { var multipart = iter.Parent as Multipart; var part = iter.Current as MimePart;

    if (multipart != null && part != null && part.IsAttachment)
    {
        // keep track of each attachment's parent multipart
        multiparts.Add(multipart);
        attachments.Add(part);
    }
}

} for (int i = 0; i < attachments.Count; i++) multiparts[i].Remove(attachments[i]);

How do I decrypt and save attachments in multiparts