Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.Extensions.Options;
- using System;
- using System.IO;
- using System.Net;
- using System.Net.Mail;
- using System.Text;
- using System.Threading.Tasks;
- namespace WSGestaoInterv.Services
- {
- // This class is used by the application to send email for account confirmation and password reset.
- // For more details see https://go.microsoft.com/fwlink/?LinkID=532713
- public class EmailSender : IEmailSender
- {
- public EmailSender(IOptions<EmailSettings> emailSettings)
- {
- _emailSettings = emailSettings.Value;
- }
- public EmailSettings _emailSettings { get; }
- public Task SendEmailAsync(string email, string tipo, string subject, dynamic message, string token = "")
- {
- try
- {
- Execute(email, tipo, subject, message, token).Wait();
- return Task.FromResult(0);
- }
- catch (Exception)
- {
- throw;
- }
- }
- public async Task Execute(string email, string tipo, string subject, dynamic message, string token)
- {
- try
- {
- string toEmail = string.IsNullOrEmpty(email) ? _emailSettings.ToEmail : email;
- var builder = new StringBuilder();
- if(tipo == "si"){
- using (var reader = File.OpenText("templates/si.html"))
- {
- builder.Append(reader.ReadToEnd());
- }
- builder.Replace("@{TipoDocumento}", message.TipoDocumento);
- builder.Replace("@{NumDocumento}", message.NumDocumento);
- builder.Replace("@{AgenteSolicitante}", message.AgenteSolicitante);
- builder.Replace("@{ServiceExecultado}", message.ServiceExecultado);
- builder.Replace("@{Equipamentos}", message.Equipamentos);
- builder.Replace("@{Periodos}", message.Periodos);
- if(token != "")
- {
- builder.Replace("@{Link}", token);
- }
- }else if(tipo == "senha"){
- using (var reader = File.OpenText("templates/senha.html"))
- {
- builder.Append(reader.ReadToEnd());
- }
- if(token != "")
- {
- builder.Replace("@{Link}", token);
- }
- builder.Replace("@{Nome}", message.Nome).Replace("@{Login}", message.Login).Replace("@{Senha}", message.Senha);
- }else if(tipo == "trocasenha"){
- using (var reader = File.OpenText("templates/trocasenha.html"))
- {
- builder.Append(reader.ReadToEnd());
- }
- if(token != "")
- {
- builder.Replace("@{Link}", token);
- }
- builder.Replace("@{Nome}", message.Nome).Replace("@{Login}", message.Login).Replace("@{Senha}", message.Senha);
- }else if(tipo == "nova senha"){
- using (var reader = File.OpenText("templates/novasenha.html"))
- {
- builder.Append(reader.ReadToEnd());
- }
- if(token != "")
- {
- builder.Replace("@{Link}", token);
- }
- builder.Replace("@{Nome}", message.Nome).Replace("@{Login}", message.Login);
- }
- MailMessage mail = new MailMessage()
- {
- From = new MailAddress(_emailSettings.UsernameEmail, "In Forma API Gestão de Intervenções")
- };
- mail.To.Add(new MailAddress(toEmail));
- // mail.CC.Add(new MailAddress(_emailSettings.CcEmail));
- mail.Subject = "[Portal] " + subject;
- mail.Body = builder.ToString();
- mail.IsBodyHtml = true;
- mail.Priority = MailPriority.High;
- // Adicionar Anexo
- //mail.Attachments.Add(new Attachment(arquivo));
- using (SmtpClient smtp = new SmtpClient(_emailSettings.PrimaryDomain, _emailSettings.PrimaryPort))
- {
- smtp.Credentials = new NetworkCredential(_emailSettings.UsernameEmail,_emailSettings.UsernamePassword);
- smtp.EnableSsl = true;
- await smtp.SendMailAsync(mail);
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement