Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- The server committed a protocol violation The server response was:
- public class YlpSmtpClient
- {
- readonly string _fromEmailAddress;
- readonly string _host;
- readonly string _username;
- readonly string _password;
- public YlpSmtpClient(string host, string username, string password, string fromEmailAddress)
- {
- _host = host;
- _username = username;
- _password = password;
- _fromEmailAddress = fromEmailAddress;
- }
- public delegate YlpSmtpClient CreateSmtpClient();
- protected YlpSmtpClient() { }
- public virtual async Task SendMailAsync(string subject, string body, string toEmailAddress, bool isBodyHtml = false)
- {
- try
- {
- using (var client = new SmtpClient(_host) { Credentials = new NetworkCredential(_username, _password) , Port = 587 })
- {
- using (MailMessage message = MakeMessage(_fromEmailAddress, subject, body, toEmailAddress, isBodyHtml))
- {
- //process exits when calling this line
- await client.SendMailAsync(message);
- }
- }
- }
- catch (System.Exception ex)
- {
- }
- }
- MailMessage MakeMessage(string fromEmailAddress, string subject, string body, string toEmailAddress, bool isBodyHtml)
- {
- var message = new MailMessage(fromEmailAddress, toEmailAddress, subject, body);
- message.IsBodyHtml = isBodyHtml;
- return message;
- }
- }
- public class EmailNotifier<TNotificationEntity> : INotifier<TNotificationEntity>
- where TNotificationEntity : NotificationEntity
- {
- private readonly INotificationTemplater<TNotificationEntity, string> _notificationTemplater;
- private readonly YlpSmtpClient _smtpClient;
- private readonly INotificationsService<TNotificationEntity> _notificationsService;
- public EmailNotifier(
- INotificationTemplater<TNotificationEntity, string> notificationTemplater,
- YlpSmtpClient smtpClient,
- INotificationsService<TNotificationEntity> notificationsService)
- {
- _notificationTemplater = notificationTemplater;
- _smtpClient = smtpClient;
- _notificationsService = notificationsService;
- }
- public async Task SendAsync(TNotificationEntity notification)
- {
- if (notification.EmailIsOn)
- {
- var templatedMessageBody = await _notificationTemplater
- .WrapInTemplateAsync(notification, NotificationType.Email);
- await _smtpClient.SendMailAsync(notification.Subject, templatedMessageBody, notification.EmailAddress);
- await _notificationsService.MarkNotificationAsSentAsync(notification, NotificationType.Email);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement