using UnivateProperties_API.Helpers; using System; using System.ComponentModel.DataAnnotations.Schema; using System.Net.Mail; using System.Text; using UnivateProperties_API.Model.Users; using System.Collections; using System.Collections.Generic; using UnivateProperties_API.Helpers.Communication; using UnivateProperties_API.Containers.Timeshare; using UnivateProperties_API.Context; namespace UnivateProperties_API.Model.Communication { public class Email : BaseEntity { private readonly DataContext _dbContext; #region Constructor public Email() { } public Email(int senderId) { SenderId = senderId; } public Email(int senderId, string toAddress, string toDisplay, string cc, string bcc, string subject, string body, bool isHtml = true) { SenderId = senderId; To = toAddress; ToDisplay = toDisplay; BCC = bcc; CC = cc; IsBodyHtml = isHtml; Body = body; Subject = subject; } public Email(Template template, Person sendTo, TimeshareWeekDto sellItem, DataContext dataContext) { if(sendTo != null && MyCommon.IsValidEmail(sendTo.Email)) { if (template.SenderId != null) { SenderId = template.SenderId.Value; } Sender = template.Sender; To = "jplouw@provision-sa.com"; // sendTo.Email; ToDisplay = sendTo.FullName; /*BCC = ConcatEmails(template.AgentBCC, template.IndividualBCC);*/ IsBodyHtml = true; EmailCodedFieldsPopulator emailCodedFieldsPopulator = new EmailCodedFieldsPopulator(dataContext); Body = emailCodedFieldsPopulator.getCodedBody(template.Body, sendTo, sellItem); Subject = template.Subject; /* foreach (var item in template.PlaceHolders) { foreach(var obj in args) { if(obj != null) { var something = obj.GetType(); if (obj.GetType() == Type.GetType(item.BoundToClass) || obj.GetType().IsSubclassOf(Type.GetType(item.BoundToClass))) { string replaceValue = (string)obj[item.BoundTo]; if (Body.Contains(item.Name)) { Body = Body.Replace(item.Name, replaceValue); } if (Subject.Contains(item.Name)) { Subject = Subject.Replace(item.Name, replaceValue); } } } } }*/ } } public Email(Template template, Person sendTo, List args) { if (sendTo != null && MyCommon.IsValidEmail(sendTo.Email)) { if (template.SenderId != null) { SenderId = template.SenderId.Value; } Sender = template.Sender; To = "jplouw@provision-sa.com"; // sendTo.Email; ToDisplay = sendTo.FullName; BCC = ConcatEmails(template.AgentBCC, template.IndividualBCC); IsBodyHtml = true; /*EmailCodedFieldsPopulator emailCodedFieldsPopulator = new EmailCodedFieldsPopulator(dataContext);*/ Body = template.Body; Subject = template.Subject; foreach (var item in template.PlaceHolders) { foreach (var obj in args) { if (obj != null) { var something = obj.GetType(); if (obj.GetType() == Type.GetType(item.BoundToClass) || obj.GetType().IsSubclassOf(Type.GetType(item.BoundToClass))) { string replaceValue = (string)obj[item.BoundTo]; if (Body.Contains(item.Name)) { Body = Body.Replace(item.Name, replaceValue); } if (Subject.Contains(item.Name)) { Subject = Subject.Replace(item.Name, replaceValue); } } } } } } } #endregion Constructor #region Properties [ForeignKey("Sender")] public int SenderId { get; set; } public string Comment { get; set; } public string Subject { get; set; } public bool IsBodyHtml { get; set; } public string CC { get; set; } public string BCC { get; set; } public string Body { get; set; } public string To { get; set; } public string ToDisplay { get; set; } public virtual SMTPAccount Sender { get; set; } #endregion #region Methods public virtual void GetSMTPAccount() { if (SenderId > 0) { } } public virtual bool SendMail() { try { if (MyCommon.IsValidEmail(To)) { if (Sender != null && Sender.SMTPHost != null) { using (SmtpClient smtp = new SmtpClient(Sender.SMTPHost.Host)) { MailMessage mail = GetMailMessage(); smtp.UseDefaultCredentials = Sender.SMTPHost.NeedsAuthorize; smtp.Credentials = Sender.SMTPHost.GetNetworkCredential(); smtp.EnableSsl = Sender.SMTPHost.UseSSL; smtp.Send(mail); Comment = "Send"; return true; }; } else Comment = "NoSender"; } } catch (SmtpFailedRecipientException ex) { SmtpStatusCode statusCode = ex.StatusCode; if (statusCode == SmtpStatusCode.MailboxBusy || statusCode == SmtpStatusCode.MailboxUnavailable || statusCode == SmtpStatusCode.TransactionFailed) { Comment = $"SmtpFailedRecipientException - Msg - {ex.Message} Stack - {ex.StackTrace}"; //TODO: Return correct error message return false; } else { Comment = $"SmtpFailedRecipientException - Msg - {ex.Message} Stack - {ex.StackTrace}"; //TODO: Return correct error message return false; } } catch (Exception ex) { Comment = $"Exception - Msg - {ex.Message} Stack - {ex.StackTrace}"; //TODO: Return correct error message return false; } return true; } private MailMessage GetMailMessage() { MailMessage mail = new MailMessage(); mail.To.Add(new MailAddress(To, ToDisplay)); if (!string.IsNullOrEmpty(CC)) { foreach (var item in CC.Split(';')) { mail.CC.Add(item); } } if (!string.IsNullOrEmpty(BCC)) { foreach (var item in BCC.Split(';')) { mail.Bcc.Add(item); } } mail.Subject = Subject; mail.Body = Body; mail.IsBodyHtml = IsBodyHtml; if (IsBodyHtml) { mail.BodyEncoding = Encoding.ASCII; } else mail.BodyEncoding = Encoding.UTF8; mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure; mail.Sender = new MailAddress(Sender.Address, Sender.DisplayName); mail.From = mail.Sender; return mail; } private string ConcatEmails(ICollection agents, ICollection individuals) { string value = string.Empty; if (agents != null && agents.Count > 0) { foreach(var item in agents) { if(MyCommon.IsValidEmail(item.Email)) { value += $"{item.Email};"; } } } if (individuals != null && individuals.Count > 0) { foreach (var item in individuals) { if (MyCommon.IsValidEmail(item.Email)) { value += $"{item.Email};"; } } } return value; } #endregion Methods } }