private EmailMessage CreateEmailMessage(Activity activity) { string body = activity.SafeGetColumnValue <string>("Body"); bool isHtmlBody = activity.SafeGetColumnValue <bool>("IsHtmlBody"); string title = activity.SafeGetColumnValue <string>("Title"); string from = GetSenderEmail(activity); string toValues = activity.SafeGetColumnValue <string>("Recepient"); string ccValues = activity.SafeGetColumnValue <string>("CopyRecepient"); string bccValue = activity.SafeGetColumnValue <string>("BlindCopyRecepient"); List <string> recipients = GetEmailAddresses(toValues); List <string> ccRecipients = GetEmailAddresses(ccValues); List <string> bccRecipients = GetEmailAddresses(bccValue); var emailPriority = EmailPriority.None; Guid priorityId = activity.SafeGetColumnValue <Guid>("PriorityId"); if (priorityId != Guid.Empty) { emailPriority = EmailPriorityConverter.GetEmailPriority(priorityId); } if (recipients.Count == 0 && ccRecipients.Count == 0 && bccRecipients.Count == 0) { throw new EmailException("ErrorOnSend", NoRecepientError); } var headerProperties = GetAdditionalHeaders(activity); Guid activityId = activity.PrimaryColumnValue; List <EmailAttachment> attachments = GetAttachments(activityId); var emailMessage = new EmailMessage { From = from, To = recipients, Cc = ccRecipients, Bcc = bccRecipients, Subject = title, Body = body, Attachments = attachments, Priority = emailPriority, IsHtmlBody = isHtmlBody, HeaderProperties = headerProperties }; return(emailMessage); }
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 } } }