API
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Email.cs 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using UnivateProperties_API.Helpers;
  2. using System;
  3. using System.ComponentModel.DataAnnotations.Schema;
  4. using System.Net.Mail;
  5. using System.Text;
  6. using UnivateProperties_API.Model.Users;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. namespace UnivateProperties_API.Model.Communication
  10. {
  11. public class Email : BaseEntity
  12. {
  13. #region Constructor
  14. public Email()
  15. {
  16. }
  17. public Email(int senderId)
  18. {
  19. SenderId = senderId;
  20. }
  21. public Email(int senderId, string toAddress, string toDisplay, string cc, string bcc, string subject, string body, bool isHtml = true)
  22. {
  23. SenderId = senderId;
  24. To = toAddress;
  25. ToDisplay = toDisplay;
  26. BCC = bcc;
  27. CC = cc;
  28. IsBodyHtml = isHtml;
  29. Body = body;
  30. Subject = subject;
  31. }
  32. public Email(Template template, Person sendTo, List<BaseEntity> args)
  33. {
  34. if(sendTo != null && MyCommon.IsValidEmail(sendTo.Email))
  35. {
  36. if (template.SenderId != null)
  37. {
  38. SenderId = template.SenderId.Value;
  39. }
  40. Sender = template.Sender;
  41. To = sendTo.Email;
  42. ToDisplay = sendTo.FullName;
  43. BCC = ConcatEmails(template.AgentBCC, template.IndividualBCC);
  44. IsBodyHtml = true;
  45. Body = template.Body;
  46. Subject = template.Subject;
  47. foreach(var item in template.PlaceHolders)
  48. {
  49. foreach(var obj in args)
  50. {
  51. if(obj.GetType() == Type.GetType(item.BoundToClass))
  52. {
  53. string replaceValue = (string)obj[item.BoundTo];
  54. if(Body.Contains(item.Name))
  55. {
  56. Body = Body.Replace(item.Name, replaceValue);
  57. }
  58. if(Subject.Contains(item.Name))
  59. {
  60. Subject = Subject.Replace(item.Name, replaceValue);
  61. }
  62. }
  63. }
  64. }
  65. }
  66. }
  67. #endregion Constructor
  68. #region Properties
  69. [ForeignKey("Sender")]
  70. public int SenderId { get; set; }
  71. public string Comment { get; set; }
  72. public string Subject { get; set; }
  73. public bool IsBodyHtml { get; set; }
  74. public string CC { get; set; }
  75. public string BCC { get; set; }
  76. public string Body { get; set; }
  77. public string To { get; set; }
  78. public string ToDisplay { get; set; }
  79. public virtual SMTPAccount Sender { get; set; }
  80. #endregion
  81. #region Methods
  82. public virtual void GetSMTPAccount()
  83. {
  84. if (SenderId > 0)
  85. {
  86. }
  87. }
  88. public virtual bool SendMail()
  89. {
  90. try
  91. {
  92. if (MyCommon.IsValidEmail(To))
  93. {
  94. if (Sender != null && Sender.SMTPHost != null)
  95. {
  96. using (SmtpClient smtp = new SmtpClient(Sender.SMTPHost.Host))
  97. {
  98. MailMessage mail = GetMailMessage();
  99. smtp.UseDefaultCredentials = Sender.SMTPHost.NeedsAuthorize;
  100. smtp.Credentials = Sender.SMTPHost.GetNetworkCredential();
  101. smtp.EnableSsl = Sender.SMTPHost.UseSSL;
  102. smtp.Send(mail);
  103. Comment = "Send";
  104. return true;
  105. };
  106. }
  107. else Comment = "NoSender";
  108. }
  109. }
  110. catch (SmtpFailedRecipientException ex)
  111. {
  112. SmtpStatusCode statusCode = ex.StatusCode;
  113. if (statusCode == SmtpStatusCode.MailboxBusy ||
  114. statusCode == SmtpStatusCode.MailboxUnavailable ||
  115. statusCode == SmtpStatusCode.TransactionFailed)
  116. {
  117. Comment = $"SmtpFailedRecipientException - Msg - {ex.Message} Stack - {ex.StackTrace}";
  118. //TODO: Return correct error message
  119. return false;
  120. }
  121. else
  122. {
  123. Comment = $"SmtpFailedRecipientException - Msg - {ex.Message} Stack - {ex.StackTrace}";
  124. //TODO: Return correct error message
  125. return false;
  126. }
  127. }
  128. catch (Exception ex)
  129. {
  130. Comment = $"Exception - Msg - {ex.Message} Stack - {ex.StackTrace}";
  131. //TODO: Return correct error message
  132. return false;
  133. }
  134. return true;
  135. }
  136. private MailMessage GetMailMessage()
  137. {
  138. MailMessage mail = new MailMessage();
  139. mail.To.Add(new MailAddress(To, ToDisplay));
  140. if (!string.IsNullOrEmpty(CC))
  141. {
  142. foreach (var item in CC.Split(';'))
  143. {
  144. mail.CC.Add(item);
  145. }
  146. }
  147. if (!string.IsNullOrEmpty(BCC))
  148. {
  149. foreach (var item in BCC.Split(';'))
  150. {
  151. mail.Bcc.Add(item);
  152. }
  153. }
  154. mail.Subject = Subject;
  155. mail.Body = Body;
  156. mail.IsBodyHtml = IsBodyHtml;
  157. if (IsBodyHtml)
  158. {
  159. mail.BodyEncoding = Encoding.ASCII;
  160. }
  161. else mail.BodyEncoding = Encoding.UTF8;
  162. mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
  163. mail.Sender = new MailAddress(Sender.Address, Sender.DisplayName);
  164. mail.From = mail.Sender;
  165. return mail;
  166. }
  167. private string ConcatEmails(ICollection<Agent> agents, ICollection<Individual> individuals)
  168. {
  169. string value = string.Empty;
  170. if (agents != null && agents.Count > 0)
  171. {
  172. foreach(var item in agents)
  173. {
  174. if(MyCommon.IsValidEmail(item.Email))
  175. {
  176. value += $"{item.Email};";
  177. }
  178. }
  179. }
  180. if (individuals != null && individuals.Count > 0)
  181. {
  182. foreach (var item in individuals)
  183. {
  184. if (MyCommon.IsValidEmail(item.Email))
  185. {
  186. value += $"{item.Email};";
  187. }
  188. }
  189. }
  190. return value;
  191. }
  192. #endregion Methods
  193. }
  194. }