Mandrill
I am using RestSharp for making an API call to the Mandrill for sending emails.
public List<EmailResult> SendMessageTemplate(MandrillApi mapiResponse, string templateName, EmailMessage emailMessage)
{
//if (mapiResponse != null && !string.IsNullOrEmpty(mapiResponse.ApiKey) && !string.IsNullOrEmpty(mapiResponse.BaseUrl)
// && !string.IsNullOrEmpty(templateName) && templateContent != null && templateContent.Count > 0 && emailMessage != null)
if (mapiResponse != null && !string.IsNullOrEmpty(mapiResponse.ApiKey) && !string.IsNullOrEmpty(mapiResponse.BaseUrl)
&& !string.IsNullOrEmpty(templateName) && emailMessage != null)
{
List<EmailResult> emailResult = new List<EmailResult>();
var soEmailMessage = JsonConvert.SerializeObject(emailMessage);
restRequest = new RestSharp.RestRequest
{
RequestFormat = DataFormat.Json,
Method = RestSharp.Method.POST,
//Resource = string.Format("{0}/send-template.json", "messages")
Resource = string.Format("{0}/send.json", "messages")
};
restRequest.AddBody(new { key = mapiResponse.ApiKey, template_name = templateName, message = soEmailMessage });
restClient = new RestSharp.RestClient(mapiResponse.BaseUrl);
var response = restClient.Execute(restRequest);
emailResult = JsonConvert.DeserializeObject<List<EmailResult>>(response.Content);
return emailResult;
}
else
return null;
}
However I get the response as
"Please enter an array".
{"status":"error","code":-2,"name":"ValidationError","message":"Validation error: {\"message\":\"Please enter an array\"}"}
The JSON generated after serialization is as follows:
"message": {
"attachments": [],
"auto_html": null,
"auto_text": true,
"bcc_address": null,
"from_email": "test@gmail.com",
"from_name": "ABC",
"global_merge_vars": [{
"content": "test",
"name": "ffname"
}],
"google_analytics_campaign": null,
"google_analytics_domains": null,
"headers": null,
"html": null,
"images": null,
"important": null,
"inline_css": null,
"merge": null,
"merge_vars": null,
"metadata": null,
"preserve_recipients": false,
"raw_message": null,
"raw_to": null,
"recipient_metadata": null,
"return_path_domain": null,
"signing_domain": null,
"subaccount": null,
"subject": "Test",
"merge_language": null,
"tags": ["Sep-2019"],
"text": null,
"to": [{
"email": "shubhamtest@gmail.com",
"name": "Test Email Address",
"type": null
}],
"track_clicks": true,
"track_opens": true,
"tracking_domain": null,
"url_strip_qs": null,
"view_content_link": null }
The EmailMessage class is part of the Mandrill assembly and it contains:
Mandrill Assembly: I installed it via NugetPackage. Please find the below link:
https://www.nuget.org/packages/Mandrill/
I tried few solutions like adding the emailmessage object into a list and then converting it into an array. But it gives the response as "[]"
.
Can anyone please suggest for the same?
Thanks