sitqadmin
asked on
Encrypted MailMessage with Attachements
Hi,
I have succeeded sending an encrypted mailmessage using the following code.
But if I try to add attachements (using mail.Attachments.Add(new Attachement(FileName)) ) the email is not encrypted anymore and the message body is empty.
Anyone have a clue on how to do it?
Thanks
I have succeeded sending an encrypted mailmessage using the following code.
But if I try to add attachements (using mail.Attachments.Add(new Attachement(FileName)) ) the email is not encrypted anymore and the message body is empty.
Anyone have a clue on how to do it?
Thanks
MailMessage mail = new MailMessage();
StringBuilder Message = new StringBuilder();
Message.AppendLine("Content-Type: text/html; charset=\"iso-8859-1\"");
Message.AppendLine("Content-Transfer-Encoding: 8bit");
Message.AppendLine();
Message.AppendLine(sEmailBody);
byte[] BodyBytes = Encoding.Default.GetBytes(Message.ToString());
EnvelopedCms ECms = new EnvelopedCms(new ContentInfo(BodyBytes));
CmsRecipient Recipient = new CmsRecipient(SubjectIdentifierType.IssuerAndSerialNumber, MyX509Certificate);
ECms.Encrypt(Recipient);
byte[] EncryptedBytes = ECms.Encode();
MemoryStream ms = new MemoryStream(EncryptedBytes);
AlternateView av = new AlternateView(ms, "application/pkcs7-mime; smime-type=signed-data;name=smime.p7m");
mail.AlternateViews.Add(av);
mail.To.Add(new MailAddress("mymail@mail.com"));
mail.From = new MailAddress("mymail@mail.com");
mail.Subject = "Test";
SmtpClient mySmtpClient = new SmtpClient();
mySmtpClient.Host = MyHost;
mySmtpClient.Port = MyPort;
mySmtpClient.Send(mail);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Open in new window