public NotificationSentEvent SendEmail( string senderEmail, string senderName, string recipientEmail, string recipientName, string subject, string body) { var notificationSentEvent = new NotificationSentEvent { Status = NotificationStatus.Send, Type = NotificationType.Email, Reciever = recipientEmail, Subject = subject, Message = body }; if (!bool.Parse(ConfigurationManager.AppSettings["EnableEmails"])) { return(notificationSentEvent); } try { using (var mailMessage = new MailMessage { From = new MailAddress(_fromEmail, senderName), Subject = subject, Body = body, BodyEncoding = Encoding.UTF8, IsBodyHtml = true }) { mailMessage.To.Add(new MailAddress(recipientEmail, recipientName)); using (var smtpClient = new SmtpClient(_host, _port) { Credentials = new NetworkCredential(_userName, _password), EnableSsl = true }) { // After calling SendAsync, you must wait for the e-mail transmission to complete before attempting to send another e-mail message using Send or SendAsync. // https://msdn.microsoft.com/en-us/library/x5x13z6h.aspx smtpClient.Send(mailMessage); } } } catch (SmtpException e) { notificationSentEvent.Status = NotificationStatus.Error; notificationSentEvent.ErrorCode = Enum.GetName(typeof(SmtpStatusCode), e.StatusCode); notificationSentEvent.ErrorMessage = e.Message; } return(notificationSentEvent); }
public NotificationSentEvent SendSms(string from, string to, string body) { var notificationSentEvent = new NotificationSentEvent { Status = NotificationStatus.Send, Type = NotificationType.Sms, Message = body, Reciever = to }; if (!bool.Parse(ConfigurationManager.AppSettings["EnableSms"])) { return(notificationSentEvent); } var accountSid = ConfigurationManager.AppSettings["Twilio.AccountSid"]; var authToken = ConfigurationManager.AppSettings["Twilio.AuthToken"]; var twilioRestClient = new TwilioRestClient(accountSid, authToken); var message = twilioRestClient.SendMessage("+" + from, "+" + to, body); if (message.RestException != null) { notificationSentEvent.Status = NotificationStatus.Error; notificationSentEvent.ErrorCode = message.RestException.Code; notificationSentEvent.ErrorMessage = message.RestException.Message; } else if (message.ErrorCode.HasValue) { notificationSentEvent.Status = NotificationStatus.Error; notificationSentEvent.ErrorCode = message.ErrorCode.ToString(); notificationSentEvent.ErrorMessage = message.ErrorMessage; } return(notificationSentEvent); }