Skip to content

How to get the exact error details in response, to correct it quickly #430

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
vijay-pratap-singh opened this issue Apr 5, 2017 · 2 comments

Comments

@vijay-pratap-singh
Copy link

vijay-pratap-singh commented Apr 5, 2017

Issue Summary

Hi Team,

I am using the SendGrid Web API V3 SDK version with .NET Framework 4.5.2. I am using the SendGrid Dll version 9.1.0 for it.
Below is the code that is working fine.

        var apiKey = System.Environment.GetEnvironmentVariable("SENDGRID_APIKEY");
        var client = new SendGridClient(apiKey);
        var msg = new SendGridMessage()
        {
            From = new EmailAddress("[email protected]", "SendGrid Team"),
            Subject = "Hello World from the SendGrid CSharp SDK!",
            PlainTextContent = "Hello, Email!",
            HtmlContent = @"<strong>Dear Valued Customer</strong>"
        };
        msg.AddTo(new EmailAddress("[email protected]", "Sendgrid poc"));

        //Add this block of code for attachment - START
        List<Attachment> itms = new List<Attachment>();
        Attachment itm;

        Attachment itm = new Attachment();
        itm.Filename = "test";
        itm.Content = "iVBORw0KGgoAAAANSUhEUgAAACQAAAAkCAQAAABLCVATAAACH0lEQVR42q3WoZKrMBQGYGRkIpHEoY9DMrh1nUGtzxPcGV7gCsTaK3iBCqa2ipmrVqLrWrmytjL3nBwoEGD30ja/6JaSj/wp3SEIXjpUoB+Oeg0zpoR+NsyoDVOgi39cbYHAy4MQTc0wOYZepxRBUkn9UxxEiNnXxyYwd6w/438hSddHJilv1tqv664Shle1DeJaJihPV9uNQ+NWBRK2QVSr+GjtaFzOIpdjKFShnoY+Gv0N0u0OVLexY48NQ+68JchdpQu/o1piVMu6faJdwjNWIAYyl55bqGUtbndO53TzCIpUpCkdlEm+V3J3Ir8r3uops2+FkTmvx832IGJwN97xS/5Ti0LQ/WLwtbxMal2ueAwvc2c8CAgSJip5U4+tKHECMlUzq2UcA9EyROuJi6/71dtzWAfVcq0Jw1CsYh13kDDteVoirE+zWtLVinQ8ZAS5YlVlvRHWfi3pakUQL0OOwmp/W/vN6Gt5zBIkzEezxnCtMJsxDIECTYmhp3bej4HHzaalNMyAnzE0UBKp6Z1Do2pwd3JkAH6CxlTs/bZOZ661yMwhohDLQqREMWz8UAvWoUQleggehG5dSPUbv28GJlnKHGJsqPi7vuG/MGTyCGslOtkCOayrGOa/indajdudb6FUpXoepgiLHIIMriddyzrkMBhGAqlOH4U2hKCT2j0NdU8jFbzpZ3LQlh9srPqEQ1Y9lEP2CVa99KHvH8mnrGGdl9V9AAAAAElFTkSuQmCC";
        itm.ContentId = "1";
        itm.Disposition = "attachment";
        itm.Type = "image/jpeg";
        itms.Add(itm);

        msg.AddAttachments(itms);
        //Add this block of code for attachment - END
        
        var response = await client.SendEmailAsync(msg);

        emailStatusCode = response.StatusCode.ToString();
        if (HttpStatusCode.Accepted == response.StatusCode)
        {
            //mail sent
        }
        else
        {
            //mail not sent; for any reason
        }

Steps to Reproduce

  1. I had changed valid from email ([email protected]) to invalid from email(from@example) and forgot this change.
  2. Then, I implemented the code for the adding the attachment.
  3. Now, when I run the code to send the attachment, I get the "BadRequest" in response as a status code.

I forgot about the from email address changes and I was thinking that I`m doing something wrong in attachment coding. This wasted my whole day.

So, for this scenario I have few questions.

  1. Is there any way to get or track the error details in the above code? It will really help me in saving the development time.
  2. If there as any code, could you please share with me.
  3. As per mentioned error status codes here (https://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html), I`m getting the response reason in response status code instead of response code. Is it something like it?

Technical details:

  • sendgrid-csharp Version: master (latest commit: [commit number])
  • .NET Version: 4.5.2
@SendGridDX
Copy link

Hello @vijay-pratap-singh,

I'm very sorry you had such a bad experience :(

In the case of retrieving the error message, you can try this.

Also, better error handling is coming. Please follow this issue for updates.

With Best Regards,

Elmer

@vijay-pratap-singh
Copy link
Author

vijay-pratap-singh commented Apr 6, 2017

Yes Elmer. Below code worked for me. It is very useful to track the error details.

Console.WriteLine(response.Body.ReadAsStringAsync().Result); // The message will be here
Console.WriteLine(response.Headers.ToString());

Thanks a lot for the prompt help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants