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 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 = "
" + "

Contact from: "+ name +"!

" + "

Email: "+ email +"

" + "

Phone: " + phone + "

" + "

Property: " + property + "

" + "
" + "

Message:

" + "

" + message + "

" + "
" + "
" + ""; 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 = "
" + "

Contact from: " + name + "!

" + "

Email: " + email + "

" + "

Phone: " + phone + "

" + "
" + "

Property:

" + "

" + prop.Id + "

" + "

" + prop.PropertyName + "

" + "

" + prop.PropertyRef + "

" + "

" + prop.Price + "

" + "
" + "
" + ""; 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 = "
" + "

Request to reset password

" + "

Dear " + name + ",

" + "

There has been a request to reset your password. If this is incorrect please send an email to info@univateproperties.co.za

" + "
" + "
Please follow the link below to reset your password:
" + "

"+ link + "

" + "
"+ "

Thank You

" + "

Team Uni-Vate

" + "
" + "
" + ""; 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 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(); } } }