Advertisement
Guest User

EmailSender

a guest
Nov 18th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.75 KB | None | 0 0
  1. using Microsoft.Extensions.Options;
  2. using System;
  3. using System.IO;
  4. using System.Net;
  5. using System.Net.Mail;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace WSGestaoInterv.Services
  10. {
  11.     // This class is used by the application to send email for account confirmation and password reset.
  12.     // For more details see https://go.microsoft.com/fwlink/?LinkID=532713
  13.     public class EmailSender : IEmailSender
  14.     {
  15.         public EmailSender(IOptions<EmailSettings> emailSettings)
  16.         {
  17.             _emailSettings = emailSettings.Value;
  18.         }
  19.  
  20.         public EmailSettings _emailSettings { get; }
  21.  
  22.         public Task SendEmailAsync(string email, string tipo, string subject, dynamic message, string token = "")
  23.         {
  24.             try
  25.             {
  26.                 Execute(email, tipo, subject, message, token).Wait();
  27.                 return Task.FromResult(0);
  28.             }
  29.             catch (Exception)
  30.             {
  31.                 throw;
  32.             }
  33.         }
  34.  
  35.         public async Task Execute(string email, string tipo, string subject, dynamic message, string token)
  36.         {
  37.             try
  38.             {
  39.                 string toEmail = string.IsNullOrEmpty(email) ? _emailSettings.ToEmail : email;
  40.  
  41.                 var builder = new StringBuilder();
  42.  
  43.                 if(tipo == "si"){
  44.                     using (var reader = File.OpenText("templates/si.html"))
  45.                     {
  46.                         builder.Append(reader.ReadToEnd());
  47.                     }
  48.  
  49.                     builder.Replace("@{TipoDocumento}", message.TipoDocumento);
  50.                     builder.Replace("@{NumDocumento}", message.NumDocumento);
  51.                     builder.Replace("@{AgenteSolicitante}", message.AgenteSolicitante);
  52.                     builder.Replace("@{ServiceExecultado}", message.ServiceExecultado);
  53.                     builder.Replace("@{Equipamentos}", message.Equipamentos);
  54.                     builder.Replace("@{Periodos}", message.Periodos);
  55.  
  56.                     if(token != "")
  57.                     {
  58.                         builder.Replace("@{Link}", token);
  59.                     }
  60.  
  61.                 }else if(tipo == "senha"){
  62.                     using (var reader = File.OpenText("templates/senha.html"))
  63.                     {
  64.                         builder.Append(reader.ReadToEnd());
  65.                     }
  66.  
  67.                     if(token != "")
  68.                     {
  69.                         builder.Replace("@{Link}", token);
  70.                     }
  71.  
  72.                     builder.Replace("@{Nome}", message.Nome).Replace("@{Login}", message.Login).Replace("@{Senha}", message.Senha);
  73.                 }else if(tipo == "trocasenha"){
  74.                     using (var reader = File.OpenText("templates/trocasenha.html"))
  75.                     {
  76.                         builder.Append(reader.ReadToEnd());
  77.                     }
  78.  
  79.                     if(token != "")
  80.                     {
  81.                         builder.Replace("@{Link}", token);
  82.                     }
  83.  
  84.                     builder.Replace("@{Nome}", message.Nome).Replace("@{Login}", message.Login).Replace("@{Senha}", message.Senha);
  85.                 }else if(tipo == "nova senha"){
  86.                     using (var reader = File.OpenText("templates/novasenha.html"))
  87.                     {
  88.                         builder.Append(reader.ReadToEnd());
  89.                     }
  90.  
  91.                     if(token != "")
  92.                     {
  93.                         builder.Replace("@{Link}", token);
  94.                     }
  95.  
  96.                     builder.Replace("@{Nome}", message.Nome).Replace("@{Login}", message.Login);
  97.                 }
  98.  
  99.                 MailMessage mail = new MailMessage()
  100.                 {
  101.                     From = new MailAddress(_emailSettings.UsernameEmail, "In Forma API Gestão de Intervenções")
  102.                 };
  103.  
  104.                 mail.To.Add(new MailAddress(toEmail));
  105.                 // mail.CC.Add(new MailAddress(_emailSettings.CcEmail));
  106.  
  107.                 mail.Subject = "[Portal] " + subject;
  108.                 mail.Body = builder.ToString();
  109.                 mail.IsBodyHtml = true;
  110.                 mail.Priority = MailPriority.High;
  111.  
  112.                 // Adicionar Anexo
  113.                 //mail.Attachments.Add(new Attachment(arquivo));
  114.  
  115.                 using (SmtpClient smtp = new SmtpClient(_emailSettings.PrimaryDomain, _emailSettings.PrimaryPort))
  116.                 {
  117.                     smtp.Credentials = new NetworkCredential(_emailSettings.UsernameEmail,_emailSettings.UsernamePassword);
  118.                     smtp.EnableSsl = true;
  119.                     await smtp.SendMailAsync(mail);
  120.                 }
  121.             }
  122.             catch (Exception ex)
  123.             {
  124.                 throw ex;
  125.             }
  126.         }
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement