【问题标题】:SendGrid API for creating and sending Marketing Campaign fails as Unsubscribe Tag is missing用于创建和发送营销活动的 SendGrid API 失败,因为缺少取消订阅标签
【发布时间】:2018-10-17 09:28:18
【问题描述】:

我正在开发一个 C# 控制台应用程序,以便创建活动并将其发送给收件人。为此,我一直在使用 SendGrid Web API V3。

这是我的 C# 代码:

var apiKey = Environment.GetEnvironmentVariable("MY_SENDGRID_API_KEY");
var sendGridClient = new SendGridClient(apiKey);

var campaign = new Campaign
{
    Categories = new string[] { }, 
    UnsubscribeUrl = "",
    HtmlContent = File.ReadAllText("Template/Contacts.html"),
    IpPool = "",
    SenderId = 339919,
    ListIds = new int[] { 5551303 },
    PlainContent = "A Sample Plain Content for the email",
    SegmentIds = new int[] { },
    Subject = "My Sample Campaign",
    SuppressionGroupId = 7726,
    Title = "My Sample Campaign",
};

var errors = new List<string>();
var json = JsonConvert.SerializeObject(campaign);

var response = await sendGridClient.RequestAsync(
                   method: SendGridClient.Method.POST,
                   urlPath: "campaigns",
                   requestBody: json);

var data = JsonConvert.DeserializeObject<Campaign>(
    response.Body.ReadAsStringAsync().Result,
    new JsonSerializerSettings
    {
        NullValueHandling = NullValueHandling.Include,
        Error = delegate (object sender, Newtonsoft.Json.Serialization.ErrorEventArgs args)
            {
                errors.Add(args.ErrorContext.Member.ToString());
                args.ErrorContext.Handled = true;
            }
    });

var campaignSendUrl = "campaigns/" + data.Id + "/schedules/now";
var response = await sendGridClient.RequestAsync(
                   method: SendGridClient.Method.POST,
                   urlPath: campaignSendUrl);

在上面的代码中,对于电子邮件正文,我准备了一个 HTML 文件,它被传递到“HtmlContent”属性中。

这是 HTML 代码:

<html>
<head>
    <title>PartiStaff</title>
    <style type="text/css">
        *, body, td, div {
            font-family: arial, sans-serif;
            font-size: 13pt;
            color: #404040;
        }

        td {
            padding: 3px 15px 3px 0;
        }

        #container {
            width: 100%;
            display: block;
            float: left;
        }

        #contents {
            width: 100%;
            min-height: 800px;
        }

        #footer {
            width: 100%;
            min-height: 800px;
        }

        img {
            border: none;
        }
    </style>
</head>
<body>
    <h1>Contact Form</h1>

    <div id="container">
        <div id="contents">            
            <table align="left" border="0" cellpadding="0" cellspacing="0" style="max-width: 730px; margin: 0px 0px 0px 50px; padding: 15px 0px;">
                <tr>
                    <td style="font-weight: bold; width: 90px;">First Name:</td>
                    <td>Hello [%first_name%]</td>
                </tr>
                <tr>
                    <td style="font-weight: bold;width: 90px;">Last Name:</td>
                    <td>[%last_name%]</td>
                </tr>
                <tr>
                    <td style="font-weight: bold;width: 90px;">Email:</td>
                    <td>[%email%]</td>
                </tr>
                <tr>
                    <td style="font-weight: bold;width: 90px;" valign="top">Sender Name:</td>
                    <td>[Sender_Name]</td>
                </tr>
                <tr>
                    <td style="font-weight: bold;width: 90px;" valign="top">Sender Address</td>
                    <td>[Sender_Address]</td>
                </tr>                
            </table>

            <p>If you would like to stop receiving these emails <a href="<%unsubscribe%>">click to unsubscribe here</a></p>
            <p>If you would like to stop receiving these emails <a href="<%asm_global_unsubscribe_raw_url%>">click to unsubscribe here</a></p>            
        </div>
    </div>
</body>
</html>

上面的代码可以创建一个没有任何错误的新广告系列,但它不能将广告系列发送给收件人。我一直发现以下错误。

{"errors":[{"field":"unsubscribe_tag","message":"请添加一个 [取消订阅] 标记您的内容。提供取消订阅选项是 根据商业电子邮件法规要求。"}]}

请告诉我如何解决这个问题。

【问题讨论】:

    标签: c# sendgrid sendgrid-api-v3


    【解决方案1】:

    您还需要在电子邮件的纯文本版本中包含取消订阅链接,例如在纯文本邮件内容的末尾包含此链接:

    “取消订阅此列表”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-10
      • 1970-01-01
      • 1970-01-01
      • 2017-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-02
      相关资源
      最近更新 更多