123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- using MailKit.Net.Smtp;
- using MimeKit;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Linq.Dynamic.Core;
- using UnivateProperties_API.Context;
- using UnivateProperties_API.Helpers;
- using UnivateProperties_API.Model.Communication;
-
- namespace UnivateProperties_API.Repository.Communication
- {
- public interface IMailRepository
- {
- void ContactUs(MailModel mm);
- void EnquireNow(MailModel mm);
- void AddRecipient(MailRecipient rec);
- List<MailRecipient> GetMailRecipients();
- MailRecipient GetMailRecipientById(int id);
- void UpdateMailRecipient(MailRecipient rec);
- void DeleteMailRecipient(int id);
- }
-
- public class MailRepository : IMailRepository
- {
- private readonly DataContext _dbContext;
-
- public MailRepository(DataContext db)
- {
- _dbContext = db;
- }
-
- public MailRepository()
- {
- }
-
- MimeMessage messageObj = new MimeMessage();
- MailboxAddress from;
- MailboxAddress to;
- BodyBuilder bodyBuilder = new BodyBuilder();
- SmtpClient client = new SmtpClient();
-
- public void ContactUs(MailModel mm)
- {
- string property = mm.Property;
- string phone = mm.Phone;
- string name = mm.Name;
- string email = mm.Email;
- string message = mm.Message;
- var recipients = _dbContext.MailRecipients.Where(x => x.RecipientUsage == "ContactUs").ToList();
-
- from = new MailboxAddress("Admin", mm.FromAddress);
-
- InternetAddressList list = new InternetAddressList();
- foreach (var recipient in recipients)
- {
- list.Add(new MailboxAddress(recipient.RecipientName, recipient.RecipientMail));
- }
- //to = new MailboxAddress("User", mm.ToAddress);
-
- messageObj.From.Add(from);
- messageObj.To.AddRange(list);
-
- messageObj.Subject = "Uni-Vate - New Contact Request";
-
- bodyBuilder.HtmlBody = "<div style=\"margin: 5px\">" +
- "<h4>Contact from: "+ name +"!</h4>" +
- "<h4>Email: "+ email +"</h4>" +
- "<h4>Phone: " + phone + "</h4>" +
- "<h4>Property: " + property + "</h4>" +
- "<div>" +
- "<h4>Message: </h4>" +
- "<p>" + message + "</p>" +
- "</div>" +
- "</div>" +
- "</div>";
-
- messageObj.Body = bodyBuilder.ToMessageBody();
-
- client.Connect("smtp.gmail.com", 465, true);
- client.Authenticate("jlouw365@gmail.com", "setskohatxpsceqo");
-
- client.Send(messageObj);
- client.Disconnect(true);
- client.Dispose();
- }
-
- public void EnquireNow(MailModel mm)
- {
- string phone = mm.Phone;
- string name = mm.Name;
- string email = mm.Email;
- string message = mm.Message;
- var props = _dbContext.Properties.ToList();
- var prop = props.Where(x => x.Id == Convert.ToInt32(mm.Property)).FirstOrDefault();
- var recipients = _dbContext.MailRecipients.Where(x => x.RecipientUsage == "EnquireNow").ToList();
-
-
- from = new MailboxAddress("Admin", mm.FromAddress);
-
- InternetAddressList list = new InternetAddressList();
- foreach (var recipient in recipients)
- {
- list.Add(new MailboxAddress(recipient.RecipientName, recipient.RecipientMail));
- }
- //to = new MailboxAddress("User", mm.ToAddress);
-
- messageObj.From.Add(from);
- messageObj.To.AddRange(list);
-
- messageObj.Subject = "Uni-Vate - Enquiry to view property";
-
- bodyBuilder.HtmlBody = "<div style=\"margin: 5px\">" +
- "<h4>Contact from: " + name + "!</h4>" +
- "<h4>Email: " + email + "</h4>" +
- "<h4>Phone: " + phone + "</h4>" +
- "<div>" +
- "<h4>Property: </h4>" +
- "<p>" + prop.Id + "</p>" +
- "<p>" + prop.PropertyName + "</p>" +
- "<p>" + prop.PropertyRef + "</p>" +
- "<p>" + prop.Price + "</p>" +
- "</div>" +
- "</div>" +
- "</div>";
-
- messageObj.Body = bodyBuilder.ToMessageBody();
-
- client.Connect("smtp.gmail.com", 465, true);
- client.Authenticate("jlouw365@gmail.com", "setskohatxpsceqo");
-
- client.Send(messageObj);
- client.Disconnect(true);
- client.Dispose();
- }
-
- public void ForgotPassword(MailModel mm, string link)
- {
- string name = mm.Name;
- string email = mm.Email;
-
- from = new MailboxAddress("Admin", mm.FromAddress);
-
- to = new MailboxAddress("User", mm.ToAddress);
-
- messageObj.From.Add(from);
- messageObj.To.Add(to);
-
- messageObj.Subject = "Uni-Vate - Password Reset Request";
-
- bodyBuilder.HtmlBody = "<div style=\"margin: 5px\">" +
- "<h4>Request to reset password</h4>" +
- "<h4>Dear " + name + ", </h4>" +
- "<h4>There has been a request to reset your password. If this is incorrect please send an email to info@univateproperties.co.za</h4>" +
- "<div>" +
- "<h5>Please follow the link below to reset your password:</h5>" +
- "<h3>"+ link + "</h3>" +
- "<br />"+
- "<h4>Thank You</h4>" +
- "<h4>Team Uni-Vate</h4>" +
- "</div>" +
- "</div>" +
- "</div>";
-
- messageObj.Body = bodyBuilder.ToMessageBody();
-
- client.Connect("smtp.gmail.com", 465, true);
- client.Authenticate("jlouw365@gmail.com", "setskohatxpsceqo");
-
- client.Send(messageObj);
- client.Disconnect(true);
- client.Dispose();
- }
-
- public void AddRecipient(MailRecipient rec)
- {
- if (MyCommon.IsValidEmail(rec.RecipientMail))
- {
- _dbContext.MailRecipients.Add(rec);
- _dbContext.SaveChanges();
- }
- else
- {
- throw new Exception();
- }
-
- }
-
- public List<MailRecipient> GetMailRecipients()
- {
- return _dbContext.MailRecipients.Where(x => x.IsDeleted == false).ToList();
- }
-
- public MailRecipient GetMailRecipientById(int id)
- {
- return _dbContext.MailRecipients.Where(x => x.Id == id).FirstOrDefault();
- }
-
- public void UpdateMailRecipient(MailRecipient rec)
- {
- var recipient = _dbContext.MailRecipients.Where(x => x.Id == rec.Id).FirstOrDefault();
-
- if (recipient.RecipientMail != rec.RecipientMail)
- {
- recipient.RecipientMail = rec.RecipientMail;
- }
-
- if (recipient.RecipientName != rec.RecipientName)
- {
- recipient.RecipientName = rec.RecipientName;
- }
-
- if (recipient.RecipientUsage != rec.RecipientUsage)
- {
- recipient.RecipientUsage = rec.RecipientUsage;
- }
-
-
- _dbContext.MailRecipients.Update(recipient);
- _dbContext.SaveChanges();
- }
-
- public void DeleteMailRecipient(int id)
- {
- var rec = _dbContext.MailRecipients.Where(x => x.Id == id).FirstOrDefault();
- _dbContext.MailRecipients.Remove(rec);
- _dbContext.SaveChanges();
- }
- }
- }
|