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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using MailKit.Net.Smtp;
  2. using MimeKit;
  3. using System;
  4. using System.Linq;
  5. using System.Linq.Dynamic.Core;
  6. using UnivateProperties_API.Context;
  7. using UnivateProperties_API.Model.Communication;
  8. namespace UnivateProperties_API.Repository.Communication
  9. {
  10. public interface IMailRepository
  11. {
  12. void ContactUs(MailModel mm);
  13. void EnquireNow(MailModel mm);
  14. }
  15. public class MailRepository : IMailRepository
  16. {
  17. private readonly DataContext _dbContext;
  18. public MailRepository(DataContext db)
  19. {
  20. _dbContext = db;
  21. }
  22. public MailRepository()
  23. {
  24. }
  25. MimeMessage messageObj = new MimeMessage();
  26. MailboxAddress from;
  27. MailboxAddress to;
  28. BodyBuilder bodyBuilder = new BodyBuilder();
  29. SmtpClient client = new SmtpClient();
  30. public void ContactUs(MailModel mm)
  31. {
  32. string property = mm.Property;
  33. string phone = mm.Phone;
  34. string name = mm.Name;
  35. string email = mm.Email;
  36. string message = mm.Message;
  37. from = new MailboxAddress("Admin", mm.FromAddress);
  38. to = new MailboxAddress("User", mm.ToAddress);
  39. messageObj.From.Add(from);
  40. messageObj.To.Add(to);
  41. messageObj.Subject = "Uni-Vate - New Contact Request";
  42. bodyBuilder.HtmlBody = "<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. messageObj.Body = bodyBuilder.ToMessageBody();
  54. client.Connect("smtp.gmail.com", 465, true);
  55. client.Authenticate("jlouw365@gmail.com", "setskohatxpsceqo");
  56. client.Send(messageObj);
  57. client.Disconnect(true);
  58. client.Dispose();
  59. }
  60. public void EnquireNow(MailModel mm)
  61. {
  62. string phone = mm.Phone;
  63. string name = mm.Name;
  64. string email = mm.Email;
  65. string message = mm.Message;
  66. var props = _dbContext.Properties.ToList();
  67. var prop = props.Where(x => x.Id == Convert.ToInt32(mm.Property)).FirstOrDefault();
  68. from = new MailboxAddress("Admin", mm.FromAddress);
  69. to = new MailboxAddress("User", mm.ToAddress);
  70. messageObj.From.Add(from);
  71. messageObj.To.Add(to);
  72. messageObj.Subject = "Uni-Vate - Enquiry to view property";
  73. bodyBuilder.HtmlBody = "<div style=\"margin: 5px\">" +
  74. "<h4>Contact from: " + name + "!</h4>" +
  75. "<h4>Email: " + email + "</h4>" +
  76. "<h4>Phone: " + phone + "</h4>" +
  77. "<div>" +
  78. "<h4>Property: </h4>" +
  79. "<p>" + prop.Id + "</p>" +
  80. "<p>" + prop.PropertyName + "</p>" +
  81. "<p>" + prop.PropertyRef + "</p>" +
  82. "<p>" + prop.Price + "</p>" +
  83. "</div>" +
  84. "</div>" +
  85. "</div>";
  86. messageObj.Body = bodyBuilder.ToMessageBody();
  87. client.Connect("smtp.gmail.com", 465, true);
  88. client.Authenticate("jlouw365@gmail.com", "setskohatxpsceqo");
  89. client.Send(messageObj);
  90. client.Disconnect(true);
  91. client.Dispose();
  92. }
  93. public void ForgotPassword(MailModel mm, string link)
  94. {
  95. string name = mm.Name;
  96. string email = mm.Email;
  97. from = new MailboxAddress("Admin", mm.FromAddress);
  98. to = new MailboxAddress("User", mm.ToAddress);
  99. messageObj.From.Add(from);
  100. messageObj.To.Add(to);
  101. messageObj.Subject = "Uni-Vate - Password Reset Request";
  102. bodyBuilder.HtmlBody = "<div style=\"margin: 5px\">" +
  103. "<h4>Request to reset password</h4>" +
  104. "<h4>Dear " + name + " there has been a request to reset your password. If this is incorrect please send an email to info@univateproperties.co.za</h4>" +
  105. "<div>" +
  106. "<h3>"+ link + "</h3>" +
  107. "</div>" +
  108. "</div>" +
  109. "</div>";
  110. messageObj.Body = bodyBuilder.ToMessageBody();
  111. client.Connect("smtp.gmail.com", 465, true);
  112. client.Authenticate("jlouw365@gmail.com", "setskohatxpsceqo");
  113. client.Send(messageObj);
  114. client.Disconnect(true);
  115. client.Dispose();
  116. }
  117. }
  118. }