需要解析哑剧内容

时间:2019-01-29 15:42:40

标签: c# exchangewebservices mimekit

我有一封电子邮件的mime内容的Base-64编码字符串。电子邮件本身包含.p7m附件。

是否可以使用Mimekit解析mime内容并提取附件?

谢谢!

1 个答案:

答案 0 :(得分:0)

对于您要做什么,我还不太清楚,但听起来您只有消息的一部分(即MIME部分之一的base64编码内容)。

如果您要做的只是获取原始的smime.p7m文件附件(这是您所拥有的base64 blob内部的附件),那么您真正需要做的就是调用File.WriteAllBytes ("smime.p7m", Convert.FromBase64String(base64))

但是,如果您想执行某些操作(例如解密或验证签名),则需要获得ApplicationPkcs7Mime表示形式:

ApplicationPkcs7Mime pkcs7;

using (var stream = new MemoryStream ()) {
    byte[] buffer;

    // write the Content-Type header
    buffer = Encoding.ASCII.GetBytes ("Content-Type: application/pkcs7-mime; name=smime.p7m\r\n");
    stream.Write (buffer, 0, buffer.Length);

    // write the Content-Type header
    buffer = Encoding.ASCII.GetBytes ("Content-Type: application/pkcs7-mime; name=smime.p7m\r\n");
    stream.Write (buffer, 0, buffer.Length);

    // write the header termination sequence
    buffer = Encoding.ASCII.GetBytes ("\r\n");
    stream.Write (buffer, 0, buffer.Length);

    // write the base64 encoded content
    buffer = Encoding.ASCII.GetBytes (base64);
    stream.Write (buffer, 0, buffer.Length);

    // rewind the stream
    stream.Position = 0;

    pkcs7 = (ApplicationPkcs7Mime) MimeEntity.Load (stream);
}

现在可以使用所有的ApplicationPkcs7Mime API。