Esempio n. 1
0
        public bool SendMailMessageWithDynamicLogo(EmailDetail emailDetail)
        {
            try
            {
                ServicePointManager.ServerCertificateValidationCallback = delegate { return(true); };
                // Instantiate a new instance of MailMessage
                bool validateSendEmail = true;
                System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
                string subject = emailDetail.Subject;
                ValidateSendEmail(emailDetail.ListTo, emailDetail.EmailCC, emailDetail.EmailBcc, ref subject, ref validateSendEmail, message, emailDetail.EmailAdmin);

                if (validateSendEmail)
                {
                    #region Create Email Header

                    message.Headers.Add("Message-ID", "<" + emailDetail.MessageID + ">");

                    if (!string.IsNullOrWhiteSpace(emailDetail.InReplyTo))
                    {
                        message.Headers.Add("In-Reply-To", emailDetail.InReplyTo);
                        message.Headers.Add("References", emailDetail.InReplyTo);
                    }
                    message.From = new MailAddress(emailDetail.FromAddress);

                    message.Subject    = subject;
                    message.IsBodyHtml = true;

                    #endregion Create Email Header

                    #region Embed Logo

                    List <LinkedResource> resourceCollection = new List <LinkedResource>();
                    EmbedLogo(emailDetail, "CompanyLogo", emailDetail.CompanyLogo, resourceCollection);
                    EmbedLogo(emailDetail, "CompanySignature", emailDetail.CompanySignature, resourceCollection);
                    EmbedLogo(emailDetail, "HotelPlatformLogo", emailDetail.HotelLogo, resourceCollection);
                    EmbedLogo(emailDetail, "T2PLogo", emailDetail.T2pLogo, resourceCollection);
                    EmbedLogo(emailDetail, "AirplusLogo", emailDetail.AirPlusLogo, resourceCollection);
                    AlternateView htmlView = AlternateView.CreateAlternateViewFromString(emailDetail.Body, null, "text/html");
                    resourceCollection.ForEach(x => htmlView.LinkedResources.Add(x));

                    message.AlternateViews.Add(htmlView);

                    #endregion Embed Logo

                    #region Attachment

                    if (emailDetail.ListAtt != null)
                    {
                        foreach (string item in emailDetail.ListAtt)
                        {
                            if (File.Exists(item))
                            {
                                System.Net.Mail.Attachment att = AttachmentHelper.CreateAttachment(item, Path.GetFileName(item), TransferEncoding.Base64);
                                message.Attachments.Add(att);
                            }
                        }
                    }

                    #endregion Attachment

                    #region Send Email

                    SmtpClient client = new SmtpClient(emailDetail.Server);
                    client.Credentials = new System.Net.NetworkCredential(emailDetail.UserName, emailDetail.Password);
                    client.EnableSsl   = false;
                    client.Send(message);

                    #endregion Send Email

                    foreach (System.Net.Mail.Attachment att in message.Attachments)
                    {
                        att.Dispose();
                    }
                }
                message.Dispose();

                return(true);
            }
            catch (Exception ex)
            {
                WriteLog(emailDetail);
                T2P._2015.Cross.Utility.ExceptionHandling.ExceptionHelper.GetDetailMessageAndLog(ex);
                return(false);
            }
        }
