示例#1
0
        private static string ReplaceRegistrationUrl(UserConnection userConnection, Guid contactId,
                                                     string confirmRegistrationUrl, Entity template)
        {
            var body       = template.GetTypedColumnValue <string>("Body").Replace("#RegistrationUrl#", confirmRegistrationUrl);
            var macros     = template.GetBytesValue("Macros");
            var macrosList = Json.Deserialize <List <DictionaryEntry> >(Encoding.UTF8.GetString(macros));

            try {
                body = EmailTemplateUtility.ReplaceRecipientMacrosText(body, contactId, macrosList, userConnection);
            } catch { }
            return(body);
        }
示例#2
0
        public virtual string ProcessEmailBody(string body, Guid contactId, byte[] macrosBytes)
        {
            var url = GetVisaLink();

            body = string.Format(body, url);

            var visaEntitySchemaQuery = GetVisaTemplateEntitySchemaQuery();
            var visaEntityCollection  = visaEntitySchemaQuery.GetEntityCollection(UserConnection);

            if (visaEntityCollection.Count == 0)
            {
                return(body);
            }
            var visaEntity   = visaEntityCollection[0];
            var addedColumns = (List <EntitySchemaQueryColumn>)AddedTemplateColumns;

            StringBuilder sbBody = new StringBuilder(body);

            foreach (EntitySchemaQueryColumn esqColumn in addedColumns)
            {
                var value       = visaEntity.GetColumnValue(esqColumn.Name);
                var stringValue = value is DateTime ? ((DateTime)value).ToShortDateString() : value.ToString();
                var caption     = esqColumn.Caption.ToString();
                if (String.IsNullOrEmpty(stringValue))
                {
                    var   reqexp = string.Format("#DeleteIfEmpty#([^#]*?)\\[#{0}#\\]([^#]*?)#\\/DeleteIfEmpty#", caption);
                    Regex r      = new Regex(reqexp);
                    foreach (Match match in r.Matches(body))
                    {
                        sbBody.Replace(match.Value, string.Empty);
                    }
                }
                sbBody.Replace(string.Format("[#{0}#]", esqColumn.Caption.ToString()), stringValue);
            }
            sbBody.Replace("#DeleteIfEmpty#", string.Empty);
            sbBody.Replace("#/DeleteIfEmpty#", string.Empty);
            sbBody.Insert(0, "<div style=\"font-family: 'Verdana';\">");
            sbBody.Append("</div>");
            body = sbBody.ToString();
            var macrosList = Json.Deserialize <List <DictionaryEntry> >(System.Text.Encoding.UTF8.GetString(macrosBytes));

            body = EmailTemplateUtility.ReplaceRecipientMacrosText(body, contactId, macrosList, UserConnection);
            return(body);
        }
示例#3
0
        public override void Execute(PageBase page)
        {
            var smtpclient = new SmtpClient();

            page.Response.Output.WriteLine(
                "Sending e-mail message to {0}",
                EmailAddress);

            page.Response.Output.WriteLine("Delivery method: {0}", smtpclient.DeliveryMethod);
            page.Response.Output.WriteLine("Server: {0}:{1}", smtpclient.Host, smtpclient.Port);

            var subject = String.Format("{0} test message from {1}", page.Domain.ShortTitle, Environment.MachineName);
            var body    = "Test message, please ignore.";

            var msg = EmailTemplateUtility.CreateMessage(
                page.Domain.Email, page.Domain.ShortTitle,
                EmailAddress, EmailAddress,
                subject, body);

            EmailTemplateUtility.SendMessage(msg);

            page.Response.Output.WriteLine("E-mail message sent.");
        }
