private void _GenerateHtmlForEmail(string htmlName, string emlFile, string tempFolder) { try { Mail oMail = new Mail("EG-B1374632949-00215-D3BEB6416TE2E729-A84ADDF9C5A1C85F"); oMail.Load(emlFile, false); if (oMail.IsEncrypted) { try { // este mensaje es encriptado, que descifrarlo por certificado predeterminado de usuario. // También puede utilizar el certificado especificado como este // OCert = nuevo Certificado (); // OCert.Load ("c: \\ test.pfx", "pfxpassword", Certificate.CertificateKeyLocation.CRYPT_USER_KEYSET) // Omail = oMail.Decrypt (oCert); oMail = oMail.Decrypt(null); } catch (Exception ep) { MessageBox.Show(ep.Message); oMail.Load(emlFile, false); } } if (oMail.IsSigned) { try { // este correo electrónico se firma digital. EAGetMail.Certificate cert = oMail.VerifySignature(); MessageBox.Show("This email contains a valid digital signature."); // puede agregar el certificado en el almacén de certificados como este //cert.AddToStore (Certificate.CertificateStoreLocation.CERT_SYSTEM_STORE_CURRENT_USER, // "Libreta de direcciones"); // Entonces usted puede utilizar enviar el correo electrónico cifrado de nuevo a este remitente. } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); BusSisLog.Log_Error(ex.Message.ToString(), eTipoError.ERROR, this.ToString()); } } // Winmail.dat decodificación (corriente de Outlook TNEF) automáticamente. // También convertir cuerpo RTF a cuerpo HTML automáticamente. oMail.DecodeTNEF(); string html = oMail.HtmlBody; StringBuilder hdr = new StringBuilder(); hdr.Append("<font face=\"Courier New,Arial\" size=2>"); hdr.Append("<b>From:</b> " + _FormatHtmlTag(oMail.From.ToString()) + "<br>"); MailAddress[] addrs = oMail.To; int count = addrs.Length; if (count > 0) { hdr.Append("<b>To:</b> "); for (int i = 0; i < count; i++) { hdr.Append(_FormatHtmlTag(addrs[i].ToString())); if (i < count - 1) { hdr.Append(";"); } } hdr.Append("<br>"); } addrs = oMail.Cc; count = addrs.Length; if (count > 0) { hdr.Append("<b>Cc:</b> "); for (int i = 0; i < count; i++) { hdr.Append(_FormatHtmlTag(addrs[i].ToString())); if (i < count - 1) { hdr.Append(";"); } } hdr.Append("<br>"); } hdr.Append(String.Format("<b>Subject:</b>{0}<br>\r\n", _FormatHtmlTag(oMail.Subject))); Attachment[] atts = oMail.Attachments; count = atts.Length; if (count > 0) { if (!Directory.Exists(tempFolder)) { Directory.CreateDirectory(tempFolder); } hdr.Append("<b>Attachments:</b>"); for (int i = 0; i < count; i++) { Attachment att = atts[i]; string attname = String.Format("{0}\\{1}", tempFolder, att.Name); att.SaveAs(attname, true); hdr.Append(String.Format("<a href=\"{0}\" target=\"_blank\">{1}</a> ", attname, att.Name)); if (att.ContentID.Length > 0) { //show embedded image. html = html.Replace("cid:" + att.ContentID, attname); } else if (String.Compare(att.ContentType, 0, "image/", 0, "image/".Length, true) == 0) { //show attached image. html = html + String.Format("<hr><img src=\"{0}\">", attname); } } } Regex reg = new Regex("(<meta[^>]*charset[ \t]*=[ \t\"]*)([^<> \r\n\"]*)", RegexOptions.Multiline | RegexOptions.IgnoreCase); html = reg.Replace(html, "$1utf-8"); if (!reg.IsMatch(html)) { hdr.Insert(0, "<meta HTTP-EQUIV=\"Content-Type\" Content=\"text/html; charset=utf-8\">"); } html = hdr.ToString() + "<hr>" + html; FileStream fs = new FileStream(htmlName, FileMode.Create, FileAccess.Write, FileShare.None); byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(html); fs.Write(data, 0, data.Length); fs.Close(); oMail.Clear(); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); BusSisLog.Log_Error(ex.Message.ToString(), eTipoError.ERROR, this.ToString()); } }
//we generate a html + attachment folder for every email, once the html is create, // next time we don't need to parse the email again. private void _GenerateHtmlForEmail(string htmlName, string emlFile, string tempFolder) { //For evaluation usage, please use "TryIt" as the license code, otherwise the //"invalid license code" exception will be thrown. However, the object will expire in 1-2 months, then //"trial version expired" exception will be thrown. Mail oMail = new Mail("TryIt"); oMail.Load(emlFile, false); if (oMail.IsEncrypted) { try { //this email is encrypted, we decrypt it by user default certificate. // you can also use specified certificate like this // oCert = new Certificate(); // oCert.Load("c:\\test.pfx", "pfxpassword", Certificate.CertificateKeyLocation.CRYPT_USER_KEYSET) // oMail = oMail.Decrypt( oCert ); oMail = oMail.Decrypt(null); } catch (Exception ep) { MessageBox.Show(ep.Message); oMail.Load(emlFile, false); } } if (oMail.IsSigned) { try { //this email is digital signed. EAGetMail.Certificate cert = oMail.VerifySignature(); MessageBox.Show("This email contains a valid digital signature."); //you can add the certificate to your certificate storage like this //cert.AddToStore( Certificate.CertificateStoreLocation.CERT_SYSTEM_STORE_CURRENT_USER, // "addressbook" ); // then you can use send the encrypted email back to this sender. } catch (Exception ep) { MessageBox.Show(ep.Message); } } string html = oMail.HtmlBody; StringBuilder hdr = new StringBuilder(); hdr.Append("<font face=\"Courier New,Arial\" size=2>"); hdr.Append("<b>From:</b> " + _FormatHtmlTag(oMail.From.ToString()) + "<br>"); MailAddress [] addrs = oMail.To; int count = addrs.Length; if (count > 0) { hdr.Append("<b>To:</b> "); for (int i = 0; i < count; i++) { hdr.Append(_FormatHtmlTag(addrs[i].ToString())); if (i < count - 1) { hdr.Append(";"); } } hdr.Append("<br>"); } addrs = oMail.Cc; count = addrs.Length; if (count > 0) { hdr.Append("<b>Cc:</b> "); for (int i = 0; i < count; i++) { hdr.Append(_FormatHtmlTag(addrs[i].ToString())); if (i < count - 1) { hdr.Append(";"); } } hdr.Append("<br>"); } hdr.Append(String.Format("<b>Subject:</b>{0}<br>\r\n", _FormatHtmlTag(oMail.Subject))); Attachment [] atts = oMail.Attachments; count = atts.Length; if (count > 0) { if (!Directory.Exists(tempFolder)) { Directory.CreateDirectory(tempFolder); } hdr.Append("<b>Attachments:</b>"); for (int i = 0; i < count; i++) { Attachment att = atts[i]; //this attachment is in OUTLOOK RTF format, decode it here. if (String.Compare(att.Name, "winmail.dat") == 0) { Attachment[] tatts = null; try { tatts = Mail.ParseTNEF(att.Content, true); } catch (Exception ep) { MessageBox.Show(ep.Message); continue; } int y = tatts.Length; for (int x = 0; x < y; x++) { Attachment tatt = tatts[x]; string tattname = String.Format("{0}\\{1}", tempFolder, tatt.Name); tatt.SaveAs(tattname, true); hdr.Append(String.Format("<a href=\"{0}\" target=\"_blank\">{1}</a> ", tattname, tatt.Name)); } continue; } string attname = String.Format("{0}\\{1}", tempFolder, att.Name); att.SaveAs(attname, true); hdr.Append(String.Format("<a href=\"{0}\" target=\"_blank\">{1}</a> ", attname, att.Name)); if (att.ContentID.Length > 0) { //show embedded image. html = html.Replace("cid:" + att.ContentID, attname); } else if (String.Compare(att.ContentType, 0, "image/", 0, "image/".Length, true) == 0) { //show attached image. html = html + String.Format("<hr><img src=\"{0}\">", attname); } } } Regex reg = new Regex("(<meta[^>]*charset[ \t]*=[ \t\"]*)([^<> \r\n\"]*)", RegexOptions.Multiline | RegexOptions.IgnoreCase); html = reg.Replace(html, "$1utf-8"); if (!reg.IsMatch(html)) { hdr.Insert(0, "<meta HTTP-EQUIV=\"Content-Type\" Content=\"text/html; charset=utf-8\">"); } html = hdr.ToString() + "<hr>" + html; FileStream fs = new FileStream(htmlName, FileMode.Create, FileAccess.Write, FileShare.None); byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(html); fs.Write(data, 0, data.Length); fs.Close(); oMail.Clear(); }