Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 646
  • Last Modified:

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

0
sitqadmin
Asked:
sitqadmin
1 Solution
 
sitqadminAuthor Commented:
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

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now