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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Dynamic.Core;
  5. using System.Net.Mail;
  6. using System.Text;
  7. using UnivateProperties_API.Context;
  8. using UnivateProperties_API.Helpers;
  9. using UnivateProperties_API.Model.Communication;
  10. using UnivateProperties_API.Model.Users;
  11. namespace UnivateProperties_API.Repository.Communication
  12. {
  13. public interface IMailRepository
  14. {
  15. void ContactUs(MailModel mm);
  16. void EnquireNow(MailModel mm);
  17. void AddRecipient(MailRecipient rec);
  18. List<MailRecipient> GetMailRecipients();
  19. MailRecipient GetMailRecipientById(int id);
  20. void UpdateMailRecipient(MailRecipient rec);
  21. void DeleteMailRecipient(int id);
  22. }
  23. public class MailRepository : IMailRepository
  24. {
  25. private readonly DataContext _dbContext;
  26. public MailRepository(DataContext db)
  27. {
  28. _dbContext = db;
  29. }
  30. public MailRepository()
  31. {
  32. }
  33. public void ContactUs(MailModel mm)
  34. {
  35. string property = mm.Property;
  36. string phone = mm.Phone;
  37. string name = mm.Name;
  38. string email = mm.Email;
  39. string message = mm.Message;
  40. var recipients = _dbContext.MailRecipients.Where(x => x.RecipientUsage == "ContactUs").ToList();
  41. string body = "<div style=\"margin: 5px\">" +
  42. "<h4>Contact from: " + name + "!</h4>" +
  43. "<h4>Email: " + email + "</h4>" +
  44. "<h4>Phone: " + phone + "</h4>" +
  45. "<h4>Property: " + property + "</h4>" +
  46. "<div>" +
  47. "<h4>Message: </h4>" +
  48. "<p>" + message + "</p>" +
  49. "</div>" +
  50. "</div>" +
  51. "</div>";
  52. string toList = "";
  53. int emailCount = 0;
  54. foreach (var recipient in recipients)
  55. {
  56. toList += recipient.RecipientMail + ";, ";
  57. emailCount++;
  58. }
  59. if (toList.Length > 0)
  60. {
  61. if (emailCount == 1)
  62. toList = toList.Substring(0, toList.Length - 3);
  63. else
  64. toList = toList.Substring(0, toList.Length - 2);
  65. }
  66. var host = _dbContext.Hosts.FirstOrDefault();
  67. using (SmtpClient smtp = new SmtpClient(host.Host))
  68. {
  69. MailMessage mail = new MailMessage();
  70. mail.To.Add(toList);
  71. mail.Subject = "Uni-Vate - New Contact Request";
  72. mail.Body = body;
  73. mail.IsBodyHtml = true;
  74. mail.BodyEncoding = Encoding.ASCII;
  75. mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
  76. mail.Sender = new MailAddress(host.User, "UniVate Properties");
  77. mail.From = new MailAddress(mm.Email, mm.Name);
  78. smtp.UseDefaultCredentials = host.NeedsAuthorize;
  79. smtp.Credentials = host.GetNetworkCredential();
  80. smtp.EnableSsl = host.UseSSL;
  81. smtp.Send(mail);
  82. }
  83. }
  84. public void EnquireNow(MailModel mm)
  85. {
  86. string phone = mm.Phone;
  87. string name = mm.Name;
  88. string email = mm.Email;
  89. string message = mm.Message;
  90. var prop = _dbContext.Properties.Where(p => p.Id == int.Parse(mm.Property)).FirstOrDefault();
  91. var recipients = _dbContext.MailRecipients.Where(x => x.RecipientUsage == "EnquireNow").ToList();
  92. string body = "<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. string toList = "";
  106. foreach (var recipient in recipients)
  107. {
  108. toList += recipient.RecipientMail + ";, ";
  109. }
  110. if (toList.Length > 0)
  111. toList = toList.Substring(0, toList.Length - 2);
  112. var host = _dbContext.Hosts.FirstOrDefault();
  113. using (SmtpClient smtp = new SmtpClient(host.Host))
  114. {
  115. MailMessage mail = new MailMessage();
  116. mail.To.Add(toList);
  117. mail.Subject = "Uni-Vate - Enquiry to view property";
  118. mail.Body = body;
  119. mail.IsBodyHtml = true;
  120. mail.BodyEncoding = Encoding.ASCII;
  121. mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
  122. mail.Sender = new MailAddress(host.User, "UniVate Properties");
  123. mail.From = new MailAddress(mm.ToAddress, "Admin");
  124. smtp.UseDefaultCredentials = host.NeedsAuthorize;
  125. smtp.Credentials = host.GetNetworkCredential();
  126. smtp.EnableSsl = host.UseSSL;
  127. smtp.Send(mail);
  128. }
  129. }
  130. public void ForgotPassword(Individual toPerson, string link)
  131. {
  132. string body = "<div style=\"margin: 5px\">" +
  133. "<h4>Dear " + toPerson.FullName + "</h4>" +
  134. "<h4>There has been a request to reset your password. If this is incorrect please send an email to info@univateproperties.co.za</h4>" +
  135. "<div>" +
  136. "<h4><a href='" + link + "'>Click here</a> to reset your password.</h4>" +
  137. "<br />"+
  138. "<h4>Thank You</h4>" +
  139. "<h4>Team Uni-Vate</h4>" +
  140. "</div>" +
  141. "</div>";
  142. var host = _dbContext.Hosts.FirstOrDefault();
  143. using (SmtpClient smtp = new SmtpClient(host.Host))
  144. {
  145. MailMessage mail = new MailMessage();
  146. mail.To.Add(new MailAddress(toPerson.Email, toPerson.FullName));
  147. mail.Subject = "Uni-Vate - Password Reset Request";
  148. mail.Body = body;
  149. mail.IsBodyHtml = true;
  150. mail.BodyEncoding = Encoding.ASCII;
  151. mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
  152. mail.Sender = new MailAddress(host.User, "UniVate Properties");
  153. mail.From = mail.Sender;
  154. smtp.UseDefaultCredentials = host.NeedsAuthorize;
  155. smtp.Credentials = host.GetNetworkCredential();
  156. smtp.EnableSsl = host.UseSSL;
  157. smtp.Send(mail);
  158. }
  159. }
  160. public void AddRecipient(MailRecipient rec)
  161. {
  162. if (MyCommon.IsValidEmail(rec.RecipientMail))
  163. {
  164. _dbContext.MailRecipients.Add(rec);
  165. _dbContext.SaveChanges();
  166. }
  167. else
  168. {
  169. throw new Exception();
  170. }
  171. }
  172. public List<MailRecipient> GetMailRecipients()
  173. {
  174. return _dbContext.MailRecipients.Where(x => x.IsDeleted == false).ToList();
  175. }
  176. public MailRecipient GetMailRecipientById(int id)
  177. {
  178. return _dbContext.MailRecipients.Where(x => x.Id == id).FirstOrDefault();
  179. }
  180. public void UpdateMailRecipient(MailRecipient rec)
  181. {
  182. var recipient = _dbContext.MailRecipients.Where(x => x.Id == rec.Id).FirstOrDefault();
  183. if (recipient.RecipientMail != rec.RecipientMail)
  184. {
  185. recipient.RecipientMail = rec.RecipientMail;
  186. }
  187. if (recipient.RecipientName != rec.RecipientName)
  188. {
  189. recipient.RecipientName = rec.RecipientName;
  190. }
  191. if (recipient.RecipientUsage != rec.RecipientUsage)
  192. {
  193. recipient.RecipientUsage = rec.RecipientUsage;
  194. }
  195. _dbContext.MailRecipients.Update(recipient);
  196. _dbContext.SaveChanges();
  197. }
  198. public void DeleteMailRecipient(int id)
  199. {
  200. var rec = _dbContext.MailRecipients.Where(x => x.Id == id).FirstOrDefault();
  201. _dbContext.MailRecipients.Remove(rec);
  202. _dbContext.SaveChanges();
  203. }
  204. }
  205. }