locked
trying to send via smtp2go.com's api, but receiving a bad request RRS feed

  • Question

  • User-1188570427 posted

    I am trying to send via my email service API.

    I tried to make an C# object that I can populate and then convert to Json, but I guess my conversion isn't correct.

    Can someone help?

    Here is what they are expecting:

    {
        "api_key": "api-40246460336B11E6AA53F23C91285F72",
        "to": ["Test Person <test@example.com>"],
        "sender": "Test Persons Friend <test2@example.com>",
        "subject": "Hello Test Person",
        "text_body": "You're my favorite test person ever",
        "html_body": "<h1>You're my favorite test person ever</h1>",
        "custom_headers": [
          {
            "header": "Reply-To",
            "value": "Actual Person <test3@example.com>"
          }
        ]
    }

    Here is my C# Code:

            public async Task<EmailParamInfoDto> SendEmail(EmailParamInfoDto emailParamInfo)
            {
    
    
                Smtp2GoJsonEmailObjectDto smtp2GoJsonEmailObject2 = new Smtp2GoJsonEmailObjectDto()
                {
                    api_key = "api-Appkeyhere",
                    to = "MyToEmail",
                    sender = "MyFromEmail",
                    subject = string.Format("test @ {0}", DateTime.Now.ToString()),
                    text_body = "this is a test",
                    html_body = "<h1>You're my favorite test person ever</h1>",
                    custom_headers = new List<Smtp2GoCustomHeadersDto>
                    {
                        new Smtp2GoCustomHeadersDto
                        {
                            header = "Reply-To",
                            value = "ReplyToEmail"
                        }
                    }
                };
    
    
    
                string json = JsonConvert.SerializeObject(smtp2GoJsonEmailObject);
    
                using (var client = new HttpClient())
                {
                    var response = await client.PostAsync(
                        "https://api.smtp2go.com/v3/email/send",
                         new StringContent(json, Encoding.UTF8, "application/json"));
    
                    string jsonResponseTxt = JsonConvert.SerializeObject(response);
    
                    return new EmailParamInfoDto { IsSuccessful = true, ActualEmailSentAddressTxt = "", JsonResult = jsonResponseTxt };
                }          
            }

    I get a 400 error code bad request returned.

    What am I doing wrong?

        public class Smtp2GoJsonEmailObjectDto
        {
            public Smtp2GoJsonEmailObjectDto()
            {
                custom_headers = new List<Smtp2GoCustomHeadersDto>();
            }
    
            public string api_key { get;  set; }
            public string to { get; set; }
    
            public string sender { get; set; }
    
            public string subject { get; set; }
    
            public string text_body { get; set; }
            public string html_body { get; set; }
    
            public List<Smtp2GoCustomHeadersDto> custom_headers { get; set; }
        }
        public class Smtp2GoCustomHeadersDto
        {
            public string header { get; set; }
    
            public string value { get; set; }
        }

    Monday, November 12, 2018 1:17 AM

Answers

  • User475983607 posted

    tvb2727

    I get a 400 error code bad request returned.

    A 400 is usually an bug with the request format.  In this case, the C# DTO object model does not match the JSON; the to field is an array.

    Visual Studio has a tool that converts JSON to class libraries.  Copy the JSON.  In Visual Studio open cs file, click the Edit drop down menu, select Paste Special, and finally Paste JSON as Classes. 

    The results look similar to the following.

        public class Smtp2GoJsonEmailObjectDto
        {
            public string api_key { get; set; }
            public string[] to { get; set; }
            public string sender { get; set; }
            public string subject { get; set; }
            public string text_body { get; set; }
            public string html_body { get; set; }
            public Custom_Headers[] custom_headers { get; set; }
        }
    
        public class Custom_Headers
        {
            public string header { get; set; }
            public string value { get; set; }
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 12, 2018 2:23 AM

All replies

  • User475983607 posted

    tvb2727

    I get a 400 error code bad request returned.

    A 400 is usually an bug with the request format.  In this case, the C# DTO object model does not match the JSON; the to field is an array.

    Visual Studio has a tool that converts JSON to class libraries.  Copy the JSON.  In Visual Studio open cs file, click the Edit drop down menu, select Paste Special, and finally Paste JSON as Classes. 

    The results look similar to the following.

        public class Smtp2GoJsonEmailObjectDto
        {
            public string api_key { get; set; }
            public string[] to { get; set; }
            public string sender { get; set; }
            public string subject { get; set; }
            public string text_body { get; set; }
            public string html_body { get; set; }
            public Custom_Headers[] custom_headers { get; set; }
        }
    
        public class Custom_Headers
        {
            public string header { get; set; }
            public string value { get; set; }
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 12, 2018 2:23 AM
  • User-1188570427 posted

    tvb2727

    I get a 400 error code bad request returned.

    A 400 is usually an bug with the request format.  In this case, the C# DTO object model does not match the JSON; the to field is an array.

    Visual Studio has a tool that converts JSON to class libraries.  Copy the JSON.  In Visual Studio open cs file, click the Edit drop down menu, select Paste Special, and finally Paste JSON as Classes. 

    The results look similar to the following.

        public class Smtp2GoJsonEmailObjectDto
        {
            public string api_key { get; set; }
            public string[] to { get; set; }
            public string sender { get; set; }
            public string subject { get; set; }
            public string text_body { get; set; }
            public string html_body { get; set; }
            public Custom_Headers[] custom_headers { get; set; }
        }
    
        public class Custom_Headers
        {
            public string header { get; set; }
            public string value { get; set; }
        }

    YES! That was it! Thank you.

    It is working now :-)

    You help me a lot, mgehard!

    Monday, November 12, 2018 3:39 AM