我正在开发一个 MVC5 项目,客户有兴趣使用 MailChimp 发送电子邮件。我探索了 MailChimp 和包装器 (MailChimp.NET),并在我的项目中进行了尝试。我也测试了 REST API,它似乎可以工作,例如;我能够使用 REST API 获取列表和模板。但是,我在通过 MailChimp 发送电子邮件时仍然遇到问题。
到目前为止,我已经尝试了以下代码及其工作原理。现在我想给新注册的用户发送一封电子邮件。请给我详细的代码示例,我怎样才能实现这一点,因为我在这里完全震惊了..
var apiKey = "myapikey-us11";
var listId = "mylistid";
var subscribeRequest = new
{
apikey = apiKey,
id = listId,
email = new
{
email = "[email protected]"
},
double_optin = true,
};
var requestJson = JsonConvert.SerializeObject(subscribeRequest);
var reqresult = CallMailChimpApi("lists/", requestJson);
CallMailChimApi
private static string CallMailChimpApi(string method, string requestJson)
{
var endpoint = String.Format("https://{0}.api.mailchimp.com/3.0/{1}", "us11", method);
var wc = new WebClient();
try
{
return wc.UploadString(endpoint, requestJson);
}
catch (WebException we)
{
using (var sr = new StreamReader(we.Response.GetResponseStream()))
{
return sr.ReadToEnd();
}
}
}
我使用了这个功能并且成功了
public void SendEmailByApiMailChimp ()
{
try
{
string UserEmail = " [email protected] ";
MailChimpManager mc = new MailChimpManager("16d***********-us14");
EmailParameter email = new EmailParameter()
{
Email = UserEmail
};
EmailParameter resulte = mc.Subscribe("yourlistnumber", email);
var test = resulte;
}
catch (Exception ex)
{
var ters = ex;
}
}