sendgrid / sendgrid-csharp

The Official Twilio SendGrid C#, .NetStandard, .NetCore API Library
https://sendgrid.com
MIT License
1.08k stars 585 forks source link

New templates not delivering the email #510

Closed sushant2517 closed 7 years ago

sushant2517 commented 7 years ago

Issue Summary

I created some new templates using the new Html based template editor. But my code (that uses the Web class) is not working for these templates. The old templates are still working with the same code.

Technical details:

            var mailMessage = new SendGridMessage();
            to.ToList().ForEach(toAddr =>
            {
                mailMessage.AddTo(toAddr);
            });

            if (cc != null && cc.Any())
            {
                cc.ToList().ForEach(ccAddr =>
                {
                    mailMessage.AddCc(ccAddr);
                });
            }

            if (bcc != null && bcc.Any())
            {
                bcc.ToList().ForEach(bccAddr =>
                {
                    mailMessage.AddBcc(bccAddr);
                });
            }

            if (String.IsNullOrWhiteSpace(fromAddress))
            {
                fromAddress = FromAddress;
                fromName = FromName;
            }

            mailMessage.From = new MailAddress(fromAddress, fromName);
            mailMessage.Subject = "Some subject";

            mailMessage.Html = " ";

            mailMessage.EnableTemplateEngine("template-id");
            var substitutions = GetSubstitutions(data);

            substitutions.Keys.ToList().ForEach(key =>
            {
                mailMessage.AddSubstitution($"%{key}%", new List<string>()
                {
                    substitutions[key]
                });
            });

            await _sendGridTransport.DeliverAsync(mailMessage);
thinkingserious commented 7 years ago

Hello @sushant2517,

Can you please check what version of the library you are using? Once you upgrade to the latest version, this example should work for you.

With Best Regards,

Elmer

sushant2517 commented 7 years ago

Thanks for your response. I will give it a try. But looking at the example, it uses the SendGridApiClient and not the Web (ITransport) class method that i have implemented. The older templates are still working using this. Do you mean the Web class is no longer supported for old templates? That causes serious compatibility issues for us. I would have thought it to be backwards compatible.

thinkingserious commented 7 years ago

Hi @sushant2517,

We had a major breaking change some time ago when our underlying API was upgraded from v2 to v3. Here is some information on that change.

That said, we have not deprecated v2 and it will not go away, but it is no longer supported by our team. To continue using v2, please follow these instructions.

Perhaps you keep things the way they are for the old templates and use the new library for the new ones.

With Best Regards,

Elmer

sushant2517 commented 7 years ago

