0

I am trying add a subscriber to a list but I'm struggling to implement it without any example code. can anybody help me with the example?

S R
  • 1
  • 1
  • 1
  • I used this for awhile and ended up scrapping the mailchip.net implementation. I'd get random exceptions using it... – Jared Aug 31 '15 at 21:49
  • The MailChimp API is not the greatest thing to work with. You might want to consider using the MailChimp.NET nuget package. – Brandon Aug 31 '15 at 21:52
  • possible duplicate of [Adding subscribers to a list using Mailchimp's API v3](http://stackoverflow.com/questions/30481979/adding-subscribers-to-a-list-using-mailchimps-api-v3) – TooMuchPete Sep 02 '15 at 04:50

1 Answers1

1

Inspired by this video: MailChimp.NET Tutorial: Create, Edit And Delete List Members - here my test code to add a subscriber to a 'given list'. The subscriber will receive an email asking to confirm the subscription. After that confirmation the new subscriber will be listed in mailchimp campaign list. (used version mailchimp.net wrapper v:3 with newtonsoft.json version 10.0.3) - this worked for me.

private static readonly IMailChimpManager Manager = new MailChimpManager(ApiKey);

    public async Task AddSubscriberToCampaignList(string emailAddress, string listName, string fname, string lname)
    {
        try
        {
            var listsAwaitable = Manager.Lists.GetAllAsync().ConfigureAwait(false);
            var list = listsAwaitable.GetAwaiter().GetResult().FirstOrDefault(l =>
                            l.Name.Equals(listName, StringComparison.CurrentCultureIgnoreCase));

        if (list != null)
        {
            //the subscriber
            var member = new Member
            {
                EmailAddress = emailAddress,
                StatusIfNew = Status.Pending,
                EmailType = "html",
                TimestampSignup = DateTime.UtcNow.ToString("s"),
            };

            if (fname != null && lname != null)
            {
                var subscriberName = new Dictionary<string, object>
                {
                    {"FNAME", fname},
                    {"LNAME", lname}
                };
                member.MergeFields = subscriberName;
            }

            string campaignListKey = list.Id;
            await Manager.Members.AddOrUpdateAsync(campaignListKey, member);
        }
    }
    catch (MailChimpException e)
    {
        throw;
    }
Fran Ka
  • 29
  • 1
  • 3