/// <summary> /// Sends the email message. /// </summary> /// <param name="connectionIdentifier">The connection identifier.</param> /// <param name="messageId">The message identifier.</param> /// <param name="fromAddress">From address.</param> /// <param name="toAddresses">To addresses.</param> /// <param name="subject">The subject.</param> /// <param name="body">The body.</param> /// <param name="isHtml">if set to <c>true</c> [is HTML].</param> /// <param name="cancellationToken">The cancellation token.</param> public async Task SendEmailMessage(Guid connectionIdentifier, Guid messageId, String fromAddress, List <String> toAddresses, String subject, String body, Boolean isHtml, CancellationToken cancellationToken) { // Rehydrate Email Message aggregate EmailAggregate emailAggregate = await this.EmailAggregateRepository.GetLatestVersion(messageId, cancellationToken); // send message to provider (record event) emailAggregate.SendRequestToProvider(fromAddress, toAddresses, subject, body, isHtml); // Make call to Email provider here EmailServiceProxyResponse emailResponse = await this.EmailServiceProxy.SendEmail(messageId, fromAddress, toAddresses, subject, body, isHtml, cancellationToken); // response message from provider (record event) emailAggregate.ReceiveResponseFromProvider(emailResponse.RequestIdentifier, emailResponse.EmailIdentifier); // Save Changes to persistance await this.EmailAggregateRepository.SaveChanges(emailAggregate, cancellationToken); }
/// <summary> /// Sends the email. /// </summary> /// <param name="messageId">The message identifier.</param> /// <param name="fromAddress">From address.</param> /// <param name="toAddresses">To addresses.</param> /// <param name="subject">The subject.</param> /// <param name="body">The body.</param> /// <param name="isHtml">if set to <c>true</c> [is HTML].</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns></returns> public async Task <EmailServiceProxyResponse> SendEmail(Guid messageId, String fromAddress, List <String> toAddresses, String subject, String body, Boolean isHtml, CancellationToken cancellationToken) { EmailServiceProxyResponse response = null; // Translate the request message Smtp2GoSendEmailRequest apiRequest = new Smtp2GoSendEmailRequest { ApiKey = ConfigurationReader.GetValue("SMTP2GoAPIKey"), HTMLBody = isHtml ? body : string.Empty, TextBody = isHtml ? string.Empty : body, Sender = fromAddress, Subject = subject, TestMode = false, To = toAddresses.ToArray() }; String requestSerialised = JsonConvert.SerializeObject(apiRequest); Logger.LogDebug($"Request Message Sent to Email Provider [SMTP2Go] {requestSerialised}"); StringContent content = new StringContent(requestSerialised, Encoding.UTF8, "application/json"); String requestUri = $"{ConfigurationReader.GetValue("SMTP2GoBaseAddress")}/email/send"; HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, requestUri); requestMessage.Content = content; HttpResponseMessage httpResponse = await this.HttpClient.SendAsync(requestMessage, cancellationToken); Smtp2GoSendEmailResponse apiResponse = JsonConvert.DeserializeObject <Smtp2GoSendEmailResponse>(await httpResponse.Content.ReadAsStringAsync()); Logger.LogDebug($"Response Message Received from Email Provider [SMTP2Go] {JsonConvert.SerializeObject(apiResponse)}"); // Translate the Response response = new EmailServiceProxyResponse { ApiStatusCode = httpResponse.StatusCode, EmailIdentifier = apiResponse.Data.EmailId, Error = apiResponse.Data.Error, ErrorCode = apiResponse.Data.ErrorCode, RequestIdentifier = apiResponse.RequestId }; return(response); }