使用MimeKit寄發加密加簽信件

MimeKit.NET Foundation 開放社群的一項專案,提供處理各式Mime格式的函式庫,例如支持 S/MIME 和 PGP,並且支援.NET/Mono的所有平台等。

預設DefaultSecureMimeContext使用SQLite
Windows環境可使用WindowsSecureMimeContext
或TemporarySecureMimeContext

var joey = new MailboxAddress ("Joey", "joey@friends.com");
var alice = new MailboxAddress ("Alice", "alice@wonderland.com");

var message = new MimeMessage ();
message.From.Add (joey);
message.To.Add (alice);
message.Subject = "How you doin?";

// create our message body (perhaps a multipart/mixed with the message text and some
// image attachments, for example)
var body = CreateMessageBody ();

// now to digitally sign our message body using our custom S/MIME cryptography context
using (var ctx = new MySecureMimeContext ()) {
    // Note: this assumes that "Joey" has an S/MIME signing certificate and private key
    // with an X.509 Subject Email identifier that matches Joey's email address.
    message.Body = MultipartSigned.Create (ctx, joey, DigestAlgorithm.Sha1, body);
}

// now to digitally sign our message body using our custom S/MIME cryptography context
using (var ctx = new MySecureMimeContext ()) {
    var certificate = GetJoeysX509Certificate ();
    var signer = new CmsSigner (certificate);
    signer.DigestAlgorithm = DigestAlgorithm.Sha1;

    message.Body = MultipartSigned.Create (ctx, signer, body);
}

留言

這個網誌中的熱門文章

IIS 啟用HTTP Strict Transport Security (HSTS)

ASP.NET寄發加密加簽信件

'Microsoft.ACE.OLEDB.12.0' 提供者並未登錄於本機電腦上。 (System.Data)