コード例 #1
0
        public virtual string SendNewsletterToSingleUser(string emailTemplateId, string email, string emailSubject, string body, string attachmentFile)
        {
            //DEBUG
            //var debugHelper = new EmailDebugHelper();
            //if (debugHelper.IsBlockedEmail(email))
            //{
            //    return "User is not in allowed list";
            //}
            ////DEBUG

            var user = UserAdminRepository.GetUserByEmail(email);

            if (user == null)
            {
                return(Email.UserNotFound);
            }



            var mailSubject = emailSubject;
            var isBodyHtml  = true;
            var mailBody    = body;

            mailBody = mailBody.Replace(Email.UserId, user.Id)
                       .Replace(Email.FirstName, user.FirstName)
                       .Replace(Email.LastName, user.LastName)
                       .Replace(Email.Password, ConfigurationManager.AppSettings[Email.DefaultPassword])
                       .Replace(Email.EmailAddress, user.Email);

            var mailMessage = new MailMessage(MailFrom, email);

            //Embed images//
            EmbedImage(mailBody, emailTemplateId, user, mailMessage);
            //Embed images//

            mailMessage.Subject    = mailSubject;
            mailMessage.IsBodyHtml = isBodyHtml;


            if (!string.IsNullOrEmpty(attachmentFile))
            {
                var attachment = new Attachment(MyHttpServerUtility.MapPath("~/" + attachmentFile));
                attachment.Name = attachmentFile;
                mailMessage.Attachments.Add(attachment);
            }

            try
            {
                EmailRepository.SendMail(mailMessage);
            }
            catch (Exception exception)
            {
                IMS.Logger.Logger.Fatal(exception.Message, exception);
                return(exception.Message);
            }
            return(Email.Success);
        }
コード例 #2
0
        protected virtual void EmbedImage(string mailBody, string emailTemplateId, ApplicationUser user, MailMessage mailMessage)
        {
            //Embed images//

            Regex           reImg           = new Regex(@"<img\s[^>]*>", RegexOptions.IgnoreCase);
            Regex           reSrc           = new Regex(@"src=(?:(['""])(?<src>(?:(?!\1).)*)\1|(?<src>\S+))", RegexOptions.IgnoreCase | RegexOptions.Singleline);
            MatchCollection matchCollection = reImg.Matches(mailBody);

            foreach (Match match in matchCollection)
            {
                if (match.Groups[0].Value.Contains(Email.EmailReadReceiptToken))
                {
                    var queryString = "u=" + user.Id;
                    queryString += "&" + "t=" + emailTemplateId;
                    queryString += "&" + "d=" + DateTime.Now.ToString("MM-dd-yyyy");
                    queryString += "&" + "i=" + Guid.NewGuid();

                    var applicationUrl = MyRequest.UrlReferrer.OriginalString;
                    applicationUrl = String.Format("http://{0}{1}", MyRequest.UrlReferrer.Authority, MyRequest.UrlReferrer.AbsolutePath);

                    mailBody = mailBody.Replace(match.Groups[0].Value, "<img alt=\"image\" style=\"width:1px;height:1px;\" width=\"1\" height=\"1\" src=\"" + applicationUrl + Email.EmailReadReceiptToken + ".png" + "?" + queryString) + "\" />";
                    break;
                }
                string src = string.Empty;

                if (reSrc.IsMatch(mailBody))
                {
                    Match mSrc = reSrc.Match(match.Groups[0].Value);

                    src = mSrc.Groups["src"].Value;
                }

                string imagePath = src;

                string attachmentID = Path.GetFileName(imagePath).Replace(".", "") + "@jnj";

                try
                {
                    Attachment attachmentImage = new Attachment(MyHttpServerUtility.MapPath("~/" + imagePath));
                    attachmentImage.ContentDisposition.Inline = true;
                    attachmentImage.ContentId = attachmentID;
                    mailMessage.Attachments.Add(attachmentImage);

                    mailBody = mailBody.Replace(src, "cid:" + attachmentID);
                }
                catch (FileNotFoundException exception)
                {
                    IMS.Logger.Logger.Fatal(exception.Message, exception);
                    throw;
                }
            }
            mailMessage.Body = mailBody;
            //Embed images//
        }