Hello. There were difficulties in interacting with the API . Neither can I make a POST request to send a letter, in response I get 400 ошибку . The trouble is that I can not understand what the problem is, help me figure it out.

To send a letter I use the following code:

 var client = new HttpClient(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); var content = new StringContent(json, Encoding.UTF8, "application/json"); var url = "https://api.sendpulse.com/smtp/emails/"; var response = client.PostAsync(url, content).Result; // 400 Ошибка 

Json data:

 { "text" : "Мой текст", "subject" : "Моя тема", "from" : { "name" : "Admin", "email" : "myEmail@yandex.ru" }, "to" : [ { "name" : "Client", "email" : "client1@yandex.ru" } ] } 

PS

1) The authorization token was received correctly, sent for verification with this token Get all requests OK.

2) Json data is valid, checked them for correctness with the JsonSerializer serializer.

  • Json data also show. Only email addresses mask with asterisks. Because at the third-party view there are no errors in the code. Is that the documentation contains the address https://api.sendpulse.com/smtp/emails , and you have added a trailing slash to it - https://api.sendpulse.com/smtp/emails/ . Maybe with this service it matters. - Mark Shevchenko
  • Updated. Added Json data. In fact, I tried the address api.sendpulse.com/smtp/emails backslash and remove and leave, but there was no sense. In general, everyone has already lost his temper)) - sp7
  • From what else is clear: not all fields from the API example are present for you. Perhaps html required field. It is also possible bcc required field and must be an empty array. In general, of course, it is more correct to contact the support service by sending them your JSON. - Mark Shevchenko
  • I talked with the support, the maximum than helped, so it said that the error on your (ie, my) side, look. On account of html and other attributes, I found out, they are not all mandatory, but used if necessary. Like if the html version of the letter is sent, then the html used, otherwise just text sufficient. Although in the course of my experiments, I tried to leave all attributes from the array provided in the documentation, with the exception of html and attachments , but the result is the same. - sp7
  • In our project, the call is made through ObjectContent : var content = new ObjectContent(typeof(SendParamters), sendParamters, new JsonMediaTypeFormatter()); var result = client.PostAsync(uri, content); var content = new ObjectContent(typeof(SendParamters), sendParamters, new JsonMediaTypeFormatter()); var result = client.PostAsync(uri, content); Here, SendParameters is a C # class that describes JSON attributes. Type public class User { public string name { get; set; } public string email { get; set; } } public class SendParameters { public string text { get; set; } public string html { get; set; } public string subject { get; set; } public User from { get; set; } public IEnumerable<User> to { get; set; } ...} public class User { public string name { get; set; } public string email { get; set; } } public class SendParameters { public string text { get; set; } public string html { get; set; } public string subject { get; set; } public User from { get; set; } public IEnumerable<User> to { get; set; } ...} public class User { public string name { get; set; } public string email { get; set; } } public class SendParameters { public string text { get; set; } public string html { get; set; } public string subject { get; set; } public User from { get; set; } public IEnumerable<User> to { get; set; } ...} . Try it. - Mark Shevchenko

2 answers 2

Download a sample implementation of the REST API for C # at https://github.com/sendpulse/sendpulse-rest-api-csharp

Then use this sample code to send the letter:

  // https://login.sendpulse.com/settings/#api private static string userId = "_ВАШ_userId_"; private static string secret = "_ВАШ_secret_"; static void Main(string[] args) { Sendpulse sp = new Sendpulse(userId, secret); Dictionary<string, object> from = new Dictionary<string, object>(); from.Add("name", "SENDER_NAME"); from.Add("email", "SENDER_EMAIL@domain.com"); ArrayList to = new ArrayList(); Dictionary<string, object> elementto = new Dictionary<string, object>(); elementto.Add("name", "Test email"); elementto.Add("email", "test@test.com"); to.Add(elementto); Dictionary<string, object> emaildata = new Dictionary<string, object>(); emaildata.Add("html", "<b>Hello</b>"); emaildata.Add("text", "Hello!"); emaildata.Add("subject", "Send SMTP email"); emaildata.Add("from", from); emaildata.Add("to", to); Dictionary<string, object> result = sp.smtpSendMail(emaildata); Console.WriteLine("Response Status {0}", result["http_code"]); Console.WriteLine("Result {0}", result["data"]); Console.ReadKey(); } 

Alternatively, connect the SendPulse REST API classes to your project by downloading them from the SendPulse.restapi link . To connect, use:

 using Sendpulse_rest_api.restapi; 
  • Thanks, it helped. - sp7

If you have a simple text letter, then you can substitute an empty line for the "html" field. This is still a required parameter.