我正在开发C#控制台应用程序,以创建Campaign并将其发送给收件人。为此,我一直在使用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”,“消息”:“请添加 [取消订阅]标签到您的内容。提供退订选项是 根据商业电子邮件法规是必需的。“}]}
请让我知道如何解决此问题。
答案 0 :(得分:0)
您还需要在电子邮件的纯文本版本中包括取消订阅链接,例如,在纯文本邮件内容的末尾包括以下内容:
“从此列表中退订<%asm_group_unsubscribe_raw_url%>”