public DeliveryStatusResponseXml GetDeliveryStatusXml(Guid transactionId) { var response = new DeliveryStatusResponseXml(); try { var requestString = "https://api.elasticemail.com/mailer/status/" + transactionId + "?showstats=true"; var request = WebRequest.Create(requestString); Stream stream = new RetryLogic(_configuration.PreferredRetryStrategy).Execute(() => request.GetResponse().GetResponseStream()); var xmlString = new StreamReader(stream, Encoding.UTF8).ReadToEnd(); if (xmlString.ToLower().StartsWith("no job with transactionid")) { response.ErrorMessage = xmlString; response.ResultType = ResultType.Error; } else { try { XmlSerializer serializer = new XmlSerializer(typeof(job)); StringReader rdr = new StringReader(xmlString); var job = (job)serializer.Deserialize(rdr); var deliveryStatus = new DeliveryStatusXml(); deliveryStatus.Id = transactionId; deliveryStatus.Recipients = job.recipients; deliveryStatus.Delivered = job.delivered; deliveryStatus.Failed = job.failed; deliveryStatus.Pending = job.pending; deliveryStatus.Status = job.status; deliveryStatus.Clicked = job.clicked; deliveryStatus.Opened = job.opened; deliveryStatus.Unsubscribed = job.unsubscribed; response.DeliveryStatus = deliveryStatus; response.ResultType = ResultType.Success; } catch (Exception ex) { response.ErrorMessage = "Could not deserialize message: " + Environment.NewLine + ex; response.ResultType = ResultType.Error; } } } catch (Exception ex) { response.ErrorMessage = ex.ToString(); response.ResultType = ResultType.Error; } return(response); }
public DeliveryStatusResponseJson GetDeliveryStatusJson(Guid transactionId) { var response = new DeliveryStatusResponseJson(); try { var requestString = "https://api.elasticemail.com/v2/email/getstatus?apikey=" + _configuration.ApiKey + "&transactionID=" + transactionId + "&showstats=true"; var request = WebRequest.Create(requestString); Stream stream = new RetryLogic(_configuration.PreferredRetryStrategy).Execute(() => request.GetResponse().GetResponseStream()); var jsonString = new StreamReader(stream).ReadToEnd(); if (jsonString.StartsWith("{\"success\":false")) { response.ErrorMessage = jsonString; response.ResultType = ResultType.Error; } else { try { DeliveryStatusJson job = JsonConvert.DeserializeObject <DeliveryStatusJson>(jsonString); if (job.data == null) { response.ErrorMessage = jsonString; response.ResultType = ResultType.Error; return(response); } response.DeliveryStatus = job; response.ResultType = ResultType.Success; } catch (Exception ex) { response.ErrorMessage = "Could not deserialize message: " + Environment.NewLine + ex; response.ResultType = ResultType.Error; } } } catch (Exception ex) { response.ErrorMessage = ex.ToString(); response.ResultType = ResultType.Error; } return(response); }
public SendResult Send(ElasticemailMessage msg) { var result = new SendResult(); try { string validationErrors; if (!IsValid(msg, out validationErrors)) { result.ResultType = ResultType.Error; result.ErrorMessage = validationErrors; return(result); } var client = new WebClient(); var values = new NameValueCollection(); values.Add("api_key", _configuration.ApiKey); values.Add("from", msg.From.Address); values.Add("from_name", msg.From.DisplayName); values.Add("to", string.Join(";", msg.To.Select(x => x.Address))); values.Add("subject", msg.Subject); values.Add(msg.IsBodyHtml ? "body_html" : "body_text", msg.Body); if (msg.ReplyTo != null) { values.Add("reply_to", msg.ReplyTo.Address); } var attachmentIds = new List <string>(); foreach (var attachment in msg.Attachments) { var attId = UploadAttachment(attachment.Key, attachment.Value); attachmentIds.Add(attId); } if (attachmentIds.Any()) { values.Add("attachments", string.Join(";", attachmentIds)); } var response = new RetryLogic(_configuration.PreferredRetryStrategy).Execute(() => client.UploadValues("https://api.elasticemail.com/mailer/send", values)); var responseString = Encoding.UTF8.GetString(response); Guid guid; if (Guid.TryParse(responseString, out guid)) { result.TransactionId = guid; result.ResultType = ResultType.Success; } else { result.ErrorMessage = responseString; result.ResultType = ResultType.Error; } } catch (Exception ex) { result.ResultType = ResultType.Error; result.ErrorMessage = ex.ToString(); } return(result); }