Beispiel #1
0
        private void EmbedLogo(EmailDetail emailDetail, string pattern, string iisLogo, List <LinkedResource> resourceCollection)
        {
            Regex rgImage = new Regex("<img(?=[^>]+class=\"" + pattern + "\")[^>]+src=\"([^\">]+)\"");
            Regex rgScr   = new Regex("src=\"([^\">]+)\"");

            if (!string.IsNullOrWhiteSpace(Path.GetFileName(iisLogo)))
            {
                foreach (Match match in rgImage.Matches(emailDetail.Body))
                {
                    emailDetail.Body = emailDetail.Body.Replace(iisLogo, "cid:" + Path.GetFileName(iisLogo));
                    string src          = rgScr.Match(match.Value).Value;
                    string folderPath   = string.Empty;
                    string saveLocation = string.Empty;
                    if ((pattern == "CompanyLogo" || pattern == "CompanySignature") && !string.IsNullOrWhiteSpace(emailDetail.IISPathCompany))
                    {
                        folderPath   = Path.GetDirectoryName(iisLogo.Replace(emailDetail.IISPathCompany, string.Empty));
                        saveLocation = emailDetail.IPLocalPathCompany + folderPath + "\\" + Path.GetFileName(iisLogo);
                    }
                    else
                    {
                        folderPath   = Path.GetDirectoryName(iisLogo.Replace(emailDetail.IISPath, string.Empty));
                        saveLocation = emailDetail.IpLocalPath + folderPath + "\\" + Path.GetFileName(iisLogo);
                    }
                    LinkedResource EmbeddedObjects1 = new LinkedResource(saveLocation);
                    EmbeddedObjects1.ContentId = Path.GetFileName(iisLogo);
                    ContentType ct = new ContentType(MediaTypeNames.Image.Gif);
                    EmbeddedObjects1.ContentType = ct;
                    resourceCollection.Add(EmbeddedObjects1);
                }
            }
        }
Beispiel #2
0
        public virtual bool SendMailMessage(EmailDetail emailDetail)
        {
            try
            {
                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, "*****@*****.**");

                if (validateSendEmail)
                {
                    message.Headers.Add("Message-ID", "<" + emailDetail.MessageID + ">");
                    message.From = new MailAddress(emailDetail.FromAddress);

                    message.Subject    = emailDetail.Subject;
                    message.Body       = emailDetail.Body;
                    message.IsBodyHtml = true;

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

                    message.Dispose();
                }
                return(true);
            }
            catch (Exception ex)
            {
                WriteLog(emailDetail);
                T2P._2015.Cross.Utility.ExceptionHandling.ExceptionHelper.GetDetailMessageAndLog(ex);
                return(false);
            }
        }
Beispiel #3
0
        public bool ResultSendEmail(EmailDetail emailDetail)
        {
            bool resultSendMail = false;

            for (int i = 0; i < 3; i++)
            {
                resultSendMail = SendMailMessageWithDynamicLogo(emailDetail);
                if (resultSendMail == true)
                {
                    break;
                }
            }
            return(resultSendMail);
        }
Beispiel #4
0
 private void WriteLog(EmailDetail emailDetail)
 {
     try
     {
         string cc   = !string.IsNullOrWhiteSpace(emailDetail.EmailCC) ? emailDetail.EmailCC : string.Empty;
         string bcc  = !string.IsNullOrWhiteSpace(emailDetail.EmailBcc) ? emailDetail.EmailBcc : string.Empty;
         string from = !string.IsNullOrWhiteSpace(emailDetail.FromAddress) ? emailDetail.FromAddress : string.Empty;
         string to   = emailDetail.ListTo != null && emailDetail.ListTo.Any() ? string.Join(",", emailDetail.ListTo) : string.Empty;
         string Log  = "\nFrom: " + from
                       + "\nTo: " + to
                       + "\nCC: " + cc
                       + "\nBCC: " + bcc;
         T2P._2015.Cross.Utility.Log.Logging.Error(Log);
     }
     catch (Exception ex)
     {
         T2P._2015.Cross.Utility.ExceptionHandling.ExceptionHelper.GetDetailMessageAndLog(ex);
     }
 }
Beispiel #5
0
 private void WriteLogEmail2(string from, string to, string cC, string bCC)
 {
     try
     {
         EmailDetail emailDetail = new EmailDetail()
         {
             FromAddress = from,
             ListTo      = new List <string>()
             {
                 to
             },
             EmailCC  = cC,
             EmailBcc = bCC,
         };
         WriteLog(emailDetail);
     }
     catch (Exception ex)
     {
         T2P._2015.Cross.Utility.ExceptionHandling.ExceptionHelper.GetDetailMessageAndLog(ex);
     }
 }
Beispiel #6
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);
            }
        }