Advertisement
Guest User

Untitled

a guest
Feb 4th, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. try
  2. {
  3. using (var mail = new MailMessage())
  4. {
  5. const string email = "*******@gmail.com";
  6. const string password = "*********";
  7. var loginInfo = new NetworkCredential(email, password);
  8. mail.From = new MailAddress("*******@gmail.com");
  9. mail.To.Add(new MailAddress("email@email.com"));
  10. mail.Subject = subject;
  11. mail.Body = body;
  12. mail.IsBodyHtml = true;
  13. try
  14. {
  15. using (var smtpClient = new SmtpClient("smtp.gmail.com", 587))
  16. {
  17. smtpClient.EnableSsl = true;
  18. smtpClient.UseDefaultCredentials = false;
  19. smtpClient.Credentials = loginInfo;
  20. smtpClient.Send(mail);
  21. }
  22. }
  23. finally
  24. {
  25. mail.Dispose();
  26. }
  27.  
  28. }
  29. }
  30. catch (SmtpFailedRecipientsException ex)
  31. {
  32. foreach (SmtpFailedRecipientException t in ex.InnerExceptions)
  33. {
  34. var status = t.StatusCode;
  35. if (status == SmtpStatusCode.MailboxBusy ||
  36. status == SmtpStatusCode.MailboxUnavailable)
  37. {
  38. return View(status);
  39. }
  40. else
  41. {
  42. return View(status);
  43. }
  44. }
  45.  
  46. }
  47. catch (SmtpException Se)
  48. {
  49. // handle exception here
  50. return View(Se);
  51. }
  52.  
  53. catch (Exception ex)
  54. {
  55. return View(ex);
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement