tags:

views:

269

answers:

4

Is there any built in functionality to MIME a file in C# .Net? What I am looking to do is:

  1. Convert a file into a MIME message
  2. Sign the MIME Message to a pcks 7 blob
  3. MIME that pkcs 7 blob
  4. 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?

A: 

Have a look here, http://www.codeproject.com/KB/cs/MIME_De_Encode_in_C_.aspx

d_schnell
That code is pretty bad. First it was created by someone with little knowledge of C# who simply translated it from C++ line by line. Second it's full of bugs that have been ignored by the author (read the comments). At least you were able to find something, but one would be better off getting the C++ code and writing their your own conversion.
Tergiver
+2  A: 

There is a good commercial package for a small fee: Mime4Net

Jappie
@Jappie: If Petey accepts this answer, I'll award the bounty. My guess is that a commercial package won't be what is desired, but I cannot speak for Petey. Personally I would go this route to get something I felt confident would work.
Tergiver
I ended up writing my own MIME Parser. But i'll accept this.
Petey B
+1  A: 

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.

Randolpho
Is it possible to extract the MIME encoded message from the System.Net.Mail classes without sending it?
Rup
@Rup: excellent point. The answer is, of course, no. At least, not without digging into the internal MailWriter class. I obviously misread the question; if he wanted to sign and encrypt a MIME attachment as part as an email, it's as easy as signing and encrypting the content before passing it to the Attachment.
Randolpho
+1  A: 

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 = "[email protected]";
message.To = "[email protected]";
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)

Martin Vobr