コード例 #1
0
ファイル: MessageService.cs プロジェクト: cong1389/KuteShop
        public virtual CreateMessageResult CreateMessage(MessageContext messageContext, bool queue,
                                                         params object[] modelParts)
        {
            ValidateMessageContext(messageContext, ref modelParts);

            // Create and assign model
            var model = messageContext.Model = new TemplateModel();

            // Add all global template model parts
            _modelProvider.AddGlobalModelParts(messageContext);

            // Add specific template models for passed parts
            foreach (var part in modelParts)
            {
                _modelProvider.AddModelPart(part, messageContext);
            }

            var messageTemplate = messageContext.MessageTemplate;

            // Render templates
            //var to = RenderEmailAddress(messageTemplate.To, messageContext);
            //var replyTo = RenderEmailAddress(messageTemplate.ReplyTo, messageContext, false);
            //var bcc = RenderTemplate(messageTemplate.BccEmailAddresses, messageContext, false);

            var subject = RenderTemplate(messageTemplate.Subject, messageContext);

            ((dynamic)model).Email.Subject = subject;

            var body = RenderBodyTemplate(messageContext);

            // CSS inliner
            body = InlineCss(body);

            var mail = new SendMail
            {
                Subject   = subject,
                Body      = body,
                MessageId = "mail",
                ToEmail   = messageContext.EmailAccount.FromAddress
            };

            _sendMailService.SendMailSmtp(mail);

            return(new CreateMessageResult {
                MessageContext = messageContext
            });
        }
コード例 #2
0
        public ActionResult SendContact(ContactForm model)
        {
            try
            {
                if (!Request.IsAjaxRequest())
                {
                    return(Json(new
                    {
                        title = "Gửi liên hệ",
                        success = false,
                        errors = string.Join(", ", ModelState.Values.SelectMany(v =>
                                                                                from x in v.Errors
                                                                                select x.ErrorMessage).ToArray())
                    }));
                }

                string serverPathEmail = string.Concat(Server.MapPath("\\"), Constant.TemplateMailBasicContact);
                if (System.IO.File.Exists(serverPathEmail))
                {
                    const string messageId = "Email";
                    string       subject   = "Thông tin liên hệ";

                    var body = string.Concat("", "Người gửi: ", model.FullName, "<br>");
                    body = string.Concat(body, string.Concat("E-mail: ", model.Email), "<br>");
                    body = string.Concat(body, model.Content, "<br>");

                    XmlDocument xmlDocument = new XmlDocument();
                    xmlDocument.Load(serverPathEmail);
                    XmlNode xmlNodes = xmlDocument.SelectSingleNode(string.Concat("MailFormats/MailFormat[@Id='", messageId, "']"));

                    string subjectNodes = xmlNodes.SelectSingleNode("Subject")?.InnerText;
                    subjectNodes = subject.Replace(string.Concat(subject, "?"), subject);

                    string bodyNodes = xmlNodes.SelectSingleNode("Body")?.InnerText;
                    bodyNodes = body.Replace(string.Concat(body, "?"), body);

                    var toEmail = _systemSettingService.Get(x => x.Status == 1).Email;

                    var sendMail = new SendMail
                    {
                        MessageId = "Email",
                        ToEmail   = toEmail,
                        Subject   = subjectNodes,
                        Body      = bodyNodes
                    };

                    _sendMailService.SendMailSmtp(sendMail);

                    model.SuccessfullySent = true;
                    model.Result           = "Gửi liên hệ thành công, chúng tôi sẽ liên lạc với bạn ngay khi có thể.";
                }

                return(Json(new
                {
                    title = "Gửi liên hệ",
                    success = true,
                    message = "Gửi liên hệ thành công, chúng tôi sẽ liên lạc với bạn ngay khi có thể."
                }));
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }