我将尝试从C#函数发送纯文本电子邮件;但是,结果的邮件带有HTML标记。我已将其简化为以下控制台应用程序:
它可以成功发送电子邮件,但在查看电子邮件的源代码时,发现它是HTML格式的:
我知道没有中介重新格式化消息,因为我可以从Outlook向自己发送纯文本电子邮件。
我正在使用SendGrid 9.9.0,但我已经尝试了9.8.0,以防最近发生了什么变化。
我是否漏掉了什么,或者我期望SendGrid能够做一些它无法做到的事情?
static async Task Main(string[] args)
{
SendGridMessage msg = new SendGridMessage()
{
From = new SendGrid.Helpers.Mail.EmailAddress("noreply@test.com"),
Subject = "test",
PlainTextContent = "Hello"
};
msg.AddTo(new SendGrid.Helpers.Mail.EmailAddress("my@address.com", "test recipient"));
SendGridClient client = new SendGridClient("mykey");
Response response = await client.SendEmailAsync(msg);
Console.WriteLine(response.StatusCode);
Console.ReadLine();
}
它可以成功发送电子邮件,但在查看电子邮件的源代码时,发现它是HTML格式的:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
<body>
<p>Hello</p>
<img src="https:// ... />
</body></html>
我尝试了各种组合,包括将HtmlContent设置为纯文本(这会导致非常不同的电子邮件,但仍不是纯文本),以及以下语法:
var emailContent = new Content
{
Type = "text/plain",
Value = emailMessage
};
message.Content.Add(emailContent);
我知道没有中介重新格式化消息,因为我可以从Outlook向自己发送纯文本电子邮件。
我正在使用SendGrid 9.9.0,但我已经尝试了9.8.0,以防最近发生了什么变化。
我是否漏掉了什么,或者我期望SendGrid能够做一些它无法做到的事情?
SendGridMessage
的Contents
属性的值是什么?我的猜测是它包含HTML,你只需要从列表中删除该条目。这只是一个猜测,基于源代码。我自己没有使用过SendGrid。 - masonmsg.AddContent(MimeType.Text, "hello")
(在构造对象时显然不需要设置PlainTextContent)。 - Lennart Stoop