22

I want to send email through MailChimp. How to do this in .Net?

Does any one have sample code?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Waheed
  • 10,086
  • 20
  • 53
  • 66

2 Answers2

16

The example below will send a opt-in email:

First install the NuGet package: Install-Package mcapi.net

    static void Main(string[] args)
    {
        const string apiKey = "6ea5e2e61844608937376d514-us2";   // Replace it before
        const string listId = "y657cb2495";                      // Replace it before

        var options = new List.SubscribeOptions();
        options.DoubleOptIn = true;
        options.EmailType = List.EmailType.Html;
        options.SendWelcome = false;

        var mergeText = new List.Merges("email@provider.com", List.EmailType.Text)
                    {
                        {"FNAME", "John"},
                        {"LNAME", "Smith"}
                    };
        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);
        else
            Console.WriteLine("Success");

        Console.ReadKey();
    }
Fernando Vezzali
  • 2,219
  • 1
  • 29
  • 32
  • 1
    This should be the correct answer. By far the fastest and easiest way. – Serj Sagan Jul 23 '13 at 03:25
  • 1
    fiddler indicates this is sending to v1.3 of the api and with 3.0 now live the prior versions will not be supported after 2016. oh and it didnt appear to work anyway – wal Oct 19 '16 at 12:17
  • see answer by @Pranav Singh way below – wal Oct 19 '16 at 12:26
3

Do Check out https://github.com/danesparza/MailChimp.NET by Dan Esparza You can install the package by using Package Manager Console

Install-Package MailChimp.NET

Code example

MailChimpManager mc = new MailChimpManager("YourApiKeyHere-us2");
ListResult lists = mc.GetLists();

For email sending and stats, Mailchimp offers Mandrill by Shawn Mclean https://github.com/shawnmclean/Mandrill-dotnet

You can install Mandrill using

Install-Package Mandrill

Code example

MandrillApi api = new MandrillApi("xxxxx-xxxx-xxxx-xxxx");
UserInfo info = await api.UserInfo();
Muhammad Waqas Iqbal
  • 3,244
  • 1
  • 20
  • 9
  • Dan Esparza now has a note on that project page to check out the v3 one by Brandon Seydel, (noted in Pranav's answer) since that's only for the v2.0 API which has been deprecated. – ahwm May 26 '17 at 16:31