public async Task RunAsync(MessagePayload messagePayload)
 {
     try
     {
         Utils.PerformLookUps(messagePayload.QueueData);
         MimeMessage mimeMessage = new MimeMessage();
         BodyBuilder bodyBuilder = new BodyBuilder();
         mimeMessage.From.Add(new MailboxAddress(Vendor.VendorDetails["senderName"], Vendor.VendorDetails["senderAddress"]));
         mimeMessage.To.Add(new MailboxAddress(messagePayload.QueueData.EmailId));
         mimeMessage.Subject  = messagePayload.QueueData.Subject;
         bodyBuilder.TextBody = messagePayload.QueueData.TextBody;
         bodyBuilder.HtmlBody = messagePayload.QueueData.HTMLBody;
         mimeMessage.Body     = bodyBuilder.ToMessageBody();
         lock (Resources.GetInstance().SmtpLock)
         {
             using SmtpClient smtpClient = CreateSMTPClient();
             smtpClient.Send(mimeMessage);
             smtpClient.Disconnect(true);
         }
         messagePayload.LogEvents.Add(Utils.CreateLogEvent(messagePayload.QueueData, IRDLM.DispatchSuccessful(Vendor.VendorName)));
         messagePayload.InvitationLogEvents.Add(Utils.CreateInvitationLogEvent(EventAction.DispatchSuccessful, EventChannel.Email,
                                                                               messagePayload.QueueData, IRDLM.DispatchSuccessful(Vendor.VendorName)));
     }
     catch (Exception ex)
     {
         messagePayload.LogEvents.Add(Utils.CreateLogEvent(messagePayload.QueueData, IRDLM.DispatchUnsuccessful(Vendor.VendorName, ex)));
         messagePayload.InvitationLogEvents.Add(Utils.CreateInvitationLogEvent(EventAction.DispatchUnsuccessful, EventChannel.Email,
                                                                               messagePayload.QueueData, IRDLM.DispatchUnsuccessful(Vendor.VendorName, ex)));
     }
 }
Exemple #2
0
        public async Task RunAsync(List <MessagePayload> messagePayloads)
        {
            var subject  = messagePayloads.ElementAt(0).QueueData.Subject;
            var htmlBody = messagePayloads.ElementAt(0).QueueData.HTMLBody;
            var textBody = messagePayloads.ElementAt(0).QueueData.TextBody;

            Prepare(subject, htmlBody, textBody);

            int batchSize = int.Parse(Vendor.VendorDetails["batchSize"]);
            List <List <MessagePayload> > batchesOfMessagePayload = new List <List <MessagePayload> >();
            int noOfBatches = messagePayloads.Count / batchSize;

            if (messagePayloads.Count % batchSize > 0)
            {
                noOfBatches++;
            }
            for (int i = 0; i < noOfBatches; i++)
            {
                batchesOfMessagePayload.Add(messagePayloads.Skip(i * batchSize).Take(batchSize).ToList());
            }

            foreach (List <MessagePayload> batchOfMessagePayload in batchesOfMessagePayload)
            {
                try
                {
                    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, Vendor.VendorDetails["url"]);
                    request.Headers.Add("Authorization", Vendor.VendorDetails["apiKey"]);
                    SparkPostRequest sparkPostRequest = new SparkPostRequest
                    {
                        content = new content
                        {
                            from = new from
                            {
                                email = Vendor.VendorDetails["senderEmail"],
                                name  = Vendor.VendorDetails["senderName"]
                            },
                            html    = newHtmlBody.ToString(),
                            subject = newSubject.ToString(),
                            text    = newTextBody.ToString()
                        }
                    };
                    sparkPostRequest.recipients = new List <recipient>();
                    foreach (MessagePayload messagePayload in batchOfMessagePayload)
                    {
                        Dictionary <string, string> substitutionDataDict = new Dictionary <string, string>();
                        foreach (KeyValuePair <string, string> kvp in qIdLookUpDict)
                        {
                            if (messagePayload.QueueData.MappedValue.ContainsKey(kvp.Key))
                            {
                                substitutionDataDict.Add(kvp.Value, messagePayload.QueueData.MappedValue[kvp.Key]);
                            }
                        }
                        substitutionDataDict.Add("Token", messagePayload.QueueData.TokenId);
                        substitutionDataDict.Add("SurveyURL", Utils.GetSurveyURL(messagePayload.QueueData));
                        substitutionDataDict.Add("UnsubscribeURL", Utils.GetUnsubscribeURL(messagePayload.QueueData));

                        recipient recipient = new recipient
                        {
                            address = new address {
                                email = messagePayload.QueueData.EmailId
                            },
                            substitution_data = substitutionDataDict
                        };
                        sparkPostRequest.recipients.Add(recipient);
                    }
                    ;
                    string jsonbody = JsonConvert.SerializeObject(sparkPostRequest);
                    request.Content = new StringContent(jsonbody, Encoding.UTF8, "application/json");
                    HttpResponseMessage response = await Resources.GetInstance().HttpClient.SendAsync(request);

                    if (!response.IsSuccessStatusCode)
                    {
                        foreach (MessagePayload messagePayload in batchOfMessagePayload)
                        {
                            HttpRequestException httpRequestException = new HttpRequestException($"Spark Post API didn't return a 2xx => response headers: " +
                                                                                                 $"{JsonConvert.SerializeObject(response)} => response content: {await response.Content.ReadAsStringAsync()}");
                            messagePayload.LogEvents.Add(Utils.CreateLogEvent(messagePayload.QueueData, IRDLM.DispatchUnsuccessful(Vendor.VendorName, httpRequestException)));
                            messagePayload.InvitationLogEvents.Add(Utils.CreateInvitationLogEvent(EventAction.DispatchUnsuccessful, EventChannel.Email,
                                                                                                  messagePayload.QueueData, IRDLM.DispatchUnsuccessful(Vendor.VendorName, httpRequestException)));
                        }
                    }
                    else
                    {
                        foreach (MessagePayload messagePayload in batchOfMessagePayload)
                        {
                            messagePayload.LogEvents.Add(Utils.CreateLogEvent(messagePayload.QueueData, IRDLM.DispatchSuccessful(Vendor.VendorName)));
                            messagePayload.InvitationLogEvents.Add(Utils.CreateInvitationLogEvent(EventAction.DispatchSuccessful, EventChannel.Email,
                                                                                                  messagePayload.QueueData, IRDLM.DispatchSuccessful(Vendor.VendorName)));
                        }
                    }
                }
                catch (Exception ex)
                {
                    foreach (MessagePayload messagePayload in batchOfMessagePayload)
                    {
                        messagePayload.LogEvents.Add(Utils.CreateLogEvent(messagePayload.QueueData, IRDLM.DispatchUnsuccessful(Vendor.VendorName, ex)));
                        messagePayload.InvitationLogEvents.Add(Utils.CreateInvitationLogEvent(EventAction.DispatchUnsuccessful, EventChannel.Email,
                                                                                              messagePayload.QueueData, IRDLM.DispatchUnsuccessful(Vendor.VendorName, ex)));
                    }
                }
            }
        }
