我正在尝试使用SendGrid向ASP.Net C#Web应用程序中的多个收件人发送电子邮件
根据SendGrid文档,我需要在JSON格式的字符串中添加X-SMTPAPI标头到我的消息。我这样做,首先检查我刚刚在构建我的json电子邮件列表之前添加了一个手工键入的字符串,这里是我的代码:
string header = "{\"to\": [\"emailaddress2\",\"emailaddress3\"], \"sub\": { \"%name%\": [\"Ben\",\"Joe\"]},\"filters\": { \"footer\": { \"settings\": { \"enable\": 1,\"text/plain\": \"Thank you for your business\"}}}}";
string header2 = Regex.Replace(header, "(.{72})", "$1" + Environment.NewLine);
var myMessage3 = new SendGridMessage();
myMessage3.From = new MailAddress("emailaddress1", "FromName");
myMessage3.Headers.Add("X-SMTPAPI", header2);
myMessage3.AddTo("emailaddress4");
myMessage3.Subject = "Test subject";
myMessage3.Html = "Test message";
myMessage3.EnableClickTracking(true);
// Create credentials, specifying your user name and password.
var credentials = new NetworkCredential(ConfigurationManager.AppSettings["xxxxx"], ConfigurationManager.AppSettings["xxxxx"]);
// Create an Web transport for sending email.
var transportWeb = new Web(credentials);
// Send the email, which returns an awaitable task.
transportWeb.DeliverAsync(myMessage3);
但它似乎忽略了我的标题,并将电子邮件发送到一封电子邮件" emailaddress4"用于" addto"。 根据文档,如果标头JSON被错误地解析,那么SendGrid会将关于错误的电子邮件发送到" FROM"中设置的电子邮件地址。字段,但我没有收到有关任何错误的电子邮件。
有人有任何想法吗?
答案 0 :(得分:0)
我不确定为什么你不会在你的FROM地址收到任何错误,但你的JSON包含以下缺陷:
TO
。除此之外,您没有将标题包装为72个字符(请参阅文档中的example)。
答案 1 :(得分:0)
我认为,无论X-SMTPAPI文档是如何将标头作为JSON传递的,API本身都希望它作为参数,包含Ienumerable字符串。所以工作代码是:
var myMessage3 = new SendGridMessage();
myMessage3.From = new MailAddress("email4@email.com", "Test Sender");
myMessage3.AddTo("email2@email.com");
myMessage3.Subject = "Új klubkártya regisztrálva";
myMessage3.Html = "Teszt üzenet";
myMessage3.EnableClickTracking(true);
/* SMTP API
* ===================================================*/
// Recipients
var addresses = new[]{
"email2@email.com", "email3@email.com"
};
//string check = string.Join(",", addresses);
myMessage3.Header.SetTo(addresses);
// Create credentials, specifying your user name and password.
var credentials = new NetworkCredential(ConfigurationManager.AppSettings["xxxxxxx"], ConfigurationManager.AppSettings["xxxxxxxxx"]);
// Create an Web transport for sending email.
var transportWeb = new Web(credentials);
// Send the email, which returns an awaitable task.
transportWeb.DeliverAsync(myMessage3);