Advertisement
Guest User

Untitled

a guest
Apr 12th, 2016
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. The server committed a protocol violation The server response was:
  2.  
  3. public class YlpSmtpClient
  4. {
  5. readonly string _fromEmailAddress;
  6. readonly string _host;
  7. readonly string _username;
  8. readonly string _password;
  9. public YlpSmtpClient(string host, string username, string password, string fromEmailAddress)
  10. {
  11. _host = host;
  12. _username = username;
  13. _password = password;
  14. _fromEmailAddress = fromEmailAddress;
  15. }
  16.  
  17. public delegate YlpSmtpClient CreateSmtpClient();
  18. protected YlpSmtpClient() { }
  19.  
  20. public virtual async Task SendMailAsync(string subject, string body, string toEmailAddress, bool isBodyHtml = false)
  21. {
  22. try
  23. {
  24. using (var client = new SmtpClient(_host) { Credentials = new NetworkCredential(_username, _password) , Port = 587 })
  25. {
  26. using (MailMessage message = MakeMessage(_fromEmailAddress, subject, body, toEmailAddress, isBodyHtml))
  27. {
  28. //process exits when calling this line
  29. await client.SendMailAsync(message);
  30. }
  31. }
  32. }
  33. catch (System.Exception ex)
  34. {
  35.  
  36. }
  37. }
  38.  
  39. MailMessage MakeMessage(string fromEmailAddress, string subject, string body, string toEmailAddress, bool isBodyHtml)
  40. {
  41. var message = new MailMessage(fromEmailAddress, toEmailAddress, subject, body);
  42. message.IsBodyHtml = isBodyHtml;
  43. return message;
  44. }
  45. }
  46.  
  47. public class EmailNotifier<TNotificationEntity> : INotifier<TNotificationEntity>
  48. where TNotificationEntity : NotificationEntity
  49. {
  50. private readonly INotificationTemplater<TNotificationEntity, string> _notificationTemplater;
  51. private readonly YlpSmtpClient _smtpClient;
  52. private readonly INotificationsService<TNotificationEntity> _notificationsService;
  53.  
  54. public EmailNotifier(
  55. INotificationTemplater<TNotificationEntity, string> notificationTemplater,
  56. YlpSmtpClient smtpClient,
  57. INotificationsService<TNotificationEntity> notificationsService)
  58. {
  59. _notificationTemplater = notificationTemplater;
  60. _smtpClient = smtpClient;
  61. _notificationsService = notificationsService;
  62. }
  63.  
  64. public async Task SendAsync(TNotificationEntity notification)
  65. {
  66. if (notification.EmailIsOn)
  67. {
  68. var templatedMessageBody = await _notificationTemplater
  69. .WrapInTemplateAsync(notification, NotificationType.Email);
  70.  
  71. await _smtpClient.SendMailAsync(notification.Subject, templatedMessageBody, notification.EmailAddress);
  72. await _notificationsService.MarkNotificationAsSentAsync(notification, NotificationType.Email);
  73. }
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement