在ASP.NET用户添加到列表中的邮件黑猩猩3.0 [英] Adding subscribers to a list in Mail Chimp 3.0 in ASP.NET

查看:259
本文介绍了在ASP.NET用户添加到列表中的邮件黑猩猩3.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现邮件黑猩猩的新的API与我的ASP.NET C#网站,所以当用户输入自己的电子邮件地址,它会自动添加到我的mailchimp列表中的输入框。我曾尝试过各种其他的方法但是这些都没有工作过。



我已经试过它扔了405无法使用该方法响应的Web客户端和HttpClient的其中抛出一个错误。在出演GetStringAsync方法调用,因为它不是一个任务



到目前为止我的代码的详细情况如下:

 公共BOOL BatchSubscribe(IEnumerable的< MailChimpSubscriberModel> newSubscribers)
{
如果(string.IsNullOrEmpty(_defaultListId))抛出新的ArgumentNullException(Res.Resource.MailChimpIntegrationNoListId);

如果(string.IsNullOrEmpty(_apiKey))抛出新的ArgumentNullException(Res.Resource.MailChimpIntegrationNoApiKey);

的foreach(MailChimpSubscriberModel用户在newSubscribers)
{
字符串URL =https://开头+ _dataPoint +.api.mailchimp.com / 3.0 /列表/+ _defaultListId +/;

认购成员=新用户();
member.email = subscriber.Email;
member.subscribed =订阅;

串jsonString =新的JavaScriptSerializer()序列化(成员);

//使用(Web客户端的客户端=新的WebClient())
// {
// client.Headers [HttpRequestHeader.ContentType] =应用/的X WWW的形式-urlencoded;
// client.Headers [HttpRequestHeader.Authorization] =新AuthenticationHeaderValue(基本,_apiKey)的ToString();
//字符串HtmlResult =客户端(URL,jsonString)。

//返回true;
//}

使用(VAR HTTP =新的HttpClient())
{
http.DefaultRequestHeaders.Authorization =
新AuthenticationHeaderValue(基本 ,_apiKey);
字符串内容=等待HTTP ** ** GetStringAsync(@https://us11.api.mailchimp.com/3.0/lists);
Console.WriteLine(内容);
}
}
返回FALSE;
}


解决方案

我是有点晚对这个问题,但我花了半天时间来弄明白,这里有我的答案,因此它可以帮助别人。我使用MailChimp 3.0:

 私人无效subscribeAddress()
{
串apiKey =APIKEY -usX; //由您创建API密钥。
字符串的数据中心=USX;
串listID =listID; //你想用列表的ListID。

SubscribeClassCreatedByMe subscribeRequest =新SubscribeClassCreatedByMe
{
EMAIL_ADDRESS =somebodys@email.com,
状态=订阅
};
subscribeRequest.merge_fields =新MergeFieldClassCreatedByMe();
subscribeRequest.merge_fields.FNAME =YOURNAME;
subscribeRequest.merge_fields.LNAME =YourSurname;使用(HttpClient的客户端=新的HttpClient())
{
变种的uri =

的https://+数据中心+.api.mailchimp.com /;
VAR端点=3.0 /列表/+ listID +/成员;

client.DefaultRequestHeaders.Authorization =新System.Net.Http.Headers.AuthenticationHeaderValue(基本,apiKey);
client.DefaultRequestHeaders.Accept.Add(新System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(应用/ JSON));
client.BaseAddress =新的URI(URI);

//注意:为了避免成为方法异步''我们进入'。结果'
HttpResponseMessage响应= client.PostAsJsonAsync(端点,subscribeRequest)。结果; // PostAsJsonAsync序列化的方法一个对象
// JSON,然后发送有效载荷JSON POST请求中的
//的StatusCode == 200
// - >状态==订阅 - >在名单上,现在
// - >状态==退订 - >这个地址用于在名单上,但不再
// - >状态==待定 - >与双选入加入要求该地址,但尚未证实
// - >状态==清理 - >这个地址反弹,然后从列表

//的StatusCode == 404
已被删除,如果((response.IsSuccessStatusCode))
{
//你的代码在这里
}
}
}

下面有类SubscribeClassCreatedByMe和MergeFieldClassCreatedByMe

 命名空间的用户
{
公共类SubscribeClassCreatedByMe
{
公字符串EMAIL_ADDRESS {搞定;组; }
公共字符串状态{搞定;组; }
公共MergeFieldClassCreatedByMe merge_fields {搞定;组; }
}
}
认购命名空间
{
公共类MergeFieldClassCreatedByMe
{
公共字符串FNAME {搞定;组; }
公共字符串LNAME {搞定;组; }
}
}

好了,我希望这帮助!


I am trying to implement Mail Chimp's new API with my ASP.NET C# website so when a user enters their email address into an input box it will be added to my mailchimp list automatically. I have tried various other methods however none of these have worked.

I have tried a Web Client which threw a 405 Cannot use that Method response and a HttpClient which threw an error on the starred GetStringAsync method call because its not a task.

My code so far is detailed below:

public bool BatchSubscribe(IEnumerable<MailChimpSubscriberModel> newSubscribers)
{   
    if (string.IsNullOrEmpty(_defaultListId)) throw new ArgumentNullException(Res.Resource.MailChimpIntegrationNoListId);

    if (string.IsNullOrEmpty(_apiKey)) throw new ArgumentNullException(Res.Resource.MailChimpIntegrationNoApiKey);

    foreach (MailChimpSubscriberModel subscriber in newSubscribers)
    {
        string url = "https://" + _dataPoint + ".api.mailchimp.com/3.0/lists/" + _defaultListId + "/";

        Subscriber member = new Subscriber();
        member.email = subscriber.Email;
        member.subscribed = "subscribed";

        string jsonString = new JavaScriptSerializer().Serialize(member);

        //using (WebClient client = new WebClient())
        //{
        //    client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
        //    client.Headers[HttpRequestHeader.Authorization] = new AuthenticationHeaderValue("Basic", _apiKey).ToString();
        //    string HtmlResult = client.(url, jsonString);

        //    return true;
        //}

        using (var http = new HttpClient())
        {
            http.DefaultRequestHeaders.Authorization =
                 new AuthenticationHeaderValue("Basic", _apiKey);
            string content = await http.**GetStringAsync**(@"https://us11.api.mailchimp.com/3.0/lists");
            Console.WriteLine(content);
        }
    }
    return false;
}

解决方案

I'm a bit late to this question, but as it took me half a day to figure it out, here there is my answer, so it can help others. I'm using MailChimp 3.0:

private void subscribeAddress() 
{
  string apiKey = "APIKEY-usX"; //your API KEY created by you.
  string dataCenter = "usX";
  string listID = "listID"; //The ListID of the list you want to use.

  SubscribeClassCreatedByMe subscribeRequest = new SubscribeClassCreatedByMe 
  {
      email_address = "somebodys@email.com",
      status = "subscribed"
  };
  subscribeRequest.merge_fields = new MergeFieldClassCreatedByMe();
  subscribeRequest.merge_fields.FNAME = "YourName";
  subscribeRequest.merge_fields.LNAME = "YourSurname";

  using (HttpClient client = new HttpClient())
  {
      var uri = "https://" + dataCenter + ".api.mailchimp.com/";
      var endpoint = "3.0/lists/" + listID + "/members";

      client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", apiKey);
      client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
      client.BaseAddress = new Uri(uri);

      //NOTE: To avoid the method being 'async' we access to '.Result'
      HttpResponseMessage response = client.PostAsJsonAsync(endpoint, subscribeRequest).Result;//PostAsJsonAsync method serializes an object to 
                                                                                            //JSON and then sends the JSON payload in a POST request
    //StatusCode == 200
    // -> Status == "subscribed" -> Is on the list now
    // -> Status == "unsubscribed" -> this address used to be on the list, but is not anymore
    // -> Status == "pending" -> This address requested to be added with double-opt-in but hasn't confirmed yet
    // -> Status == "cleaned" -> This address bounced and has been removed from the list

    //StatusCode == 404
    if ((response.IsSuccessStatusCode))
    {
       //Your code here
    }
  }
}

Here there are the classes SubscribeClassCreatedByMe and MergeFieldClassCreatedByMe

namespace Subscriber
{
  public class SubscribeClassCreatedByMe 
  {
    public string email_address { get; set; }
    public string status { get; set; }
    public MergeFieldClassCreatedByMe merge_fields { get; set; }
  }
}
namespace Subscriber
{
  public class MergeFieldClassCreatedByMe
  {
    public string FNAME { get; set; }
    public string LNAME { get; set; }
  }
}

Well, I hope this help!!

这篇关于在ASP.NET用户添加到列表中的邮件黑猩猩3.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