API
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

MailRepository.cs 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. using MailKit.Net.Smtp;
  2. using MimeKit;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Linq.Dynamic.Core;
  7. using UnivateProperties_API.Context;
  8. using UnivateProperties_API.Helpers;
  9. using UnivateProperties_API.Model.Communication;
  10. namespace UnivateProperties_API.Repository.Communication
  11. {
  12. public interface IMailRepository
  13. {
  14. void ContactUs(MailModel mm);
  15. void EnquireNow(MailModel mm);
  16. void AddRecipient(MailRecipient rec);
  17. List<MailRecipient> GetMailRecipients();
  18. MailRecipient GetMailRecipientById(int id);
  19. void UpdateMailRecipient(MailRecipient rec);
  20. void DeleteMailRecipient(int id);
  21. }
  22. public class MailRepository : IMailRepository
  23. {
  24. private readonly DataContext _dbContext;
  25. public MailRepository(DataContext db)
  26. {
  27. _dbContext = db;
  28. }
  29. public MailRepository()
  30. {
  31. }
  32. MimeMessage messageObj = new MimeMessage();
  33. MailboxAddress from;
  34. MailboxAddress to;
  35. BodyBuilder bodyBuilder = new BodyBuilder();
  36. SmtpClient client = new SmtpClient();
  37. public void ContactUs(MailModel mm)
  38. {
  39. string property = mm.Property;
  40. string phone = mm.Phone;
  41. string name = mm.Name;
  42. string email = mm.Email;
  43. string message = mm.Message;
  44. var recipients = _dbContext.MailRecipients.Where(x => x.RecipientUsage == "ContactUs").ToList();
  45. from = new MailboxAddress("Admin", mm.FromAddress);
  46. InternetAddressList list = new InternetAddressList();
  47. foreach (var recipient in recipients)
  48. {
  49. list.Add(new MailboxAddress(recipient.RecipientName, recipient.RecipientMail));
  50. }
  51. //to = new MailboxAddress("User", mm.ToAddress);
  52. messageObj.From.Add(from);
  53. messageObj.To.AddRange(list);
  54. messageObj.Subject = "Uni-Vate - New Contact Request";
  55. bodyBuilder.HtmlBody = "<div style=\"margin: 5px\">" +
  56. "<h4>Contact from: "+ name +"!</h4>" +
  57. "<h4>Email: "+ email +"</h4>" +
  58. "<h4>Phone: " + phone + "</h4>" +
  59. "<h4>Property: " + property + "</h4>" +
  60. "<div>" +
  61. "<h4>Message: </h4>" +
  62. "<p>" + message + "</p>" +
  63. "</div>" +
  64. "</div>" +
  65. "</div>";
  66. messageObj.Body = bodyBuilder.ToMessageBody();
  67. client.Connect("smtp.gmail.com", 465, true);
  68. client.Authenticate("jlouw365@gmail.com", "setskohatxpsceqo");
  69. client.Send(messageObj);
  70. client.Disconnect(true);
  71. client.Dispose();
  72. }
  73. public void EnquireNow(MailModel mm)
  74. {
  75. string phone = mm.Phone;
  76. string name = mm.Name;
  77. string email = mm.Email;
  78. string message = mm.Message;
  79. var props = _dbContext.Properties.ToList();
  80. var prop = props.Where(x => x.Id == Convert.ToInt32(mm.Property)).FirstOrDefault();
  81. var recipients = _dbContext.MailRecipients.Where(x => x.RecipientUsage == "EnquireNow").ToList();
  82. from = new MailboxAddress("Admin", mm.FromAddress);
  83. InternetAddressList list = new InternetAddressList();
  84. foreach (var recipient in recipients)
  85. {
  86. list.Add(new MailboxAddress(recipient.RecipientName, recipient.RecipientMail));
  87. }
  88. //to = new MailboxAddress("User", mm.ToAddress);
  89. messageObj.From.Add(from);
  90. messageObj.To.AddRange(list);
  91. messageObj.Subject = "Uni-Vate - Enquiry to view property";
  92. bodyBuilder.HtmlBody = "<div style=\"margin: 5px\">" +
  93. "<h4>Contact from: " + name + "!</h4>" +
  94. "<h4>Email: " + email + "</h4>" +
  95. "<h4>Phone: " + phone + "</h4>" +
  96. "<div>" +
  97. "<h4>Property: </h4>" +
  98. "<p>" + prop.Id + "</p>" +
  99. "<p>" + prop.PropertyName + "</p>" +
  100. "<p>" + prop.PropertyRef + "</p>" +
  101. "<p>" + prop.Price + "</p>" +
  102. "</div>" +
  103. "</div>" +
  104. "</div>";
  105. messageObj.Body = bodyBuilder.ToMessageBody();
  106. client.Connect("smtp.gmail.com", 465, true);
  107. client.Authenticate("jlouw365@gmail.com", "setskohatxpsceqo");
  108. client.Send(messageObj);
  109. client.Disconnect(true);
  110. client.Dispose();
  111. }
  112. public void ForgotPassword(MailModel mm, string link)
  113. {
  114. string name = mm.Name;
  115. string email = mm.Email;
  116. from = new MailboxAddress("Admin", mm.FromAddress);
  117. to = new MailboxAddress("User", mm.ToAddress);
  118. messageObj.From.Add(from);
  119. messageObj.To.Add(to);
  120. messageObj.Subject = "Uni-Vate - Password Reset Request";
  121. bodyBuilder.HtmlBody = "<div style=\"margin: 5px\">" +
  122. "<h4>Request to reset password</h4>" +
  123. "<h4>Dear " + name + ", </h4>" +
  124. "<h4>There has been a request to reset your password. If this is incorrect please send an email to info@univateproperties.co.za</h4>" +
  125. "<div>" +
  126. "<h5>Please follow the link below to reset your password:</h5>" +
  127. "<h3>"+ link + "</h3>" +
  128. "<br />"+
  129. "<h4>Thank You</h4>" +
  130. "<h4>Team Uni-Vate</h4>" +
  131. "</div>" +
  132. "</div>" +
  133. "</div>";
  134. messageObj.Body = bodyBuilder.ToMessageBody();
  135. client.Connect("smtp.gmail.com", 465, true);
  136. client.Authenticate("jlouw365@gmail.com", "setskohatxpsceqo");
  137. client.Send(messageObj);
  138. client.Disconnect(true);
  139. client.Dispose();
  140. }
  141. public void AddRecipient(MailRecipient rec)
  142. {
  143. if (MyCommon.IsValidEmail(rec.RecipientMail))
  144. {
  145. _dbContext.MailRecipients.Add(rec);
  146. _dbContext.SaveChanges();
  147. }
  148. else
  149. {
  150. throw new Exception();
  151. }
  152. }
  153. public List<MailRecipient> GetMailRecipients()
  154. {
  155. return _dbContext.MailRecipients.Where(x => x.IsDeleted == false).ToList();
  156. }
  157. public MailRecipient GetMailRecipientById(int id)
  158. {
  159. return _dbContext.MailRecipients.Where(x => x.Id == id).FirstOrDefault();
  160. }
  161. public void UpdateMailRecipient(MailRecipient rec)
  162. {
  163. var recipient = _dbContext.MailRecipients.Where(x => x.Id == rec.Id).FirstOrDefault();
  164. if (recipient.RecipientMail != rec.RecipientMail)
  165. {
  166. recipient.RecipientMail = rec.RecipientMail;
  167. }
  168. if (recipient.RecipientName != rec.RecipientName)
  169. {
  170. recipient.RecipientName = rec.RecipientName;
  171. }
  172. if (recipient.RecipientUsage != rec.RecipientUsage)
  173. {
  174. recipient.RecipientUsage = rec.RecipientUsage;
  175. }
  176. _dbContext.MailRecipients.Update(recipient);
  177. _dbContext.SaveChanges();
  178. }
  179. public void DeleteMailRecipient(int id)
  180. {
  181. var rec = _dbContext.MailRecipients.Where(x => x.Id == id).FirstOrDefault();
  182. _dbContext.MailRecipients.Remove(rec);
  183. _dbContext.SaveChanges();
  184. }
  185. }
  186. }