发送multipart / signed email c#

时间:2013-07-30 10:37:36

标签: c# .net email code-signing smime

我正在尝试按照RFC1847协议创建一个多部分/签名的mime电子邮件。 这是它应该看起来的样子(签名的一部分被删除):

Content-Type: multipart/signed; protocol="application/pkcs7-signature"
    micalg=sha1; boundary="--PTBoundry=3"

----PTBoundry=3
Content-Type: multipart/mixed;
boundary="--PTBoundry=2"

----PTBoundry=2
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: quoted-printable

TEST AF signed
----PTBoundry=2
Content-Type: application/octet-stream;
    name=test2.txt
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
    filename=test2.txt

bWludGVzdGF0dGFjaG1lbnRzaWduZWQ=
----PTBoundry=2--

----PTBoundry=3
Content-Type: application/pkcs7-signature; name="smime.p7s"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="smime.p7s"

MIIKfAYJKoZIhvcNAQcCoIIKbTCCCmkCAQExCzAJBgUrDgMCGgUAMAsGCSqGSIb3DQEHAaCCCNow
ggPVMIICvaADAgECAgMCNtEwDQYJKoZIhvcNAQEFBQAwQjELMAkGA1UEBhMCVVMxFjAUBgNVBAoT
PrENekpgrYkz

----PTBoundry=3--
empty line

我无法弄清楚如何将其作为电子邮件实际发送。我正在使用MailMessage,我尝试按下面的说明添加它:

var stream = new MemoryStream(Encoding.ASCII.GetBytes(message));
var view = new AlternateView(stream, "application/pkcs7-mime; smime-type=signed-data;name=smime.p7m");

然而它不起作用。 MailMessage添加了不同的标题并将其全部搞砸了。

如何正确发送?

1 个答案:

答案 0 :(得分:0)

我不再使用MailMessage类,因为我无法使用它。 相反,我正在使用这样的EWS托管api:

var mail = new EmailMessage(_service);
mail.Subject = filename;
mail.MimeContent = new MimeContent("us-ascii", Encoding.ASCII.GetBytes(messageContentWithHeaders));
mail.SendAndSaveCopy();