Digital Signature (CDO and CryptoAPI)

G

Guest

I Have a .NET program sending mails using CDOEX.

These mail I need to sign. I god a tip that i shoud use CAPICOM. That worked
fine sending a mail with signature.

BUT the problem is that I have to type the password for my certificate every
time my program signs a mail :blush:(

My program is a windows service running on a server so typing passwords is
bad.

Then I got a new tip, I should use CryptoAPI instead of CAPICOM.

I think i have solved the CryptoAPI mystic and got my certificate, signed my
body of the mail and got the hash for it. But I don't know how to get my
signed code into the mail?

The code looks like this, and gives this error when the outlook client
reseive the mail:

Error: Can't open this item. Your Digital ID name can not be found by the
underlying security system.

This takes a byte array that I send from my CryptoAPI code.
Code:

private void SendMail(byte[] byteSignature)
{
CDO.IBodyPart oBodyPart;
ADODB.Fields cFields;
ADODB.Stream oStream;

// set sender, recipient, and subject.
oMessage = new CDO.Message();

oMessage.To = "(e-mail address removed)";
oMessage.Subject = "Test Mail";
oMessage.Fields["urn:schemas:mailheader:date"].Value = DateTime.UtcNow;
oMessage.Fields.Update();

oMessage.From = "(e-mail address removed)";

oBodyPart = oMessage.BodyPart.AddBodyPart(1);
cFields = oBodyPart.Fields;

cFields["urn:schemas:mailheader:content-type"].Value =
CDO.CdoContentTypeValues.cdoTextPlain;
cFields.Update();

oStream = oBodyPart.GetDecodedContentStream();
oStream.WriteText("Hello this is some test text",0);
oStream.Flush();

//
//
// Start the new message
//
//
CDO.Message oSignedMsg = new CDO.Message();
CDO.IBodyPart oBodyPart2;
ADODB.Fields cFields2;
ADODB.Stream oStream2;

oSignedMsg.From = "(e-mail address removed)";

// this is to be a clear text signed message so we need to copy the
interesting
// parts (sender, recipient, and subject) into the new header
oSignedMsg.To = oMessage.To;
oSignedMsg.CC = oMessage.CC;
oSignedMsg.Subject = oMessage.Subject;

oBodyPart2 = oSignedMsg.BodyPart.AddBodyPart(1);
cFields2 = oBodyPart2.Fields;

cFields2["urn:schemas:mailheader:content-type"].Value =
oMessage.BodyPart.BodyParts[1].Fields["urn:schemas:mailheader:content-type"].Value;
cFields2.Update();

// Attach the signature and let CDO base64 encode it
oBodyPart2 = oSignedMsg.BodyPart.AddBodyPart(1);
cFields2 = oBodyPart2.Fields;
oBodyPart2.Fields["urn:schemas:mailheader:content-type"].Value =
"application/x-pkcs7-signature\rName = " + '\u0022' + "smime.p7s" + '\u0022'
+ "";

oBodyPart2.Fields["urn:schemas:mailheader:content-transfer-encoding"].Value =
"base64";
oBodyPart2.Fields["urn:schemas:mailheader:content-disposition"].Value =
"attachment;\rFileName=" + '\u0022' + "smime.p7s" + '\u0022' + "";
cFields2.Update();
//
oStream2 = oBodyPart2.GetDecodedContentStream();
oStream2.Type = ADODB.StreamTypeEnum.adTypeBinary;
oStream2.Write (byteSignature);
oStream2.Flush();

// Set the messages content type, this needs to be done last to ensure
it is not changed when we add the BodyParts

oSignedMsg.Fields["urn:schemas:mailheader:content-type"].Value =
"multipart/signed;\rprotocol=" + '\u0022' + "application/x-pkcs7-signature" +
'\u0022' + ";\rmicalg=SHA1;";

oSignedMsg.Fields.Update();

oMessage = oSignedMsg;
oMessage.Send();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top