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.8KB

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