Sendgrid-csharp: A key or value in your X-SMTPAPI header is null

Created on 13 Mar 2016  路  23Comments  路  Source: sendgrid/sendgrid-csharp

Get this error, but i'm not using the SMTP api?

help wanted bug

All 23 comments

        var _sendgridTransport = new Web("SG.KEYHERE");

        var emailMessage = new SendGridMessage();
        emailMessage.AddTo(message.VendorEmailAddress);
        emailMessage.From = new MailAddress("[email protected]", "Name");
        emailMessage.Subject = "You've got mail";
        emailMessage.EnableTemplateEngine("IDHERE");
        emailMessage.Html = "Test";

        emailMessage.AddSubstitution("-customerName", new[] { message.CustomerName }.ToList());
        emailMessage.AddSubstitution("-customerEmailAddress", new[] { message.CustomerEmailAddress }.ToList());
        emailMessage.AddSubstitution("-customerPhoneNumber", new[] { message.CustomerPhoneNumber }.ToList());
        emailMessage.AddSubstitution("-serviceName", new[] { message.ServiceName }.ToList());

        _sendgridTransport.DeliverAsync(emailMessage).Wait();

@alexjamesbrown - facing same issue since last two days!
Anyone to the rescue?

I've contacted support about it, but not got a reply!

Sad :( Thanks @alexjamesbrown !

I am experiencing the exact same issue, If you do not use the EnableTemplateEngine it works. But you are left with a text only mail.

I was getting Invalid SMTP Header errors which turned out to be because I was using the wrong template id. Also, dont you have to set both the Html and Text properties of the message in order to send it?

Is there some answer for this issue? I'm getting the same problem and I can't figure out the problem here. I'm using the AddSubstitution method to try to dynamically substitute tags that are present in the template email. If I don't use this method the email is sent but with the tags as is.

No answer?

Hello Everyone,

I apologize for the delay. We are focused on releasing the next version of this library, which will be based on this HTTP client: https://github.com/sendgrid/csharp-http-client

That said, I've added this issue to our backlog.

In the mean time, have you tried what @rndthoughts suggested and making sure you have both HTML and text properties set?

If so, any further details, especially code snippets would help us get this solved quicker.

Didn't work for me, I gave up and rendered templates in my application, then sent the html through sendgrid.

Massive limitation -
Considering sticking with Mandrill, and paying for Mailchimp - will be cheaper than me working around this

I tried to set both the Html and Text properties as empty, but got the same issue...this is a massive limitation because I'm trying to have a provider that hangs the templating on their side.

myMessage.From = new MailAddress("[email protected]", "First Last");
myMessage.AddTo("[email protected]");
myMessage.Subject = "Sending with SendGrid is Fun";
myMessage.Html = " ";
myMessage.Text = " ";
myMessage.EnableTemplateEngine("SerialNumberTemplating");

foreach (var item in email.Parameters)
{
     var keyName = string.Format("-{0}-", item.Key.ToString());
     myMessage.AddSubstitution(keyName, new List<string>() { item.Value });
}

var credentials = new System.Net.NetworkCredential(username, password);
var transportWeb = new SendGrid.Web("stuff", credentials, new TimeSpan(0,0,59));
transportWeb.DeliverAsync(myMessage).Wait();

From my experience, you cannot set those values to empty or blank, you must put something in them.

To be honest, most of the values are set on the .AddSubstitution, I really don't want to set nothing on the html, text or even subject.

I agree with what you are saying, but i could not get it to work at all without setting html, text and subject properties. This seems to be a requirement according to the Sendgrid documentation.

So you're saying that I need to defined a specific value in this properties and get it to be nice on the template I have on the SendGrid?

I found the SendGrid docs very confusing:

Enabling a transactional template means that the subject and body content of your message will behave differently. If you want only the message鈥檚 content to be displayed, populate only the token in the template鈥檚 field. If you want only the template鈥檚 content to be displayed, leave the message field (subject or body) empty, and the template will populate.

It is best practice to provide content for both the html and the text properties in all of your emails.

The text property is substituted into the <%body%> of the text template and html is substituted into the <%body%> of the HTML template. If the text property is present, but not html, then the resulting email will only contain the text version of the template, not the HTML version. The <%subject%> property is used for both Text and HTML templates.

I found that I could not save my template unless i had <%body%> and <%subject%> tags present in the template on the SendGrid designer. When i attempted to use this library to send emails using my template, it would not send anything unless i populated the Body, Subject and Html properties before sending. I ended up bunging "Hello" in there to see what would happen!

Once i did this, i got my email (albeit with an unwanted piece of text in there). I ended up specifying a small chunk of the body text.

Tried to define something on that properties and I've got the same problem:

myMessage.From = new MailAddress("[email protected]", "First Last");
myMessage.AddTo("[email protected]");
myMessage.Subject = "Sending with SendGrid is Fun";
myMessage.Html = "somethig";
myMessage.Text = "something";
myMessage.EnableTemplateEngine("SerialNumberTemplating");

foreach (var item in email.Parameters)
{
     var keyName = string.Format("-{0}-", item.Key.ToString());
     myMessage.AddSubstitution(keyName, new List<string>() { item.Value });
}

var credentials = new System.Net.NetworkCredential(username, password);
var transportWeb = new SendGrid.Web("stuff", credentials, new TimeSpan(0,0,59));
transportWeb.DeliverAsync(myMessage).Wait();

I just posted an update on a related ticket: https://github.com/sendgrid/sendgrid-csharp/issues/157

Hello Everyone,

The new library is now available (in beta) as well as our new v3 /mail/send endpoint, please see:

I'm going to leave this ticket open until we can verify the functionality is working as advertised.

With Best Regards,

Elmer

Just in case anyone comes across this when searching there is still a "bug" if the substitution value is NULL. So in tiago-costa-farfetch's example above: I would change:

myMessage.AddSubstitution(keyName, new List<string>() { item.Value });

to

myMessage.AddSubstitution(keyName, new List<string>() { item.Value ?? string.Empty });

That will solve the error / problem for ya.

Thanks @zgirod,

I've re-opened this issue to investigate further.

I'm using SendGrid 6.3.4 so that I can get the .net 4.5 application to work with confirmation and password recovery: https://docs.microsoft.com/en-us/aspnet/identity/overview/features-api/account-confirmation-and-password-recovery-with-aspnet-identity.

Can't get the article to work in the latest version.

I am also facing the same issue: Value can not be null but I can not debug to see where the error is?

Thanks,
Graham

Hello @grahamoriley,

Have you tried compiling the example with .NET 4.5.2 as the target (that is the lowest version of .NET that Microsoft now supports). If you do that, you can use the v9 (current) version of this SDK.

With Best Regards,

Elmer

Was this page helpful?
0 / 5 - 0 ratings

Related issues

LorenDorez picture LorenDorez  路  4Comments

lebamb2 picture lebamb2  路  4Comments

digime99 picture digime99  路  5Comments

thinkingserious picture thinkingserious  路  4Comments

shoter picture shoter  路  4Comments