protected async void Button1_Click(object sender, EventArgs e) { MandrillApi api = new MandrillApi("Mandrill Api Key", true); IEnumerable<EmailAddress> addresses = new[] { new EmailAddress("*****@*****.**", "Name Of the Contact"), }; var message = new EmailMessage(); message.FromEmail = "*****@*****.**"; message.FromName = "Your name"; message.Html = "emailbody"; message.Text = "emailbodytext"; message.Subject = "subject"; message.To = addresses; SendMessageRequest req = new SendMessageRequest(message); var returnvalue = await api.SendMessage(req); }
static void AsynchronousAPICalls() { string key = ConfigurationManager.AppSettings["MandrillKey"]; string url = ConfigurationManager.AppSettings["MandrillUrl"]; string fromEmail = ConfigurationManager.AppSettings["MandrillFromEmail"]; string fromDisplayName = ConfigurationManager.AppSettings["MandrillFromEmailDisplay"]; string toEmail = ConfigurationManager.AppSettings["MandrillToEmail"]; string toDisplayName = ConfigurationManager.AppSettings["MandrillToEmailDisplay"]; var madrilTest = new Mandrill(key, url); Console.WriteLine("1 sending ping request"); madrilTest.PingAsync(new PingRequest(), PingHandler); Console.WriteLine("2 sending get info request"); madrilTest.GetInfoAsync(new GetInfoRequest(), InfoHandler); Console.WriteLine("3 sending get sender data request"); madrilTest.GetSenderDataAsync(new GetSenderDataRequest(), SenderDataHandler); Console.WriteLine("4 sending get templates request"); madrilTest.GetTemplatesAsync(new GetTemplatesRequest(), TemaplateListHandler); Console.WriteLine("5 sending a create email request"); var message = new EmailMessage { To = new List<EmailAddress> { new EmailAddress { Email = toEmail, Name = toDisplayName } }, FromEmail = fromEmail, FromName = fromDisplayName, Subject = "Mandril Test Email", Html = "<strong>Html email in the house!</strong>", Text = "plain text email on the job" }; madrilTest.SendEmailAsync(new SendEmailRequest { Message = message }, SendEmailHandler); }
public string SendTmplMessage(MandrillApi mAPI, Mandrill.Model.MandrillMessage message) { var result = mAPI.Messages.Send(message); return result.ToString(); }
static void SynchronousAPICalls() { string key = ConfigurationManager.AppSettings["MandrillKey"]; string url = ConfigurationManager.AppSettings["MandrillUrl"]; string fromEmail = ConfigurationManager.AppSettings["MandrillFromEmail"]; string fromDisplayName = ConfigurationManager.AppSettings["MandrillFromEmailDisplay"]; string toEmail = ConfigurationManager.AppSettings["MandrillToEmail"]; string toDisplayName = ConfigurationManager.AppSettings["MandrillToEmailDisplay"]; string sampleTemplateHtml = ConfigurationManager.AppSettings["MandrillSampleTemplateHtml"]; string sampleTemplateText = ConfigurationManager.AppSettings["MandrillSampleTemplateText"]; var madrilTest = new Mandrill(key, url); // 1 Ping Mandrill var ping = madrilTest.Ping(new PingRequest()); Console.WriteLine("Ping returns: " + ping); // 2 Get Info about the user associated with the current Mandril key var getInfo = madrilTest.GetInfo(new GetInfoRequest()); Console.WriteLine(getInfo.Username); // 3 Get Some statistics about recently sent emails var senderDataResponses = madrilTest.GetSenderData(new GetSenderDataRequest()); foreach (var sender in senderDataResponses) { Console.WriteLine("Sender:{0} Create Date:{1} Opens:{2}", sender.Address, sender.CreatedAt, sender.Opens); } // 4 Send a simple email message var message1 = new EmailMessage { To = new List<EmailAddress> { new EmailAddress { Email = toEmail, Name = toDisplayName } }, FromEmail = fromEmail, Subject = "Mandril Test Email", Html = "<strong>Html email in the house!</strong>", Text = "plain text email on the job" }; var sendResponses1 = madrilTest.SendEmail(new SendEmailRequest { Message = message1 }); foreach (var sendEmailResponse in sendResponses1) { Console.WriteLine("Email send results: " + sendEmailResponse.Status); } // 5 Create a new template var newTemplate = new PostTemplateRequest { TemplateName = "Dummy Template", FromEmail = fromEmail, FromName = fromDisplayName, Subject = "My fancy template", Code = GetTextFromFile(sampleTemplateHtml), Text = GetTextFromFile(sampleTemplateText), Publish = true }; var response = madrilTest.PostTemplate(newTemplate); Console.WriteLine(response.Slug); // 5 get a list of all templates var templates = madrilTest.GetTemplates(new GetTemplatesRequest()); foreach (var templateInfo in templates) { Console.WriteLine("Template Name: {0} Slug: {1}", templateInfo.TemplateName, templateInfo.Slug); } // 6 Send a templated email with merge variables and and mc:edit region for the footer var message2 = new EmailMessage { To = new List<EmailAddress> { new EmailAddress {Email = toEmail, Name = toDisplayName} }, FromEmail = fromEmail, FromName = fromDisplayName, Html = null, Text = null }; //string to = "*****@*****.**"; message2.AddGlobalVariable("customername", toDisplayName); message2.AddGlobalVariable("orderdate", DateTime.Now.Date.ToShortDateString()); message2.AddGlobalVariable("invoicedetails", "SMS Data fee $19.99"); message2.Merge = true; var templateContents = new List<TemplateContent> { new TemplateContent {Name = "footer", Content = "Contact us at [email protected]"} }; var request = new SendEmailWithTemplateRequest { Message = message2, TemplateContent = templateContents, TemplateName = "Dummy Template" }; var sendResponses = madrilTest.SendEmailWithTemplate(request); foreach (var sendEmailResponse in sendResponses) { Console.WriteLine("Templated email send results: " + sendEmailResponse.Status); } // 8 Update a template response = madrilTest.PutTemplate(new PutTemplateRequest { TemplateName = "Dummy Template", Code = "<strong>Updated!</strong>" }); Console.WriteLine(response.Code); // 9 Delete a template var results = madrilTest.DeleteTemplate(new DeleteTemplateRequest { TemplateName = "Dummy Template" }); Console.WriteLine(results.TemplateName + " was deleted"); }
/// <summary> /// Sends the constructed email to mandrill, /// adding required extra parameters. /// </summary> private static void SendToMandrill(Mandrill.EmailMessage email, List<Mandrill.EmailAddress> cc = null, List<Mandrill.attachment> attachments = null, string replyTo = null, List<string> tags = null, bool tracking = false, string plain = null) { Mandrill.MandrillApi api = new Mandrill.MandrillApi(CONFIG.ApiKey); // Loop through and add all recipients to the mandrill email. if (cc != null) { List<Mandrill.EmailAddress> recipients = (List<Mandrill.EmailAddress>) email.to.ToList(); foreach (Mandrill.EmailAddress address in cc) { recipients.Add(address); } email.to = recipients; } // Add tags if required, as well as project tag. if (tags == null) { tags = new List<string>(); }; tags.Add(CONFIG.ProjectTag); if (tags != null) { } email.tags = tags; // Add plain text if required. if (plain != null) { email.text = plain; } // Set reply-to address. if (replyTo != null) { email.AddHeader("Reply-To", replyTo); } // Add any attachments. if (attachments != null) { email.attachments = attachments; } // Set subaccount if required. if (CONFIG.SubAccount != null) { email.subaccount = CONFIG.SubAccount; } else { if (CONFIG.TrapApiKey == CONFIG.ApiKey && CONFIG.TrapAccount != null) { email.subaccount = CONFIG.TrapAccount; } } // Set tracking options. email.track_clicks = tracking; email.track_opens = tracking; try { api.SendMessage(email); } catch (Exception ex) { throw ex; } }