示例#4
0
        public virtual void SendEmail(Dictionary <Guid, string> recepients)
        {
            object useEmails;

            if (Terrasoft.Core.Configuration.SysSettings.TryGetValue(UserConnection, "SendVisaEmail", out useEmails))
            {
                if (useEmails is bool && !((bool)useEmails))
                {
                    return;
                }
            }
            var tempalteId = GetEmailTemplateId();

            if (tempalteId == Guid.Empty)
            {
                return;
            }
            var esq = new EntitySchemaQuery(UserConnection.EntitySchemaManager, "EmailTemplate");

            esq.AddColumn("Subject");
            esq.AddColumn("Body");
            esq.AddColumn("IsHtmlBody");
            esq.AddColumn("Macros");
            var template = (Terrasoft.Configuration.EmailTemplate)esq.GetEntity(UserConnection, tempalteId);

            if (template == null)
            {
                return;
            }
            var credentials = new MailCredentials();

            credentials.Host         = (string)Terrasoft.Core.Configuration.SysSettings.GetValue(UserConnection, "SmtpHost");
            credentials.Port         = int.Parse(Terrasoft.Core.Configuration.SysSettings.GetValue(UserConnection, "SmtpPort").ToString());
            credentials.UseSsl       = (bool)Terrasoft.Core.Configuration.SysSettings.GetValue(UserConnection, "SmtpEnableSsl");
            credentials.UserName     = (string)Terrasoft.Core.Configuration.SysSettings.GetValue(UserConnection, "SmtpUserName");
            credentials.UserPassword = (string)Terrasoft.Core.Configuration.SysSettings.GetValue(UserConnection, "SmtpUserPassword");
            if (string.IsNullOrEmpty(credentials.Host) || string.IsNullOrEmpty(credentials.UserName))
            {
                return;
            }
            foreach (var recepient in recepients)
            {
                if (string.IsNullOrEmpty(recepient.Value))
                {
                    continue;
                }
                var priorityId   = Guid.Parse("ab96fa02-7fe6-df11-971b-001d60e938c6");
                var body         = ProcessEmailBody(template.Body, recepient.Key, template.GetBytesValue("Macros"));
                var attachments  = EmailTemplateUtility.GetEmailTemplateAttachments(UserConnection, tempalteId);
                var emailMessage = new EmailMessage {
                    From = credentials.UserName,
                    To   = new List <string> {
                        recepient.Value
                    },
                    Subject     = template.Subject,
                    Body        = body,
                    Priority    = EmailPriorityConverter.GetEmailPriority(priorityId),
                    IsHtmlBody  = template.IsHtmlBody,
                    Attachments = attachments,
                };
                try {
                    var          emailClientFactory = ClassFactory.Get <EmailClientFactory>(new ConstructorArgument("userConnection", UserConnection));
                    IEmailSender emailSender        = ClassFactory.Get <IEmailSender>(
                        new ConstructorArgument("emailClientFactory", emailClientFactory),
                        new ConstructorArgument("userConnection", UserConnection));
                    emailSender.Send(emailMessage, credentials);
                } catch (SmtpException e) {
                    // TODO: log exception
                }
            }
        }
 public EmailService(EmailTemplateUtility responseBuilder)
 {
     _responseBuilder = responseBuilder;
 }
示例#6
0
        protected void Ok_Click(object sender, EventArgs e)
        {
            string    templatefile;
            Exception ex      = null;
            long      eventid = 0;

            if (mode == Mode.Error && Session[Constants.SessionException] != null)
            {
                ex      = (Exception)Session[Constants.SessionException];
                eventid = (long)Session[Constants.SessionExceptionEventID];

                templatefile = "~/Templates/ErrorFeedbackEmail.xml";
            }
            else if (mode == Mode.JobError)
            {
                templatefile = "~/Templates/ErrorFeedbackEmail.xml";
            }
            else
            {
                templatefile = "~/Templates/FeedbackEmail.xml";
            }

            var    template = File.ReadAllText(MapPath(templatefile));
            string subject;
            string body;

            EmailTemplateUtility.LoadEmailTemplate(template, out subject, out body);

            EmailTemplateUtility.ReplaceEmailToken(ref subject, "[$ShortTitle]", Federation.ShortTitle);
            EmailTemplateUtility.ReplaceEmailToken(ref subject, "[$Subject]", Subject.Text);

            EmailTemplateUtility.ReplaceEmailToken(ref body, "[$ShortTitle]", Federation.ShortTitle);
            EmailTemplateUtility.ReplaceEmailToken(ref body, "[$Name]", Name.Text);
            EmailTemplateUtility.ReplaceEmailToken(ref body, "[$Email]", Email.Text);
            EmailTemplateUtility.ReplaceEmailToken(ref body, "[$Subject]", Subject.Text);
            EmailTemplateUtility.ReplaceEmailToken(ref body, "[$Comments]", Comments.Text);

            if (mode == Mode.Error && Session[Constants.SessionException] != null)
            {
                EmailTemplateUtility.ReplaceEmailToken(ref body, "[$ErrorMessage]", ex.Message);
                EmailTemplateUtility.ReplaceEmailToken(ref body, "[$JobGUID]", "-");
                EmailTemplateUtility.ReplaceEmailToken(ref body, "[$EventID]", eventid.ToString());
            }
            else if (mode == Mode.JobError)
            {
                var job = new JobInstance(RegistryContext);
                job.Guid = jobGuid;
                job.Load();

                EmailTemplateUtility.ReplaceEmailToken(ref body, "[$ErrorMessage]", job.ExceptionMessage);
                EmailTemplateUtility.ReplaceEmailToken(ref body, "[$JobGUID]", job.Guid.ToString());
                EmailTemplateUtility.ReplaceEmailToken(ref body, "[$EventID]", "-");
            }

            var msg = EmailTemplateUtility.CreateMessage(
                Federation.Email, Federation.ShortTitle,
                Federation.Email, Federation.ShortTitle,
                subject, body);

            // Append stack trace
            if (mode == Mode.Error && Session[Constants.SessionException] != null)
            {
                System.Net.Mail.Attachment att;

                using (var mem = new MemoryStream(Encoding.UTF8.GetBytes(new HttpUnhandledException(ex.Message, ex).GetHtmlErrorMessage())))
                {
                    att = new System.Net.Mail.Attachment(mem, "error.html");

                    msg.Attachments.Add(att);
                    EmailTemplateUtility.SendMessage(msg);
                }
            }
            else
            {
                EmailTemplateUtility.SendMessage(msg);
            }

            FeedbackForm.Visible = false;
            SuccessForm.Visible  = true;
        }