Esempio n. 2
0
        //private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(ToolSendEmail));
        //private string version = "Version " + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString() + " ";

        //public static string MailServer = ConfigurationManager.AppSettings["MailServer"];

        //public static string[] EmailBCC = { "*****@*****.**" };

        public bool SendMail(
            string eMAIL_SEND_USER,
            string eMAIL_SEND_PASSWORD,
            string eMAIL_SEND_SERVER,
            string from, string to, string inReplyTo, string cC, string bCC, string subject, string body, string[] listAttachment,
            string localPathCompanyLogo,
            string localPathHotelPlatformLogo,
            string localPathAirPlusLogo,
            string localPathCompanySignatureLogo,
            string localPathT2PLogo,
            string messageID = ""
            )
        {
            bool result = false;

            try
            {
                bool validateSendEmail = true;
                System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
                List <string> listTo = new List <string>()
                {
                    to
                };
                ValidateSendEmail(listTo, cC, bCC, ref subject, ref validateSendEmail, message);

                if (validateSendEmail)
                {
                    message.Headers.Add("Message-ID", "<" + (!string.IsNullOrWhiteSpace(messageID) ? messageID : Guid.NewGuid().ToString()) + ">");
                    message.From       = new MailAddress(from);
                    message.Subject    = subject;
                    message.IsBodyHtml = true;

                    if (!string.IsNullOrWhiteSpace(inReplyTo))
                    {
                        message.Headers.Add("In-Reply-To", inReplyTo);
                        message.Headers.Add("References", inReplyTo);
                    }

                    // embed logo
                    List <LinkedResource> listLinkedResource = new List <LinkedResource>();
                    if (!string.IsNullOrWhiteSpace(localPathCompanyLogo))
                    {
                        LinkedResource linkedResource = new LinkedResource(localPathCompanyLogo);
                        linkedResource.ContentId = Path.GetFileName(localPathCompanyLogo);
                        ContentType ct = new ContentType(MediaTypeNames.Image.Gif);
                        linkedResource.ContentType = ct;
                        listLinkedResource.Add(linkedResource);
                    }

                    if (!string.IsNullOrWhiteSpace(localPathHotelPlatformLogo))
                    {
                        LinkedResource linkedResource = new LinkedResource(localPathHotelPlatformLogo);
                        linkedResource.ContentId = Path.GetFileName(localPathHotelPlatformLogo);
                        ContentType ct = new ContentType(MediaTypeNames.Image.Gif);
                        linkedResource.ContentType = ct;
                        listLinkedResource.Add(linkedResource);
                    }

                    if (!string.IsNullOrWhiteSpace(localPathAirPlusLogo))
                    {
                        LinkedResource linkedResource = new LinkedResource(localPathAirPlusLogo);
                        linkedResource.ContentId = Path.GetFileName(localPathAirPlusLogo);
                        ContentType ct = new ContentType(MediaTypeNames.Image.Gif);
                        linkedResource.ContentType = ct;
                        listLinkedResource.Add(linkedResource);
                    }

                    if (!string.IsNullOrWhiteSpace(localPathCompanySignatureLogo))
                    {
                        LinkedResource linkedResource = new LinkedResource(localPathCompanySignatureLogo);
                        linkedResource.ContentId = Path.GetFileName(localPathCompanySignatureLogo);
                        ContentType ct = new ContentType(MediaTypeNames.Image.Gif);
                        linkedResource.ContentType = ct;
                        listLinkedResource.Add(linkedResource);
                    }

                    if (!string.IsNullOrWhiteSpace(localPathT2PLogo))
                    {
                        LinkedResource linkedResource = new LinkedResource(localPathT2PLogo);
                        linkedResource.ContentId = Path.GetFileName(localPathT2PLogo);
                        ContentType ct = new ContentType(MediaTypeNames.Image.Gif);
                        linkedResource.ContentType = ct;
                        listLinkedResource.Add(linkedResource);
                    }

                    // convert base64 image to embed
                    Regex r = new Regex(@"(<img\s(?>(?!src=)[^>])*?src=\"")data:image/(gif|png|jpeg);base64,([\w=+/]+)(\""[^>]*>)");
                    foreach (Match match in r.Matches(body))
                    {
                        Regex  g      = new Regex(@"data:image/(gif|png|jpeg);base64,([\w=+/]+)");
                        string src    = g.Match(match.Value).Value;
                        string base64 = Regex.Replace(src, "data:image/(gif|png|jpeg);base64,", string.Empty, RegexOptions.IgnoreCase);

                        var imageData = Convert.FromBase64String(base64);

                        var            contentId      = Guid.NewGuid().ToString();
                        LinkedResource linkedResource = new LinkedResource(new MemoryStream(imageData), "image/jpeg");
                        linkedResource.ContentId        = contentId;
                        linkedResource.TransferEncoding = TransferEncoding.Base64;

                        body = body.Replace(src, "cid:" + contentId);
                        listLinkedResource.Add(linkedResource);
                    }

                    AlternateView htmlView = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
                    listLinkedResource.ForEach(x => htmlView.LinkedResources.Add(x));
                    message.AlternateViews.Add(htmlView);

                    // add attachment
                    if (listAttachment != null && listAttachment.Length > 0)
                    {
                        foreach (string item in listAttachment)
                        {
                            if (File.Exists(item))
                            {
                                System.Net.Mail.Attachment att = AttachmentHelper.CreateAttachment(item, Path.GetFileName(item), TransferEncoding.Base64);
                                message.Attachments.Add(att);
                            }
                        }
                    }

                    // send mail
                    SmtpClient client = new SmtpClient(eMAIL_SEND_SERVER);
                    client.Credentials = new System.Net.NetworkCredential(eMAIL_SEND_USER, eMAIL_SEND_PASSWORD);
                    client.EnableSsl   = false;

                    client.Send(message);

                    foreach (System.Net.Mail.Attachment att in message.Attachments)
                    {
                        att.Dispose();
                    }

                    message.Dispose();
                }
                result = true;

                return(result);
            }
            catch (Exception ex)
            {
                WriteLogEmail2(from, to, cC, bCC);

                T2P._2015.Cross.Utility.ExceptionHandling.ExceptionHelper.GetDetailMessageAndLog(ex);
                return(false);
            }
        }