【问题标题】:Sendgrid C# bulk email X-SMTPAPI header not workingSendgrid C# 批量电子邮件 X-SMTPAPI 标头不起作用
【发布时间】:2016-06-19 20:00:43
【问题描述】:

我正在尝试使用 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);

但它似乎只是忽略了我的标题,并将电子邮件发送到“addto”中使用的一个电子邮件“emailaddress4”。 根据文档,如果标头 JSON 解析错误,则 SendGrid 会向“FROM”字段中设置的电子邮件地址发送一封有关错误的电子邮件,但我没有收到有关任何错误的电子邮件。

有人知道吗?

【问题讨论】:

    标签: c# asp.net email sendgrid


    【解决方案1】:

    我不确定为什么您的 FROM 地址不会收到任何错误,但您的 JSON 包含以下缺陷:

    • ,接近结尾使字符串无效的json
    • %name% 中第一个 % 周围的空格,这可能会使 sendgrid 认为它是无效的替换标记
    • 如果您使用 X-SMTPAPI 标头指定多个收件人,则不应使用 AddTo() 添加标准 SMTP TO

    除此之外,您没有将标头包装为 72 个字符(请参阅文档中的 example)。

    【讨论】:

    • 感谢您的回答,但基本上,它不在 JSON 周围,因为我在此处提问之前验证了这一点。只是打错了。我更新了上面的代码......据我在文档中看到的标准 SMTP TO 是必需的参数并且需要有效。我还添加了换行,没有任何改变,我的 X-SMTPAPI 标头没有任何变化
    【解决方案2】:

    我认为,但是 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-27
      • 1970-01-01
      • 2016-09-07
      • 1970-01-01
      • 2013-03-01
      • 2021-03-22
      • 1970-01-01
      相关资源
      最近更新 更多