Link to home
Start Free TrialLog in
Avatar of sitqadmin
sitqadminFlag for Canada

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


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);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Anurag Thakur
Anurag Thakur
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sitqadmin

ASKER

Thank you very much for that link, that gave me a good lead on how to get the solution.  I had to do some tweaks a little but now it works.  Let me post my final solution :


        private void SendEncryptedEmail(X509Certificate2 certEncrypt, string To, string From, string Subject, string Body,
                                        string SmtpServer, int SmtpPort, bool HTML, String sFileName)
        {
 
 
            StringBuilder Message = new StringBuilder();
 
            // Create Mutlipart/Mixed email
            Message.AppendLine("content-type: multipart/mixed;");
            Message.AppendLine(" boundary=\"----=_NextPart_414_0c36_f93e4f59.6ee76195_.MIX\"");
            Message.AppendLine();
 
            // Create Body Part
            Message.AppendLine("------=_NextPart_414_0c36_f93e4f59.6ee76195_.MIX");
            Message.AppendLine("Content-Type: text/" + ((HTML) ? "html" : "plain") + "; charset=\"iso-8859-1\"");
            Message.AppendLine("Content-Transfer-Encoding: 8bit");
            Message.AppendLine();
            Message.AppendLine(Body);
 
 
            // Create Attachment Part
            String sFileNameOnly = sFileName.Remove(0, (sFileName.LastIndexOf("\\") + 1));
            Byte[] aFileBytes = File.ReadAllBytes(sFileName);
 
            Message.AppendLine("------=_NextPart_414_0c36_f93e4f59.6ee76195_.MIX");
            Message.AppendLine("content-disposition: attachment; filename=\"" + sFileNameOnly + "\"");
            Message.AppendLine("content-transfer-encoding: binary");
            Message.AppendLine(" name=\"" + sFileNameOnly + "\"");
            Message.AppendLine();
            Message.AppendLine(Encoding.Default.GetString(aFileBytes));
 
 
            // Convert Body into Byte[]
            byte[] BodyBytes = Encoding.Default.GetBytes(Message.ToString());
 
            // Encrypt the Body
            EnvelopedCms ECms = new EnvelopedCms(new ContentInfo(BodyBytes));
            CmsRecipient Recipient = new CmsRecipient(SubjectIdentifierType.IssuerAndSerialNumber, certEncrypt);
            ECms.Encrypt(Recipient);
            byte[] EncryptedBytes = ECms.Encode();
 
            // Create the MailMessage to send
            MailMessage Msg = new MailMessage();
            Msg.To.Add(new MailAddress(To));
            Msg.From = new MailAddress(From);
            Msg.Subject = Subject;
 
            MemoryStream ms = new MemoryStream(EncryptedBytes);
            AlternateView av = new AlternateView(ms, "application/pkcs7-mime; smime-type=signed-data;name=smime.p7m");
            Msg.AlternateViews.Add(av);
 
            SmtpClient smtp = new SmtpClient(SmtpServer, SmtpPort);
            smtp.UseDefaultCredentials = true;
            smtp.Send(Msg);
 
        }

Open in new window