C# Create MIME Message?
Is there any built in functionality to MIME a file in C# .Net? What I am looking to do is:
- Convert a file into a MIME message 开发者_高级运维
- Sign the MIME Message to a pcks 7 blob
- MIME that pkcs 7 blob
- Finally encrypt the entire thing.
Any suggestions on how I would go about this (not the encryption or signing part but the MIMEing)? What exactly is envolved in MIMEing a file?
There is a good commercial package for a small fee: Mime4Net
Rather than deal with third party libraries, I suggest you look to the core .NET library. Use the Attachment class; it's been around since .NET 2.
As far as I know there is no such support in the bare .NET. You have to try one of third party libraries. One of them is our Rebex Secure Mail for .NET. Following code shows how to achieve it:
using Rebex.Mail;
using Rebex.Mime.Headers;
using Rebex.Security.Certificates;
...
// load the sender's certificate and
// associated private key from a file
Certificate signer = Certificate.LoadPfx("hugo.pfx", "password");
// load the recipient's certificate
Certificate recipient = Certificate.LoadDer("joe.cer");
// create an instance of MailMessage
MailMessage message = new MailMessage();
// set its properties to desired values
message.From = "hugo@example.com";
message.To = "joe@example.com";
message.Subject = "This is a simple message";
message.BodyText = "Hello, Joe!";
message.BodyHtml = "Hello, <b>Joe</b>!";
// sign the message using Hugo's certificate
message.Sign(signer);
// and encrypt it using Joe's certificate
message.Encrypt(recipient);
// if you wanted Hugo to be able to read the message later as well,
// you can encrypt it for Hugo as well instead - comment out the previous
// encrypt and uncomment this one:
// message.Encrypt(recipient, signer)
(Code taken from the S/MIME tutorial page)
Something that hasn't been mentioned is MimeKit. It does everything that you need it to do.
(this is still a top hit in google so thought I'd add this gem)
精彩评论