Question

I'm using C#, .NET 4.0 to send a digitally-signed email, like so:

private void SendMailMessage(string emailTo)
{
    MailMessage message = new MailMessage();
    message.From = new MailAddress(fromAddress);
    message.To.Add(new MailAddress(emailTo));
    message.Subject = "Regarding your lottery winnings";
    message.IsBodyHtml = false;
    string body = "Content-Type: text/plain;charset=\"iso-8859-1\"\nContent-Transfer-Encoding: quoted-printable\n\nThe URL to your secret is: " + url + "\nIt can only be viewed once.";                
    byte[] messageBytes = Encoding.ASCII.GetBytes(body);
    ContentInfo content = new ContentInfo(messageBytes);
    SignedCms signedCms = new SignedCms(content, false);
    CmsSigner Signer = new CmsSigner(SubjectIdentifierType.IssuerAndSerialNumber, emailCert);
    signedCms.ComputeSignature(Signer);
    byte[] signedBytes = signedCms.Encode();
    MemoryStream ms = new MemoryStream(signedBytes);
    AlternateView av = new AlternateView(ms, "application/pkcs7-mime; smime-type=signed-data;name=smime.p7m");
    message.AlternateViews.Add(av);                
    SmtpClient client = new SmtpClient(smtpServer, int.Parse(smtpServerPort));
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.Send(message);
    message.Dispose();
    client = null;
}

Notice that message.Body is left blank, and only the AlternateView is added to the email. When I send this email and view it in Outlook, it shows up perfectly, with a certificate icon on the email message, and the S/MIME Outlook extensions validate the signature successfully and automatically.

Aces.

(If I add anything to message.Body, it breaks. Outlook no longer recognizes it as a signed email and I only see the message.Body text, not the AlternateView.)

But if I send this email to a Gmail address, for instance, it shows up as a blank email with smime.p7m as an attachment, and inside it I see the text of the email, but it's surrounded by what looks like a ton of binary gibberish.

Is there a way to make this digitally signed email compatible with both an Outlook client and a Gmail web client?

Was it helpful?

Solution

When outlook generates a signed email it adds an alternate view with the signed message, another alternate view with the html version and then another alternate view with a plain text version. I think if you also do this then it will work in most all email clients.

there is a plain text alternate view that is not signed Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit

there is an html version that is not signed Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable

There is a signed alternate view Content-Type: application/pkcs7-signature; name="smime.p7s" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="smime.p7s"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top