Using CAPICOM and .NET to envelope email.
From: Michael Marr (michael_at_marrconsulting.com)
Date: 08/31/05
- Next message: Daniel: "how to make sure a xsl document has valid xsl syntax? i tried loading it into an xml document but that doesnt show syntax errors inside attributes such as "foo/bar" vs "bar\foo""
- Previous message: Nicole Calinoiu: "Re: Questions and observations about CAS and the StrongNameIdentityPermssionAttribute."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Date: 30 Aug 2005 16:23:32 -0700
Hello,
I'm using CAPICOM and .NET to envelope an e-mail. When I receive the
e-mail, I see that it is encrypted, but the body of the e-mail is empty
when I open it. I've been working on the issue for a couple days, so
any help would be greatly appreciated. Here's the code I'm using.
Thanks,
Michael...
-----------------------------------------------------------
using System;
using System.IO;
using CAPICOM;
using CDO;
using System.Runtime.InteropServices;
namespace test
{
class EmailTest
{
[STAThread]
static void Main(string[] args)
{
try
{
EmailTest me = new EmailTest();
CAPICOM.Certificate signerCert = null;
CAPICOM.Certificate recieverCert = null;
CAPICOM.Store store = null;
CAPICOM.Certificates certs = null;
CDO.IMessage message = new MessageClass();
CDO.IMessage secondMsg = new MessageClass();
CDO.Configuration mailerConfig = new
CDO.ConfigurationClass();
//
// Setup mailer configuration.
//
mailerConfig.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value
= "localhost";
mailerConfig.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"].Value
= "25";
mailerConfig.Fields.Update();
//
// Retrieve certificates from certificate store.
//
store = new StoreClass();
store.Open(CAPICOM_STORE_LOCATION.CAPICOM_CURRENT_USER_STORE,
"AddressBook", CAPICOM_STORE_OPEN_MODE.CAPICOM_STORE_OPEN_EXISTING_ONLY
| CAPICOM_STORE_OPEN_MODE.CAPICOM_STORE_OPEN_READ_ONLY);
certs = (Certificates)store.Certificates;
certs =
(Certificates)certs.Find(CAPICOM_CERTIFICATE_FIND_TYPE.CAPICOM_CERTIFICATE_FIND_SUBJECT_NAME,
"foo@domain.com", false);
if(0 != certs.Count)
{
recieverCert = (Certificate)certs[1];
}
store.Open(CAPICOM_STORE_LOCATION.CAPICOM_CURRENT_USER_STORE,
"My", CAPICOM_STORE_OPEN_MODE.CAPICOM_STORE_OPEN_EXISTING_ONLY |
CAPICOM_STORE_OPEN_MODE.CAPICOM_STORE_OPEN_READ_ONLY);
certs = (Certificates)store.Certificates;
certs =
(Certificates)certs.Find(CAPICOM_CERTIFICATE_FIND_TYPE.CAPICOM_CERTIFICATE_FIND_SUBJECT_NAME,
"bar@domain.com", false);
if(0 != certs.Count)
{
signerCert = (Certificate)certs[1];
}
//
// Setup message...
//
message.To =
recieverCert.GetInfo(CAPICOM.CAPICOM_CERT_INFO_TYPE.CAPICOM_CERT_INFO_SUBJECT_EMAIL_NAME);
message.From =
signerCert.GetInfo(CAPICOM.CAPICOM_CERT_INFO_TYPE.CAPICOM_CERT_INFO_SUBJECT_EMAIL_NAME);
message.Subject = "Test Message " + DateTime.Now.ToString();
message.HTMLBody = "<html><body>Test 123</body></html>";
secondMsg = me.EnvelopeMessage(message, recieverCert);
secondMsg.Configuration = mailerConfig;
secondMsg.Send();
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
}
private CDO.IMessage EnvelopeMessage(CDO.IMessage message,
Certificate cert)
{
CDO.IMessage secureMsg = new CDO.MessageClass();
CDO.IBodyPart bodyPart = null;
CAPICOM.EnvelopedData envData = new
CAPICOM.EnvelopedDataClass();
string content = String.Empty;
string encryptedData = String.Empty;
//
// Copy message into new object.
//
secureMsg.DataSource.OpenObject(message, "IMessage");
//
// Configure header inforamtion.
//
bodyPart = secureMsg.BodyPart;
bodyPart.ContentMediaType =
"application/pkcs7-mime;smime-type=enveloped-data;name=smime.p7m;";
bodyPart.ContentTransferEncoding = "base64";
bodyPart.Fields["urn:schemas:mailheader:content-disposition"].Value =
"attachment;filename=smime.p7m";
bodyPart.Fields.Update();
//
// Add the recipient certificate and encrypt the bodypart from
the original message.
//
envData.Recipients.Add(cert);
ADODB.Stream tmpStream = message.BodyPart.GetStream();
content = tmpStream.ReadText(tmpStream.Size);
envData.Content = content;
encryptedData =
envData.Encrypt(CAPICOM.CAPICOM_ENCODING_TYPE.CAPICOM_ENCODE_BASE64);
//
// Get stream handle and copy the encrypted data into the secure
message.
//
ADODB.Stream stream =
secureMsg.BodyPart.GetDecodedContentStream();
stream.Type = ADODB.StreamTypeEnum.adTypeBinary;
byte[] byteData = Convert.FromBase64String(encryptedData);
stream.Write(byteData);
stream.Flush();
stream.Close();
return secureMsg;
}
}
}
- Next message: Daniel: "how to make sure a xsl document has valid xsl syntax? i tried loading it into an xml document but that doesnt show syntax errors inside attributes such as "foo/bar" vs "bar\foo""
- Previous message: Nicole Calinoiu: "Re: Questions and observations about CAS and the StrongNameIdentityPermssionAttribute."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]