0 votes

I have a Base64 encoded string and I need to add it to my mail as an attachment. How can I do that?
Thank you.

by (201 points)

1 Answer

0 votes

Decode it using System.Convert and use MailBuilder.AddAttachment overload that takes byte array to create an attachment, which is going to be added to the email you are creating.

 const string base64 = "SGVsbG8=";
 byte[] data = Convert.FromBase64String(base64);

 MailBuilder builder = new MailBuilder();
 builder.From.Add(new MailBox("alice@example.com"));
 builder.To.Add(new MailBox("bob@example.com"));
 builder.Text = "See attached file.";
 builder.Subject = "Attachment";

 MimeData attachment = builder.AddAttachment(data);
 attachment.FileName = "report.dat";

 IMail email = builder.Create();

[Edit]

Here's the code to create valid message with detached signature:

MimeFactory f = new MimeFactory();
MimeMixed root = f.CreateMimeMixed(f.CreateMimeText("text"));
root.ContentType = ContentType.MultipartSigned;
MimePkcs7Signature signature = 
    f.CreatePkcs7Signature(Convert.FromBase64String(@"MIIE....=="));
signature.ContentType = ContentType.ApplicationPkcs7Signature;
root.Parts.Add(signature);
MimeDocument document = new MimeDocumentFactory().CreateMimeDocument(root);

IMail mail = new MailBuilder().CreateFromDocument(document);
mail.From.Add(new MailBox("email@in-the-certificate.com"));

Following code checks, if the parsed message has valid signature:

new MailBuilder().CreateFromEml(mail.Render()).CheckSignature(true);

You should probably add additional Content-Type parameters, such as protocol and micalg:

Content-Type: multipart/signed;
    protocol="application/pkcs7-signature";
    micalg=SHA1;

Again - This is not correct way of doing this. You should use MailBuilder.Sign method:

https://www.limilabs.com/blog/send-signed-email-using-smime

https://www.limilabs.com/blog/send-signed-email-smime-sha-512

by (301k points)
edited by
I don't want to decode it, I need to add the encoded string as an attachment.
You must decode it. This is the only way to do it.
I've done it like this:
MimeData attach = builder.AddAttachment(myByteArray);//>it is my base64 string, not encoded just converted to byte array
attach.Headers["Content-Type"] = "application/pkcs7-signature; name=smime.p7s";
attach.Headers["Content-Description"] = "S/MIME Cryptographic Signature";
attach.FileName = "smime.p7s";

My string is added as an attachment and displayed like smime.p7s file. But I have a problem: In outlook or any other mail program, I cannot see this mail as signed mail. Its content type is multipart/mixed not multipart/signed. How can I add smime.p7s file to mail and display it as signed mail? I need to use my own sign method not Mail.dll's sign method.
Thank you.
> it is my base64 string, not encoded just converted to byte array
As I said - you must decode it. Otherwise data will be encoded twice.

>AddAttachment
Signature must be under multipart/signed element. Use MailBuilder.Sign method.

> I need to use my own sign method not Mail.dll's sign method.
Why?
We are a digital signature company and we will sign it via our interface with our customers' smartcard. So I need to add smime.p7s file to mail manually and display it as signature.
The thing is, that what you are trying to do doesn't make much sense. What exactly are you signing? Text? Html? Some message part?
Is it attached or detached signature? Why are you not using certificate store to access certificates stored on the smart card?
It is detached signature. I've signed my mail via Mail.dll. But your smime.p7s file is not compatible to my country's digital signature standards. So I have to use my interface.
I'm signing email.
What exactly is "not compatible"? Have you used SignatureConfiguration to configure how exactly the message is signed?
> I'm signing email.
This makes no sense. You can not sign entire email message. For example: during the transport headers are added to the message - that would invalidate any signature created in such way. You must sign exact content found as the first element under multipart/signed (it may be multipart/alternative for HTML email message) or use attached signature. This is why you should use MailBuilder.Sign method.
...