5
votes

I'm trying to send a plain text e-mail from a C# function; however, the resulting mail comes through with HTML tags. I've reduced this down to the following console app:

static async Task Main(string[] args)
{
    SendGridMessage msg = new SendGridMessage()
    {
        From = new SendGrid.Helpers.Mail.EmailAddress("[email protected]"),
        Subject = "test",
        PlainTextContent = "Hello"
    };
    msg.AddTo(new SendGrid.Helpers.Mail.EmailAddress("[email protected]", "test recipient"));            

    SendGridClient client = new SendGridClient("mykey");
    Response response = await client.SendEmailAsync(msg);

    Console.WriteLine(response.StatusCode);
    Console.ReadLine();

}

It sends the e-mail fine, but doing a view source on the e-mail shows it to be HTML formatted:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
<body>
<p>Hello</p>

<img src="https:// ... />
</body></html>

I've tried various combinations, including setting the HtmlContent to plain text (which results in a very different e-mail, but still not plain text), and a syntax such as:

var emailContent = new Content
{
    Type = "text/plain",
    Value = emailMessage
};
message.Content.Add(emailContent);

I know that there is no intermediary reformatting the message, as I can send a plain text e-mail to myself from outlook.

I'm using SendGrid 9.9.0, but I've tried 9.8.0 in case it's something that has recently changed.

Am I missing something here, or am I expecting SendGrid to be able to do something that it isn't capable of?

1
What is the value of the Contents property of the SendGridMessage, right before you send? My guess is that it contains HTML, and you simply need to remove that entry from the list. Just a guess, based on the source code. I've not used SendGrid myself. - mason
it is also very important question how do you do the operation view source of the message? - astaykov
Contents is null. Like I said above, if I populate contents manually, then it's whatever I set it to (e.g. "text/plain", "msg"), but it makes no difference to the resulting e-mail - Paul Michaels
@astaykov Right click on the message in outlook and select view source - Paul Michaels
Have you tried setting the mime type, i.e.msg.AddContent(MimeType.Text, "hello"); (obviously without setting the PlainTextContent when constructing the object) - Lennart Stoop

1 Answers

6
votes

Taken from the SendGrid online docs:

First, login to your SendGrid account, click on “Settings”, then “Mail Settings”, and drop down the Plain Content setting, and then place a check mark in the Don’t Convert Plaintext to HTML option. Be sure to save this setting.

The standard seems to be that e-mails are sent with both HTML and plain text, so a version of plain text is automatically converted to HTML as well.