| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 | 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, 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);
                                }
                            }
                        }
                    }
                }*//*
            }
        }*/
        #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<Agent> agents, ICollection<Individual> 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
    }
}
 |