Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. public interface IInvioMail
  2. {
  3. bool SendEmail(string destinatario, string _TestoEmail);
  4. }
  5.  
  6. public class InvioMail:IInvioMail
  7. {
  8. public bool SendEmail(string destinatario, string _TestoEmail)
  9. {
  10. try
  11. {
  12. MailMessage mail = new MailMessage();
  13. SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
  14.  
  15. mail.From = new MailAddress("your_email_address@gmail.com");
  16. mail.To.Add(destinatario);
  17. mail.Subject = "News Letter fanstastica";
  18. mail.Body = _TestoEmail;
  19.  
  20. SmtpServer.Port = 587;
  21. SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
  22. SmtpServer.EnableSsl = true;
  23.  
  24. SmtpServer.Send(mail);
  25. }
  26. catch (Exception ex)
  27. {
  28. return false;
  29. }
  30. return true;
  31.  
  32. }
  33. }
  34.  
  35. public class InvioMailNewsLetter
  36. {
  37. private LocalDbContext mLocalDbContext = new LocalDbContext();
  38. IInvioMail invioMail;
  39. public InvioMailNewsLetter(IInvioMail _invioMail)
  40. {
  41. invioMail = _invioMail;
  42. }
  43.  
  44. public bool EseguiAnalisi(string _TestoEmail)
  45. {
  46. List<ListaMailNewsletter> ListaMailDaInviare = mLocalDbContext.SaldiFinali.Where(x => x.MailDaInviare && !string.IsNullOrEmpty(x.Mail)).ToList();
  47.  
  48. foreach (var item in ListaMailDaInviare)
  49. {
  50. if (!invioMail.SendEmail(item.Mail, _TestoEmail))
  51. return false;
  52.  
  53. item.MailDaInviare = false;
  54.  
  55. }
  56.  
  57. mLocalDbContext.SaveChanges();
  58. return true;
  59. }
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement