Skip to content

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

Closed
@vijay-pratap-singh

Description

@vijay-pratap-singh

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions