public async Task <BPOClient> Send(BPOClient ClientVM) { var dbContextOptions = new DbContextOptionsBuilder <DataContext>(); dbContextOptions.UseSqlServer(_configuration.GetConnectionString("DefaultConnection")); using (var newContext = new DataContext(dbContextOptions.Options)) //Separate this context's instance from the request { var client = newContext.BPOClient.FirstOrDefault(c => c.Id == ClientVM.Id); if (client == null) { throw new NullReferenceException(); } try { await _mail.SendEmailAsync("Roland", _configuration.GetSection("AdminEmailCredentials:SenderEmail").Value, _configuration.GetSection("AdminEmailCredentials:ReceiverEmail").Value, ClientVM, "New BPO Client"); client.IsSent = 1; newContext.Update(client); } catch (Exception ex) { client.Message = ex.Message + "\n " + client.Message; client.IsSent = 0; } finally { newContext.SaveChanges(); } return(null); } }
public async Task SendEmailAsync(string Name, string SenderEmail, string ReceiverEmail, BPOClient client, string Subject) { var template = "./HtmlTemplate/EmailTemplate.html"; var Content = "A message received from a potential BPO client.<br>" + "Name: <b>" + client.FullName + "</b><br>" + "Company: " + client.CompanyName + "<br>" + "Email: " + client.Email + "<br>" + "Phone: " + client.ContactNumber + "<br>" + "Message: " + client.Message + "<br>" ; var emailMessage = new MimeMessage { From = { new MailboxAddress("Dev Partners", SenderEmail) }, To = { new MailboxAddress("Recipient", ReceiverEmail) }, Subject = Subject, Body = new BodyBuilder() { HtmlBody = File.ReadAllText(template).Replace("<Fullname>", Name) .Replace("<Subject>", Subject) .Replace("<Content>", Content) }.ToMessageBody() }; await SmtpAsync(emailMessage); }