Thanks Elmer for your response! So, does that mean that the new templates would not work with the v2 api? (the one that uses the Web class's DeliverAsync method). It does not throw any error (nor any blocked requests etc.), just silently ignores. Not a very elegant way to handle the requests IMO. Is it possible to migrate all the older templates to the new version. We have quite a few of them and would be pretty time consuming to bring them all over to the new version.

Thanks again for your help.

sushant2517 commented 7 years ago

hi @thinkingserious , I tried the above example, but i still don't get the email delivered. Here is the response I received after the SendEmailAsync call:

{"StatusCode":202,"Body":{"Headers":[{"Key":"Content-Length","Value":["0"]},{"Key":"Content-Type","Value":["text/plain; charset=utf-8"]}]},"Headers":[{"Key":"Connection","Value":["keep-alive"]},{"Key":"X-Message-Id","Value":["HFZkzSVnTDSDPVICLh8T1A"]},{"Key":"Access-Control-Allow-Origin","Value":["https://sendgrid.api-docs.io"]},{"Key":"Access-Control-Allow-Methods","Value":["POST"]},{"Key":"Access-Control-Allow-Headers","Value":["Authorization, Content-Type, On-behalf-of, x-sg-elas-acl"]},{"Key":"Access-Control-Max-Age","Value":["600"]},{"Key":"X-No-CORS-Reason","Value":["https://sendgrid.com/docs/Classroom/Basics/API/cors.html"]},{"Key":"Date","Value":["Wed, 30 Aug 2017 10:14:43 GMT"]},{"Key":"Server","Value":["nginx"]}]}

Here is the new code:

var mailMessage = new SendGridMessage();
            to.ToList().ForEach(toAddr =>
            {
                mailMessage.AddTo(toAddr);
            });

            if (cc != null && cc.Any())
            {
                cc.ToList().ForEach(ccAddr =>
                {
                    mailMessage.AddCc(ccAddr);
                });
            }

            if (bcc != null && bcc.Any())
            {
                bcc.ToList().ForEach(bccAddr =>
                {
                    mailMessage.AddBcc(bccAddr);
                });
            }

            if (String.IsNullOrWhiteSpace(fromAddress))
            {
                fromAddress = FromAddress;
                fromName = FromName;
            }

            mailMessage.SetFrom(fromAddress, fromName);
            mailMessage.Subject = subjectTemplate;

            mailMessage.SetTemplateId("template-id");

            var substitutions = GetSubstitutions(data);

            substitutions.Keys.ToList().ForEach(key =>
            {
                mailMessage.AddSubstitution($"%{key}%", substitutions[key]);
            });
            var response = await _sendGridClient.SendEmailAsync(mailMessage);
thinkingserious commented 7 years ago

Hello @sushant2517,

A status of 202 means the email should have been delivered, if not, you may want to reach out to our support team to take a deeper look at your account.

Also, to make sure the recipient emails are getting set correctly, you may want to try this.

With Best Regards,

Elmer

Niladri24dutta commented 7 years ago

@sushant2517 what is the object type for to in the below code

to.ToList().ForEach(toAddr =>
            {
                mailMessage.AddTo(toAddr);
            });

It should be an object of type EmailAddress class . Can you post the serialized version of the mailMessage just before sending the mail with something like below

Debug.WriteLine(mailMessage.Serialize())

sushant2517 commented 7 years ago

Hi Niladri,

I am using the overload of the AddTo method that takes the address as string. Here is the serialized Message request:

{"from":{"name":"RefGrid Admin","email":"admin@refgrid.com"},"subject":null,"personalizations":[{"to":[{"name":null,"email":"skulkarni.net@gmail.com"}],"cc":null,"bcc":null,"subject":"RefGrid: Job Archived","headers":null,"substitutions":{"%ReferrerName%":"Sushant Kulkarni","%JobTitle%":"Test Job 11023","%NetworkTitle%":"Tet .NET Network 11023"},"custom_args":null,"send_at":null}],"content":[{"type":"text/html","value":" "}],"attachments":null,"template_id":"ed2a0f9b-6b38-44fe-b5b0-8d1eb951e741","headers":null,"sections":null,"categories":null,"custom_args":null,"send_at":null,"asm":null,"batch_id":null,"ip_pool_name":null,"mail_settings":null,"tracking_settings":null,"reply_to":null}

The response looks like this: {"StatusCode":202,"Body":{"Headers":[{"Key":"Content-Length","Value":["0"]},{"Key":"Content-Type","Value":["text/plain; charset=utf-8"]}]},"Headers":[{"Key":"Connection","Value":["keep-alive"]},{"Key":"X-Message-Id","Value":["RR-1Bq_xRLW0O0C0DIlMhg"]},{"Key":"Access-Control-Allow-Origin","Value":["https://sendgrid.api-docs.io"]},{"Key":"Access-Control-Allow-Methods","Value":["POST"]},{"Key":"Access-Control-Allow-Headers","Value":["Authorization, Content-Type, On-behalf-of, x-sg-elas-acl"]},{"Key":"Access-Control-Max-Age","Value":["600"]},{"Key":"X-No-CORS-Reason","Value":["https://sendgrid.com/docs/Classroom/Basics/API/cors.html"]},{"Key":"Date","Value":["Wed, 30 Aug 2017 20:32:06 GMT"]},{"Key":"Server","Value":["nginx"]}]}

Still, the email is not delivered. I tried sending the test email using this template and it works. The older templates still work with the v2 sdk.

Thanks for your help.

thinkingserious commented 7 years ago

@sushant2517,

Do you see any email activity here? If not, there may be something specific to your account that is amiss since a 202 response means that your message was received by SendGrid and queued for delivery. In that case, our support team has the necessary tools to help debug.

With Best Regards,

Elmer

sushant2517 commented 7 years ago

I dont see any activity in any of the sections.

thinkingserious commented 7 years ago

@sushant2517,

In that case, there may be something wrong with your particular account. Please reach out to our support team so that they can look deeper into your account.

With Best Regards,

Elmer