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 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 != null)
  52. {
  53. if (obj.GetType() == Type.GetType(item.BoundToClass) || obj.GetType().IsSubclassOf(Type.GetType(item.BoundToClass)))
  54. {
  55. string replaceValue = (string)obj[item.BoundTo];
  56. if (Body.Contains(item.Name))
  57. {
  58. Body = Body.Replace(item.Name, replaceValue);
  59. }
  60. if (Subject.Contains(item.Name))
  61. {
  62. Subject = Subject.Replace(item.Name, replaceValue);
  63. }
  64. }
  65. }
  66. }
  67. }
  68. }
  69. }
  70. #endregion Constructor
  71. #region Properties
  72. [ForeignKey("Sender")]
  73. public int SenderId { get; set; }
  74. public string Comment { get; set; }
  75. public string Subject { get; set; }
  76. public bool IsBodyHtml { get; set; }
  77. public string CC { get; set; }
  78. public string BCC { get; set; }
  79. public string Body { get; set; }
  80. public string To { get; set; }
  81. public string ToDisplay { get; set; }
  82. public virtual SMTPAccount Sender { get; set; }
  83. #endregion
  84. #region Methods
  85. public virtual void GetSMTPAccount()
  86. {
  87. if (SenderId > 0)
  88. {
  89. }
  90. }
  91. public virtual bool SendMail()
  92. {
  93. try
  94. {
  95. if (MyCommon.IsValidEmail(To))
  96. {
  97. if (Sender != null && Sender.SMTPHost != null)
  98. {
  99. using (SmtpClient smtp = new SmtpClient(Sender.SMTPHost.Host))
  100. {
  101. MailMessage mail = GetMailMessage();
  102. smtp.UseDefaultCredentials = Sender.SMTPHost.NeedsAuthorize;
  103. smtp.Credentials = Sender.SMTPHost.GetNetworkCredential();
  104. smtp.EnableSsl = Sender.SMTPHost.UseSSL;
  105. smtp.Send(mail);
  106. Comment = "Send";
  107. return true;
  108. };
  109. }
  110. else Comment = "NoSender";
  111. }
  112. }
  113. catch (SmtpFailedRecipientException ex)
  114. {
  115. SmtpStatusCode statusCode = ex.StatusCode;
  116. if (statusCode == SmtpStatusCode.MailboxBusy ||
  117. statusCode == SmtpStatusCode.MailboxUnavailable ||
  118. statusCode == SmtpStatusCode.TransactionFailed)
  119. {
  120. Comment = $"SmtpFailedRecipientException - Msg - {ex.Message} Stack - {ex.StackTrace}";
  121. //TODO: Return correct error message
  122. return false;
  123. }
  124. else
  125. {
  126. Comment = $"SmtpFailedRecipientException - Msg - {ex.Message} Stack - {ex.StackTrace}";
  127. //TODO: Return correct error message
  128. return false;
  129. }
  130. }
  131. catch (Exception ex)
  132. {
  133. Comment = $"Exception - Msg - {ex.Message} Stack - {ex.StackTrace}";
  134. //TODO: Return correct error message
  135. return false;
  136. }
  137. return true;
  138. }
  139. private MailMessage GetMailMessage()
  140. {
  141. MailMessage mail = new MailMessage();
  142. mail.To.Add(new MailAddress(To, ToDisplay));
  143. if (!string.IsNullOrEmpty(CC))
  144. {
  145. foreach (var item in CC.Split(';'))
  146. {
  147. mail.CC.Add(item);
  148. }
  149. }
  150. if (!string.IsNullOrEmpty(BCC))
  151. {
  152. foreach (var item in BCC.Split(';'))
  153. {
  154. mail.Bcc.Add(item);
  155. }
  156. }
  157. mail.Subject = Subject;
  158. mail.Body = Body;
  159. mail.IsBodyHtml = IsBodyHtml;
  160. if (IsBodyHtml)
  161. {
  162. mail.BodyEncoding = Encoding.ASCII;
  163. }
  164. else mail.BodyEncoding = Encoding.UTF8;
  165. mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
  166. mail.Sender = new MailAddress(Sender.Address, Sender.DisplayName);
  167. mail.From = mail.Sender;
  168. return mail;
  169. }
  170. private string ConcatEmails(ICollection<Agent> agents, ICollection<Individual> individuals)
  171. {
  172. string value = string.Empty;
  173. if (agents != null && agents.Count > 0)
  174. {
  175. foreach(var item in agents)
  176. {
  177. if(MyCommon.IsValidEmail(item.Email))
  178. {
  179. value += $"{item.Email};";
  180. }
  181. }
  182. }
  183. if (individuals != null && individuals.Count > 0)
  184. {
  185. foreach (var item in individuals)
  186. {
  187. if (MyCommon.IsValidEmail(item.Email))
  188. {
  189. value += $"{item.Email};";
  190. }
  191. }
  192. }
  193. return value;
  194. }
  195. #endregion Methods
  196. }
  197. }