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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. MimeMessage messageObj = new MimeMessage();
  23. MailboxAddress from;
  24. MailboxAddress to;
  25. BodyBuilder bodyBuilder = new BodyBuilder();
  26. SmtpClient client = new SmtpClient();
  27. public void ContactUs(MailModel mm)
  28. {
  29. string property = mm.Property;
  30. string phone = mm.Phone;
  31. string name = mm.Name;
  32. string email = mm.Email;
  33. string message = mm.Message;
  34. from = new MailboxAddress("Admin", mm.FromAddress);
  35. to = new MailboxAddress("User", mm.ToAddress);
  36. messageObj.From.Add(from);
  37. messageObj.To.Add(to);
  38. messageObj.Subject = "Uni-Vate - New Contact Request";
  39. bodyBuilder.HtmlBody = "<div style=\"margin: 5px\">" +
  40. "<h4>Contact from: "+ name +"!</h4>" +
  41. "<h4>Email: "+ email +"</h4>" +
  42. "<h4>Phone: " + phone + "</h4>" +
  43. "<h4>Property: " + property + "</h4>" +
  44. "<div>" +
  45. "<h4>Message: </h4>" +
  46. "<p>" + message + "</p>" +
  47. "</div>" +
  48. "</div>" +
  49. "</div>";
  50. messageObj.Body = bodyBuilder.ToMessageBody();
  51. client.Connect("smtp.gmail.com", 465, true);
  52. client.Authenticate("jlouw365@gmail.com", "setskohatxpsceqo");
  53. client.Send(messageObj);
  54. client.Disconnect(true);
  55. client.Dispose();
  56. }
  57. public void EnquireNow(MailModel mm)
  58. {
  59. string phone = mm.Phone;
  60. string name = mm.Name;
  61. string email = mm.Email;
  62. string message = mm.Message;
  63. var props = _dbContext.Properties.ToList();
  64. var prop = props.Where(x => x.Id == Convert.ToInt32(mm.Property)).FirstOrDefault();
  65. from = new MailboxAddress("Admin", mm.FromAddress);
  66. to = new MailboxAddress("User", mm.ToAddress);
  67. messageObj.From.Add(from);
  68. messageObj.To.Add(to);
  69. messageObj.Subject = "Uni-Vate - Enquiry to view property";
  70. bodyBuilder.HtmlBody = "<div style=\"margin: 5px\">" +
  71. "<h4>Contact from: " + name + "!</h4>" +
  72. "<h4>Email: " + email + "</h4>" +
  73. "<h4>Phone: " + phone + "</h4>" +
  74. "<div>" +
  75. "<h4>Property: </h4>" +
  76. "<p>" + prop.Id + "</p>" +
  77. "<p>" + prop.PropertyName + "</p>" +
  78. "<p>" + prop.PropertyRef + "</p>" +
  79. "<p>" + prop.Price + "</p>" +
  80. "</div>" +
  81. "</div>" +
  82. "</div>";
  83. messageObj.Body = bodyBuilder.ToMessageBody();
  84. client.Connect("smtp.gmail.com", 465, true);
  85. client.Authenticate("jlouw365@gmail.com", "setskohatxpsceqo");
  86. client.Send(messageObj);
  87. client.Disconnect(true);
  88. client.Dispose();
  89. }
  90. }
  91. }