您需要在您的 aspx 页面中有一个订阅按钮
<asp:Button ID="btnSubscribe" CssClass="btn btn-default" OnClick="btnSubscribe_Click" runat="server" Text="Go" />
在代码隐藏中
除了密钥之外,您还需要有 MailChimp dll 需要引用到您的项目
protected void btnSubscribe_Click(object sender, EventArgs e)
{
try
{
// Keys for Mail chimp , when you signup in their site
// there you can get these keys and i stored them in webconfig
string apiKey = Convert.ToString(ConfigurationManager.AppSettings["MailChimpApiKey"]);
string listId = Convert.ToString(ConfigurationManager.AppSettings["MailChimpListId"]);
var options = new List.SubscribeOptions();
options.DoubleOptIn = true;
options.EmailType = List.EmailType.Html;
options.SendWelcome = false;
var mergeText = new List.Merges(txtSubscribe.Text.Trim(), List.EmailType.Text)
{
{"FNAME", "webruster"},
{"LNAME", "Web"}
};
var merges = new List<List.Merges> { mergeText };
var mcApi = new MCApi(apiKey, false);
var batchSubscribe = mcApi.ListBatchSubscribe(listId, merges, options);
if (batchSubscribe.Errors.Count > 0)
// Console.WriteLine("Error:{0}", batchSubscribe.Errors[0].Message);
ScriptManager.RegisterStartupScript(btnSubscribe, btnSubscribe.GetType(), "subscribe", "alert('Given email address is already subscribed, thank you!')", true);
// ScriptManager.RegisterStartupScript(btnSubscribe, btnSubscribe.GetType(), "subscribe", "alert('"+ batchSubscribe.Errors[0].Message + @"')", true);
else
// Console.WriteLine("Success");
ScriptManager.RegisterStartupScript(btnSubscribe, btnSubscribe.GetType(), "subscribe", "alert('You are subscribed to newsletter successfully.')", true);
}
catch (Exception ex)
{
// ErrorLogger.ClientErrorLogger(ex);
throw ex;
}
}