public void Transmit(IUser recipient, SingleMessageInstance instance) { if (_isDisposed) { throw new ObjectDisposedException(nameof(EmailTransmissionStrategy)); } try { MailAddress to = new MailAddress(recipient.Email, recipient.Name); MailMessage emailMessage = new MailMessage(_from, to) { Subject = "Design Patterns in C# message", Body = instance.Contents }; _smtpClient.Send(emailMessage); } catch (Exception exception) { throw new MessagingException( $"Could not transmit STMP email message {instance.InstanceId} to {recipient.Email}", exception, MessagingExceptionReason.MessageSendError ); } ILogger logger = _loggerFactory.Create(nameof(EmailTransmissionStrategy)); logger.Info("Successfully sent STMP email message", recipient.Email, instance.InstanceId); }
public void Transmit(IUser recipient, SingleMessageInstance instance) { if (string.IsNullOrWhiteSpace(recipient.Phone)) { throw new MessagingException( $"No phone number specified for {recipient.Name} in message instance {instance.InstanceId}", reason: MessagingExceptionReason.MessageSendError ); } TwilioClient.Init(_accountSid, _authToken); PhoneNumber to = new PhoneNumber(recipient.Phone); MessageResource mr = MessageResource.Create(to, from: _from, body: instance.Contents); if (mr.ErrorCode.HasValue) { throw new MessagingException( $"SMS to {recipient.Phone} was not delivered by Twilio. Error code {mr.ErrorCode.Value} in message instance {instance.InstanceId}", reason: MessagingExceptionReason.MessageSendError ); } ILogger logger = _loggerFactory.Create(nameof(SmsTransmissionStrategy)); logger.Info("Successfully sent SMS text message", recipient.Phone, instance.InstanceId); }
public void Send(IMessage message) { ILogger logger = _loggerFactory.Create(nameof(Messenger)); // Step 3a) MessageTemplate messageTemplate = _repository.GetById(message.MessageTemplateId); // Step 3b) string contents = _substitutor.Substitute(messageTemplate, message.Parameters); SingleMessageInstance instance = new SingleMessageInstance(contents); // Step 3c) _strategy.Transmit(message.Recipient, instance); logger.Info("Message Send() completed successfully", message); }