using System; using System.Collections.Generic; using System.Linq; using System.Linq.Dynamic.Core; using System.Net.Mail; using System.Text; using UnivateProperties_API.Context; using UnivateProperties_API.Helpers; using UnivateProperties_API.Model.Communication; using UnivateProperties_API.Model.Users; 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() { } 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(); string body = "
" + "

Contact from: " + name + "!

" + "

Email: " + email + "

" + "

Phone: " + phone + "

" + "

Property: " + property + "

" + "
" + "

Message:

" + "

" + message + "

" + "
" + "
" + ""; string toList = ""; int emailCount = 0; foreach (var recipient in recipients) { toList += recipient.RecipientMail + ";, "; emailCount++; } if (toList.Length > 0) { if (emailCount == 1) toList = toList.Substring(0, toList.Length - 3); else toList = toList.Substring(0, toList.Length - 2); } var host = _dbContext.Hosts.FirstOrDefault(); using (SmtpClient smtp = new SmtpClient(host.Host)) { MailMessage mail = new MailMessage(); mail.To.Add(toList); mail.Subject = "Uni-Vate - New Contact Request"; mail.Body = body; mail.IsBodyHtml = true; mail.BodyEncoding = Encoding.ASCII; mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure; mail.Sender = new MailAddress(host.User, "UniVate Properties"); mail.From = new MailAddress(mm.Email, mm.Name); smtp.UseDefaultCredentials = host.NeedsAuthorize; smtp.Credentials = host.GetNetworkCredential(); smtp.EnableSsl = host.UseSSL; smtp.Send(mail); } } public void EnquireNow(MailModel mm) { string phone = mm.Phone; string name = mm.Name; string email = mm.Email; string message = mm.Message; var prop = _dbContext.Properties.Where(p => p.Id == int.Parse(mm.Property)).FirstOrDefault(); var recipients = _dbContext.MailRecipients.Where(x => x.RecipientUsage == "EnquireNow").ToList(); string body = "
" + "

Contact from: " + name + "!

" + "

Email: " + email + "

" + "

Phone: " + phone + "

" + "
" + "

Property:

" + "

" + prop.Id + "

" + "

" + prop.PropertyName + "

" + "

" + prop.PropertyRef + "

" + "

" + prop.Price + "

" + "
" + "
" + ""; string toList = ""; foreach (var recipient in recipients) { toList += recipient.RecipientMail + ";, "; } if (toList.Length > 0) toList = toList.Substring(0, toList.Length - 2); var host = _dbContext.Hosts.FirstOrDefault(); using (SmtpClient smtp = new SmtpClient(host.Host)) { MailMessage mail = new MailMessage(); mail.To.Add(toList); mail.Subject = "Uni-Vate - Enquiry to view property"; mail.Body = body; mail.IsBodyHtml = true; mail.BodyEncoding = Encoding.ASCII; mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure; mail.Sender = new MailAddress(host.User, "UniVate Properties"); mail.From = new MailAddress(mm.ToAddress, "Admin"); smtp.UseDefaultCredentials = host.NeedsAuthorize; smtp.Credentials = host.GetNetworkCredential(); smtp.EnableSsl = host.UseSSL; smtp.Send(mail); } } public void ForgotPassword(Individual toPerson, string link) { string body = "
" + "

Dear " + toPerson.FullName + "

" + "

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

" + "
" + "

Click here to reset your password.

" + "
"+ "

Thank You

" + "

Team Uni-Vate

" + "
" + "
"; var host = _dbContext.Hosts.FirstOrDefault(); using (SmtpClient smtp = new SmtpClient(host.Host)) { MailMessage mail = new MailMessage(); mail.To.Add(new MailAddress(toPerson.Email, toPerson.FullName)); mail.Subject = "Uni-Vate - Password Reset Request"; mail.Body = body; mail.IsBodyHtml = true; mail.BodyEncoding = Encoding.ASCII; mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure; mail.Sender = new MailAddress(host.User, "UniVate Properties"); mail.From = mail.Sender; smtp.UseDefaultCredentials = host.NeedsAuthorize; smtp.Credentials = host.GetNetworkCredential(); smtp.EnableSsl = host.UseSSL; smtp.Send(mail); } } 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(); } } }