用于创建和发送营销活动的 SendGrid API 失败,因为缺少取消订阅标签



我正在开发一个 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>

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

{">

错误":[{"字段":"unsubscribe_tag","消息":"请添加一个 [取消订阅] 标记。提供取消订阅选项是 根据商业电子邮件法规的要求。"}]}

请让我知道我该如何解决这个问题。

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

"取消订阅此列表 <%asm_group_unsubscribe_raw_url%>">

最新更新