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.

MailRepository.cs 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. using Abp.Specifications;
  2. using MimeKit;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Linq.Dynamic.Core;
  7. using System.Net.Mail;
  8. using System.Text;
  9. using UnivateProperties_API.Context;
  10. using UnivateProperties_API.Helpers;
  11. using UnivateProperties_API.Model.Communication;
  12. using UnivateProperties_API.Model.ProcessFlow;
  13. using UnivateProperties_API.Model.Timeshare;
  14. using UnivateProperties_API.Model.Users;
  15. namespace UnivateProperties_API.Repository.Communication
  16. {
  17. public interface IMailRepository
  18. {
  19. void ContactUs(MailModel mm);
  20. void EnquireNow(MailModel mm);
  21. void AddRecipient(MailRecipient rec);
  22. List<MailRecipient> GetMailRecipients();
  23. MailRecipient GetMailRecipientById(int id);
  24. void UpdateMailRecipient(MailRecipient rec);
  25. void DeleteMailRecipient(int id);
  26. }
  27. public class MailRepository : IMailRepository
  28. {
  29. private readonly DataContext _dbContext;
  30. public MailRepository(DataContext db)
  31. {
  32. _dbContext = db;
  33. }
  34. public MailRepository()
  35. {
  36. }
  37. public void ContactUs(MailModel mm)
  38. {
  39. var recipients = _dbContext.MailRecipients.Where(x => x.RecipientUsage == "ContactUs").Where(y => y.IsDeleted == false).ToList();
  40. string body = _dbContext.Templates.Where(x => x.Name == "ContactUs").Where(y => y.IsDeleted == false).FirstOrDefault().Body;
  41. body = body.Replace("[FULLNAME]", mm.Name);
  42. body = body.Replace("[USEREMAIL]", mm.Email);
  43. body = body.Replace("[USERCELLPHONE]", mm.Phone);
  44. body = body.Replace("[PROPERTYREF]", mm.Property);
  45. body = body.Replace("[USERMESSAGE]", mm.Message);
  46. string toList = "";
  47. int emailCount = 0;
  48. foreach (var recipient in recipients)
  49. {
  50. toList += recipient.RecipientMail + ", ";
  51. emailCount++;
  52. }
  53. if (toList.Length > 0)
  54. {
  55. if (emailCount == 1)
  56. toList = toList.Substring(0, toList.Length - 2);
  57. else
  58. toList = toList.Substring(0, toList.Length - 2);
  59. }
  60. var host = _dbContext.Hosts.FirstOrDefault();
  61. using (SmtpClient smtp = new SmtpClient(host.Host))
  62. {
  63. MailMessage mail = new MailMessage();
  64. mail.To.Add(toList);
  65. mail.Subject = "Uni-Vate - New Contact Request";
  66. mail.Body = body;
  67. mail.IsBodyHtml = true;
  68. mail.BodyEncoding = Encoding.ASCII;
  69. mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
  70. mail.Sender = new MailAddress(host.User, "UniVate Properties");
  71. mail.From = new MailAddress(mm.Email, mm.Name);
  72. smtp.UseDefaultCredentials = host.NeedsAuthorize;
  73. smtp.Credentials = host.GetNetworkCredential();
  74. smtp.EnableSsl = host.UseSSL;
  75. smtp.Send(mail);
  76. }
  77. }
  78. public void EnquireNow(MailModel mm)
  79. {
  80. var recipients = _dbContext.MailRecipients.Where(x => x.RecipientUsage == "EnquireNow").Where(y => y.IsDeleted == false).ToList();
  81. string body = _dbContext.Templates.Where(x => x.Name == "EnquireNow").Where(y => y.IsDeleted == false).FirstOrDefault().Body;
  82. var property = _dbContext.Properties.Where(x => x.Id == Convert.ToInt32(mm.Property)).FirstOrDefault();
  83. body = body.Replace("[FULLNAME]", mm.Name);
  84. body = body.Replace("[USEREMAIL]", mm.Email);
  85. body = body.Replace("[USERCELLPHONE]", mm.Phone);
  86. body = body.Replace("[PROPERTYID]", mm.Property);
  87. body = body.Replace("[PROPERTYNAME]", property.PropertyName);
  88. body = body.Replace("[PROPERTYREF]", property.PropertyRef);
  89. body = body.Replace("[PROPERTYPRICE]", property.Price.ToString());
  90. body = body.Replace("[USERMESSAGE]", mm.Message);
  91. string toList = "";
  92. int emailCount = 0;
  93. foreach (var recipient in recipients)
  94. {
  95. toList += recipient.RecipientMail + ", ";
  96. emailCount++;
  97. }
  98. if (toList.Length > 0)
  99. {
  100. if (emailCount == 1)
  101. toList = toList.Substring(0, toList.Length - 2);
  102. else
  103. toList = toList.Substring(0, toList.Length - 2);
  104. }
  105. var host = _dbContext.Hosts.FirstOrDefault();
  106. using (SmtpClient smtp = new SmtpClient(host.Host))
  107. {
  108. MailMessage mail = new MailMessage();
  109. mail.To.Add(toList);
  110. mail.Subject = "Uni-Vate - Enquiry to view property";
  111. mail.Body = body;
  112. mail.IsBodyHtml = true;
  113. mail.BodyEncoding = Encoding.ASCII;
  114. mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
  115. mail.Sender = new MailAddress(host.User, "UniVate Properties");
  116. mail.From = new MailAddress(mm.Email, mm.Name);
  117. smtp.UseDefaultCredentials = host.NeedsAuthorize;
  118. smtp.Credentials = host.GetNetworkCredential();
  119. smtp.EnableSsl = host.UseSSL;
  120. smtp.Send(mail);
  121. }
  122. }
  123. public void ForgotPassword(Individual toPerson, string link)
  124. {
  125. string body = "<div style=\"margin: 5px\">" +
  126. "<h4>Dear " + toPerson.FullName + "</h4>" +
  127. "<h4>There has been a request to reset your password. If this is incorrect please send an email to info@univateproperties.co.za</h4>" +
  128. "<h4>Once your password has been reset you can use the username "+ toPerson.User.Username + " and the new password to log in.</h4>" +
  129. "<div>" +
  130. "<h4><a href='" + link + "'>Click here</a> to reset your password.</h4>" +
  131. "<br />"+
  132. "<h4>Thank You</h4>" +
  133. "<h4>Team Uni-Vate</h4>" +
  134. "</div>" +
  135. "</div>";
  136. var host = _dbContext.Hosts.FirstOrDefault();
  137. using (SmtpClient smtp = new SmtpClient(host.Host))
  138. {
  139. MailMessage mail = new MailMessage();
  140. mail.To.Add(new MailAddress(toPerson.Email, toPerson.FullName));
  141. mail.Subject = "Uni-Vate - Password Reset Request";
  142. mail.Body = body;
  143. mail.IsBodyHtml = true;
  144. mail.BodyEncoding = Encoding.ASCII;
  145. mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
  146. mail.Sender = new MailAddress(host.User, "UniVate Properties");
  147. mail.From = mail.Sender;
  148. smtp.UseDefaultCredentials = host.NeedsAuthorize;
  149. smtp.Credentials = host.GetNetworkCredential();
  150. smtp.EnableSsl = host.UseSSL;
  151. smtp.Send(mail);
  152. }
  153. }
  154. public void WeekOfferMadeOwner(TimeshareWeek week, BidItem bid)
  155. {
  156. var recipients = _dbContext.MailRecipients.Where(x => x.RecipientUsage == "WeekOfferMade-Owner").Where(y => y.IsDeleted == false).ToList();
  157. string body = _dbContext.Templates.Where(x => x.Name == "WeekOfferMade-Owner").Where(y => y.IsDeleted == false).FirstOrDefault().Body;
  158. body = body.Replace("[OWNER]", week.DisplayOwner);
  159. body = body.Replace("[RESORTNAME]", week.ResortName);
  160. body = body.Replace("[UNITNUMBER]", week.UnitNumber);
  161. body = body.Replace("[MODULE]", week.Module);
  162. body = body.Replace("[OFFERMADE]", bid.Amount.ToString());
  163. body = body.Replace("[FULLNAME]", bid.BidMaker.Display);
  164. body = body.Replace("[USEREMAIL]", bid.BidMaker.Email);
  165. body = body.Replace("[USERCELLPHONE]", bid.BidMaker.CellNumber);
  166. body = body.Replace("[USERTELEPHONE]", bid.BidMaker.Telephone);
  167. body = body.Replace("[USERCOMMENT]", bid.Comment);
  168. string toList = "";
  169. int emailCount = 0;
  170. foreach (var recipient in recipients)
  171. {
  172. toList += recipient.RecipientMail + ", ";
  173. emailCount++;
  174. }
  175. if (toList.Length > 0)
  176. {
  177. if (emailCount == 1)
  178. toList = toList.Substring(0, toList.Length - 2);
  179. else
  180. toList = toList.Substring(0, toList.Length - 2);
  181. }
  182. var host = _dbContext.Hosts.FirstOrDefault();
  183. using (SmtpClient smtp = new SmtpClient(host.Host))
  184. {
  185. MailMessage mail = new MailMessage();
  186. mail.To.Add(toList);
  187. mail.Subject = "Uni-Vate - New Contact Request";
  188. mail.Body = body;
  189. mail.IsBodyHtml = true;
  190. mail.BodyEncoding = Encoding.ASCII;
  191. mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
  192. mail.Sender = new MailAddress(host.User, "UniVate Properties");
  193. mail.From = mail.Sender;
  194. smtp.UseDefaultCredentials = host.NeedsAuthorize;
  195. smtp.Credentials = host.GetNetworkCredential();
  196. smtp.EnableSsl = host.UseSSL;
  197. smtp.Send(mail);
  198. }
  199. }
  200. public void WeekOfferMadeUser(TimeshareWeek week, BidItem bid)
  201. {
  202. //var recipients = _dbContext.MailRecipients.Where(x => x.RecipientUsage == "WeekOfferMade-User").Where(y => y.IsDeleted == false).ToList();
  203. string body = _dbContext.Templates.Where(x => x.Name == "WeekOfferMade-User").Where(y => y.IsDeleted == false).FirstOrDefault().Body; ;
  204. body = body.Replace("[OWNER]", week.DisplayOwner);
  205. body = body.Replace("[RESORTNAME]", week.ResortName);
  206. body = body.Replace("[UNITNUMBER]", week.UnitNumber);
  207. body = body.Replace("[MODULE]", week.Module);
  208. body = body.Replace("[OFFERMADE]", bid.Amount.ToString());
  209. body = body.Replace("[FULLNAME]", bid.BidMaker.Display);
  210. body = body.Replace("[USEREMAIL]", bid.BidMaker.Email);
  211. body = body.Replace("[USERCELLPHONE]", bid.BidMaker.CellNumber);
  212. body = body.Replace("[USERTELEPHONE]", bid.BidMaker.Telephone);
  213. body = body.Replace("[USERCOMMENT]", bid.Comment);
  214. var host = _dbContext.Hosts.FirstOrDefault();
  215. using (SmtpClient smtp = new SmtpClient(host.Host))
  216. {
  217. MailMessage mail = new MailMessage();
  218. mail.To.Add(bid.BidMaker.Email);
  219. mail.Subject = "Uni-Vate - New Contact Request";
  220. mail.Body = body;
  221. mail.IsBodyHtml = true;
  222. mail.BodyEncoding = Encoding.ASCII;
  223. mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
  224. mail.Sender = new MailAddress(host.User, "UniVate Properties");
  225. mail.From = mail.Sender;
  226. smtp.UseDefaultCredentials = host.NeedsAuthorize;
  227. smtp.Credentials = host.GetNetworkCredential();
  228. smtp.EnableSsl = host.UseSSL;
  229. smtp.Send(mail);
  230. }
  231. }
  232. public void WeekOfferMadeAdmin()
  233. {
  234. }
  235. public void WeekLoadedAgent(TimeshareWeek week)
  236. {
  237. string body = _dbContext.Templates.Where(x => x.Name == "WeekLoaded-Agent").Where(y => y.IsDeleted == false).FirstOrDefault().Body;
  238. var recipients = _dbContext.MailRecipients.Where(x => x.RecipientUsage == "WeekLoaded-Agent").Where(y => y.IsDeleted == false).ToList();
  239. body = body.Replace("[FULLNAME]", week.DisplayOwner);
  240. body = body.Replace("[RESORTNAME]", week.ResortName);
  241. body = body.Replace("[UNITNUMBER]", week.UnitNumber);
  242. body = body.Replace("[MODULE]", week.Module);
  243. string toList = "";
  244. int emailCount = 0;
  245. foreach (var recipient in recipients)
  246. {
  247. toList += recipient.RecipientMail + ", ";
  248. emailCount++;
  249. }
  250. if (toList.Length > 0)
  251. {
  252. if (emailCount == 1)
  253. toList = toList.Substring(0, toList.Length - 2);
  254. else
  255. toList = toList.Substring(0, toList.Length - 2);
  256. }
  257. var host = _dbContext.Hosts.FirstOrDefault();
  258. using (SmtpClient smtp = new SmtpClient(host.Host))
  259. {
  260. MailMessage mail = new MailMessage();
  261. mail.To.Add(toList);
  262. mail.Subject = "Uni-Vate - Password Reset Request";
  263. mail.Body = body;
  264. mail.IsBodyHtml = true;
  265. mail.BodyEncoding = Encoding.ASCII;
  266. mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
  267. mail.Sender = new MailAddress(host.User, "UniVate Properties");
  268. mail.From = mail.Sender;
  269. smtp.UseDefaultCredentials = host.NeedsAuthorize;
  270. smtp.Credentials = host.GetNetworkCredential();
  271. smtp.EnableSsl = host.UseSSL;
  272. smtp.Send(mail);
  273. }
  274. }
  275. public void WeekLoadedOwner(TimeshareWeek week)
  276. {
  277. string body = _dbContext.Templates.Where(x => x.Name == "WeekLoaded-Owner").Where(y => y.IsDeleted == false).FirstOrDefault().Body;
  278. var recipients = _dbContext.MailRecipients.Where(x => x.RecipientUsage == "WeekLoaded-Owner").Where(y => y.IsDeleted == false).ToList();
  279. body = body.Replace("[FULLNAME]", week.DisplayOwner);
  280. body = body.Replace("[RESORTNAME]", week.ResortName);
  281. body = body.Replace("[UNITNUMBER]", week.UnitNumber);
  282. body = body.Replace("[MODULE]", week.Module);
  283. string toList = "";
  284. int emailCount = 0;
  285. foreach (var recipient in recipients)
  286. {
  287. toList += recipient.RecipientMail + ", ";
  288. emailCount++;
  289. }
  290. if (toList.Length > 0)
  291. {
  292. if (emailCount == 1)
  293. toList = toList.Substring(0, toList.Length - 2);
  294. else
  295. toList = toList.Substring(0, toList.Length - 2);
  296. }
  297. var host = _dbContext.Hosts.FirstOrDefault();
  298. using (SmtpClient smtp = new SmtpClient(host.Host))
  299. {
  300. MailMessage mail = new MailMessage();
  301. mail.To.Add(toList);
  302. mail.Subject = "Uni-Vate - Password Reset Request";
  303. mail.Body = body;
  304. mail.IsBodyHtml = true;
  305. mail.BodyEncoding = Encoding.ASCII;
  306. mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
  307. mail.Sender = new MailAddress(host.User, "UniVate Properties");
  308. mail.From = mail.Sender;
  309. smtp.UseDefaultCredentials = host.NeedsAuthorize;
  310. smtp.Credentials = host.GetNetworkCredential();
  311. smtp.EnableSsl = host.UseSSL;
  312. smtp.Send(mail);
  313. }
  314. }
  315. public void AddRecipient(MailRecipient rec)
  316. {
  317. if (MyCommon.IsValidEmail(rec.RecipientMail))
  318. {
  319. _dbContext.MailRecipients.Add(rec);
  320. _dbContext.SaveChanges();
  321. }
  322. else
  323. {
  324. throw new Exception();
  325. }
  326. }
  327. public List<MailRecipient> GetMailRecipients()
  328. {
  329. return _dbContext.MailRecipients.Where(x => x.IsDeleted == false).ToList();
  330. }
  331. public MailRecipient GetMailRecipientById(int id)
  332. {
  333. return _dbContext.MailRecipients.Where(x => x.Id == id).FirstOrDefault();
  334. }
  335. public void UpdateMailRecipient(MailRecipient rec)
  336. {
  337. var recipient = _dbContext.MailRecipients.Where(x => x.Id == rec.Id).FirstOrDefault();
  338. if (recipient.RecipientMail != rec.RecipientMail)
  339. {
  340. recipient.RecipientMail = rec.RecipientMail;
  341. }
  342. if (recipient.RecipientName != rec.RecipientName)
  343. {
  344. recipient.RecipientName = rec.RecipientName;
  345. }
  346. if (recipient.RecipientUsage != rec.RecipientUsage)
  347. {
  348. recipient.RecipientUsage = rec.RecipientUsage;
  349. }
  350. _dbContext.MailRecipients.Update(recipient);
  351. _dbContext.SaveChanges();
  352. }
  353. public void DeleteMailRecipient(int id)
  354. {
  355. var rec = _dbContext.MailRecipients.Where(x => x.Id == id).FirstOrDefault();
  356. _dbContext.MailRecipients.Remove(rec);
  357. _dbContext.SaveChanges();
  358. }
  359. }
  360. }