Pages

Wednesday, 4 June 2014

Hi Guys,

Haven't been blogged since a long time. So here i come up with sending Email with mandrill, a Transactional  Email from Mail Chimp.
First lemme explain about Transactional Emails.
Transactional Emails are those which are one time sending Email like we send a mail to the user after he registers or when he click on forgot password where we will send a mail to his Email.

Here we need to go to Mandrill web site and should be registered there. So that we can get an api key. So Regiter with the site and then you will be shown this screen



Click on Get api key and then it will take you to this where you need to add a key


Now you can save that key and use later on. 
So here are the few simple steps to set up things
1) Create a solution in visual studio  which ever version you are using.
2) Add a new project web or windows anything you want to. I chose Web
3) Right click on project select manage nuget packages and  search for MailChimp
4) Then add a web form so that we can send email on a button click or what ever
5) Write some code in button click

        protected void Unnamed_Click(object sender, EventArgs e)
        {
            SendEmail("emailSubject", "String emailBody");
        }

6) Now write the SendEmail method

        static void SendEmail( String emailSubject, String emailBody)
        {
            try
            {
                    //Api key that was generated after registering
                MandrillApi mandrillAPI = new MandrillApi("XXXXXXXXXXXXXX");
                 
                MailChimp.Types.Mandrill.Messages.Message message = new                                                                                                                                      MailChimp.Types.Mandrill.Messages.Message();

     
                message.FromEmail = "fromEmail";
                message.FromName = "Name";
                message.PreserveRecipients = false;  
                message.TrackOpens = true;          
                message.TrackClicks = true;          
                message.Merge = true;                

                Int32 recipientCount = 1;

                MailChimp.Types.Mandrill.Messages.Recipient[] recipientList = new                                                                                  MailChimp.Types.Mandrill.Messages.Recipient[recipientCount];  // Receipient List
                MailChimp.Types.Mandrill.Messages.MergeVars[] mergeVarList = new                                                                             MailChimp.Types.Mandrill.Messages.MergeVars[recipientCount];

                for (Int32 counter = 0; counter < 1; counter++)
                {
                    // Add new recipient email and name
                    recipientList[counter] = new MailChimp.Types.Mandrill.Messages.Recipient("toEmail", "Name"+ " " + "Last Name");

                    // Add receipient specific information
                    var mergeVars = new MailChimp.Types.Mandrill.NameContentList<string>();

                    mergeVarList[counter] = new MailChimp.Types.Mandrill.Messages.MergeVars("toEmail", mergeVars);
                }

                // Assign receipient list to message
                message.To = recipientList;

                // Assign receipient related merge vars list to message
                message.MergeVars = mergeVarList;

                // Email Subject
                message.Subject = emailSubject;

                // Email Body
                message.Html = emailBody;

                mandrillAPI.Send(message);
            }
            catch (MailChimp.Types.MCException ex)
            {
                throw;
            }
        }

Boom  you should get an email right away after clicking the button.

Happy Coding