Question

I am using the MailChimp.net api here: https://github.com/danesparza/MailChimp.NET

Here is the code I currently have, which works for subscribing users to my list, but obviously it throws an exception when the email already exists in the mailchimp list, and the API isn't fully complete yet, so not all mailchimp methods are available to me.

 //SUBSCRIBE IN MAILCHIMP
 MailChimpManager mc = new MailChimpManager("**API-KEY**");
 //  Create the email parameter
 EmailParameter subscriber = new EmailParameter()
 {
     Email = model.email
 };

 EmailParameter results = mc.Subscribe("**LIST-ID*", subscriber);

I've been trying to do something like this:

mc.GetMemberInfo("**LIST-ID**", "member email");

But i don't know the correct syntax or if that is even the correct method to use.

Mailchimp doesn't give me a clear cut way for handling exceptions, and I'm not the best backend developer. I know I need to check if the email exists in the list, just need a little help getting there.

Thanks all for any help!

Was it helpful?

Solution

You should be able to do something like this:

MailChimpManager mc = new MailChimpManager(** apikey **);

var emails = new List<EmailParameter>();
emails.Add(new EmailParameter() { Email = ** youremail ** });

var info = mc.GetMemberInfo(** listid **, emails);
var member = info.Data.SingleOrDefault();

if (member.Status == "subscribed") {
    ** email is subscribed **
}
else {
    ** email is not subscribed **
}

OTHER TIPS

Very nooby to Mailchimp and I use the following (knowing that I use the first list in the returned listresult)

        MailChimpManager mc = new MailChimpManager(WebConfigurationManager.AppSettings["OOooKey"]);
    ListResult lists = mc.GetLists();

    Matches match = mc.SearchMembers("anemailaddress.com.au", lists.Data[0].Id);

    if(match.ExactMatches.Members.Count > 0)
    {

    }

The MailChimpManager.Subscribe method has an optional updateExisting parameter, which will cause it to update a member that already exists, instead of throwing an exception:

EmailParameter results = mc.Subscribe("**LIST-ID**", subscriber, updateExisting: true);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top