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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. using UnivateProperties_API.Helpers.Communication;
  10. using UnivateProperties_API.Containers.Timeshare;
  11. using UnivateProperties_API.Context;
  12. namespace UnivateProperties_API.Model.Communication
  13. {
  14. public class Email : BaseEntity
  15. {
  16. private readonly DataContext _dbContext;
  17. #region Constructor
  18. public Email()
  19. {
  20. }
  21. public Email(int senderId)
  22. {
  23. SenderId = senderId;
  24. }
  25. public Email(int senderId, string toAddress, string toDisplay, string cc, string bcc, string subject, string body, bool isHtml = true)
  26. {
  27. SenderId = senderId;
  28. To = toAddress;
  29. ToDisplay = toDisplay;
  30. BCC = bcc;
  31. CC = cc;
  32. IsBodyHtml = isHtml;
  33. Body = body;
  34. Subject = subject;
  35. }
  36. public Email(Template template, Person sendTo, TimeshareWeekDto sellItem, DataContext dataContext)
  37. {
  38. if(sendTo != null && MyCommon.IsValidEmail(sendTo.Email))
  39. {
  40. if (template.SenderId != null)
  41. {
  42. SenderId = template.SenderId.Value;
  43. }
  44. Sender = template.Sender;
  45. To = "jplouw@provision-sa.com"; // sendTo.Email;
  46. ToDisplay = sendTo.FullName;
  47. /*BCC = ConcatEmails(template.AgentBCC, template.IndividualBCC);*/
  48. IsBodyHtml = true;
  49. EmailCodedFieldsPopulator emailCodedFieldsPopulator = new EmailCodedFieldsPopulator(dataContext);
  50. Body = emailCodedFieldsPopulator.getCodedBody(template.Body, sendTo, sellItem);
  51. Subject = template.Subject;
  52. /* foreach (var item in template.PlaceHolders)
  53. {
  54. foreach(var obj in args)
  55. {
  56. if(obj != null)
  57. {
  58. var something = obj.GetType();
  59. if (obj.GetType() == Type.GetType(item.BoundToClass) || obj.GetType().IsSubclassOf(Type.GetType(item.BoundToClass)))
  60. {
  61. string replaceValue = (string)obj[item.BoundTo];
  62. if (Body.Contains(item.Name))
  63. {
  64. Body = Body.Replace(item.Name, replaceValue);
  65. }
  66. if (Subject.Contains(item.Name))
  67. {
  68. Subject = Subject.Replace(item.Name, replaceValue);
  69. }
  70. }
  71. }
  72. }
  73. }*/
  74. }
  75. }
  76. /*public Email(Template template, Person sendTo, TimeshareWeekDto sellItem, DataContext dataContext)
  77. {
  78. if (sendTo != null && MyCommon.IsValidEmail(sendTo.Email))
  79. {
  80. if (template.SenderId != null)
  81. {
  82. SenderId = template.SenderId.Value;
  83. }
  84. Sender = template.Sender;
  85. To = "jplouw@provision-sa.com"; // sendTo.Email;
  86. ToDisplay = sendTo.FullName;
  87. *//*BCC = ConcatEmails(template.AgentBCC, template.IndividualBCC);*//*
  88. IsBodyHtml = true;
  89. EmailCodedFieldsPopulator emailCodedFieldsPopulator = new EmailCodedFieldsPopulator(dataContext);
  90. Body = emailCodedFieldsPopulator.getCodedBody(template.Body, sendTo, sellItem);
  91. Subject = template.Subject;
  92. *//*foreach (var item in template.PlaceHolders)
  93. {
  94. foreach (var obj in args)
  95. {
  96. if (obj != null)
  97. {
  98. var something = obj.GetType();
  99. if (obj.GetType() == Type.GetType(item.BoundToClass) || obj.GetType().IsSubclassOf(Type.GetType(item.BoundToClass)))
  100. {
  101. string replaceValue = (string)obj[item.BoundTo];
  102. if (Body.Contains(item.Name))
  103. {
  104. Body = Body.Replace(item.Name, replaceValue);
  105. }
  106. if (Subject.Contains(item.Name))
  107. {
  108. Subject = Subject.Replace(item.Name, replaceValue);
  109. }
  110. }
  111. }
  112. }
  113. }*//*
  114. }
  115. }*/
  116. #endregion Constructor
  117. #region Properties
  118. [ForeignKey("Sender")]
  119. public int SenderId { get; set; }
  120. public string Comment { get; set; }
  121. public string Subject { get; set; }
  122. public bool IsBodyHtml { get; set; }
  123. public string CC { get; set; }
  124. public string BCC { get; set; }
  125. public string Body { get; set; }
  126. public string To { get; set; }
  127. public string ToDisplay { get; set; }
  128. public virtual SMTPAccount Sender { get; set; }
  129. #endregion
  130. #region Methods
  131. public virtual void GetSMTPAccount()
  132. {
  133. if (SenderId > 0)
  134. {
  135. }
  136. }
  137. public virtual bool SendMail()
  138. {
  139. try
  140. {
  141. if (MyCommon.IsValidEmail(To))
  142. {
  143. if (Sender != null && Sender.SMTPHost != null)
  144. {
  145. using (SmtpClient smtp = new SmtpClient(Sender.SMTPHost.Host))
  146. {
  147. MailMessage mail = GetMailMessage();
  148. smtp.UseDefaultCredentials = Sender.SMTPHost.NeedsAuthorize;
  149. smtp.Credentials = Sender.SMTPHost.GetNetworkCredential();
  150. smtp.EnableSsl = Sender.SMTPHost.UseSSL;
  151. smtp.Send(mail);
  152. Comment = "Send";
  153. return true;
  154. };
  155. }
  156. else Comment = "NoSender";
  157. }
  158. }
  159. catch (SmtpFailedRecipientException ex)
  160. {
  161. SmtpStatusCode statusCode = ex.StatusCode;
  162. if (statusCode == SmtpStatusCode.MailboxBusy ||
  163. statusCode == SmtpStatusCode.MailboxUnavailable ||
  164. statusCode == SmtpStatusCode.TransactionFailed)
  165. {
  166. Comment = $"SmtpFailedRecipientException - Msg - {ex.Message} Stack - {ex.StackTrace}";
  167. //TODO: Return correct error message
  168. return false;
  169. }
  170. else
  171. {
  172. Comment = $"SmtpFailedRecipientException - Msg - {ex.Message} Stack - {ex.StackTrace}";
  173. //TODO: Return correct error message
  174. return false;
  175. }
  176. }
  177. catch (Exception ex)
  178. {
  179. Comment = $"Exception - Msg - {ex.Message} Stack - {ex.StackTrace}";
  180. //TODO: Return correct error message
  181. return false;
  182. }
  183. return true;
  184. }
  185. private MailMessage GetMailMessage()
  186. {
  187. MailMessage mail = new MailMessage();
  188. mail.To.Add(new MailAddress(To, ToDisplay));
  189. if (!string.IsNullOrEmpty(CC))
  190. {
  191. foreach (var item in CC.Split(';'))
  192. {
  193. mail.CC.Add(item);
  194. }
  195. }
  196. if (!string.IsNullOrEmpty(BCC))
  197. {
  198. foreach (var item in BCC.Split(';'))
  199. {
  200. mail.Bcc.Add(item);
  201. }
  202. }
  203. mail.Subject = Subject;
  204. mail.Body = Body;
  205. mail.IsBodyHtml = IsBodyHtml;
  206. if (IsBodyHtml)
  207. {
  208. mail.BodyEncoding = Encoding.ASCII;
  209. }
  210. else mail.BodyEncoding = Encoding.UTF8;
  211. mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
  212. mail.Sender = new MailAddress(Sender.Address, Sender.DisplayName);
  213. mail.From = mail.Sender;
  214. return mail;
  215. }
  216. private string ConcatEmails(ICollection<Agent> agents, ICollection<Individual> individuals)
  217. {
  218. string value = string.Empty;
  219. if (agents != null && agents.Count > 0)
  220. {
  221. foreach(var item in agents)
  222. {
  223. if(MyCommon.IsValidEmail(item.Email))
  224. {
  225. value += $"{item.Email};";
  226. }
  227. }
  228. }
  229. if (individuals != null && individuals.Count > 0)
  230. {
  231. foreach (var item in individuals)
  232. {
  233. if (MyCommon.IsValidEmail(item.Email))
  234. {
  235. value += $"{item.Email};";
  236. }
  237. }
  238. }
  239. return value;
  240. }
  241. #endregion Methods
  242. }
  243. }