Exemple #3
0
        public async Task RunAsync(MessagePayload messagePayload)
        {
            try
            {
                Utils.PerformLookUps(messagePayload.QueueData);
                HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, Vendor.VendorDetails["url"]);
                request.Headers.Add("Authorization", "AccessKey " + Vendor.VendorDetails["accesskey"]);
                MessageBirdRequest messageBirdRequest = new MessageBirdRequest
                {
                    body       = messagePayload.QueueData.TextBody,
                    originator = Vendor.VendorDetails["originator"],
                    recipients = messagePayload.QueueData.MobileNumber,
                    shortcode  = Vendor.VendorDetails["shortcode"] ?? "",
                    datacoding = Vendor.VendorDetails["datacoding"] ?? "plain"
                };
                string jsonbody = JsonConvert.SerializeObject(messageBirdRequest);
                request.Content = new StringContent(jsonbody, Encoding.UTF8, "application/json");
                HttpResponseMessage response = await Resources.GetInstance().HttpClient.SendAsync(request);

                if (!response.IsSuccessStatusCode)
                {
                    HttpRequestException httpRequestException = new HttpRequestException($"Message Bird API didn't return a 2xx => " +
                                                                                         $"response headers: {JsonConvert.SerializeObject(response)} => response content: {await response.Content.ReadAsStringAsync()}");
                    messagePayload.LogEvents.Add(Utils.CreateLogEvent(messagePayload.QueueData, IRDLM.DispatchUnsuccessful(Vendor.VendorName, httpRequestException)));
                    messagePayload.InvitationLogEvents.Add(Utils.CreateInvitationLogEvent(EventAction.DispatchUnsuccessful, EventChannel.SMS,
                                                                                          messagePayload.QueueData, IRDLM.DispatchUnsuccessful(Vendor.VendorName, httpRequestException)));
                }
                else
                {
                    messagePayload.LogEvents.Add(Utils.CreateLogEvent(messagePayload.QueueData, IRDLM.DispatchSuccessful(Vendor.VendorName)));
                    messagePayload.InvitationLogEvents.Add(Utils.CreateInvitationLogEvent(EventAction.DispatchSuccessful, EventChannel.SMS,
                                                                                          messagePayload.QueueData, IRDLM.DispatchSuccessful(Vendor.VendorName)));
                }
            }
            catch (Exception ex)
            {
                messagePayload.LogEvents.Add(Utils.CreateLogEvent(messagePayload.QueueData, IRDLM.DispatchUnsuccessful(Vendor.VendorName, ex)));
                messagePayload.InvitationLogEvents.Add(Utils.CreateInvitationLogEvent(EventAction.DispatchUnsuccessful, EventChannel.SMS,
                                                                                      messagePayload.QueueData, IRDLM.DispatchUnsuccessful(Vendor.VendorName, ex)));
            }
        }