/// <summary> /// Gets the template code with the specified /// parameters formatted into place, and with a /// specified outside wrapper. /// </summary> /// <param name="name">The name of the template.</param> /// <param name="wrapper">The name of template used to wrap the email (for style etc.).</param> /// <param name="args">A list of arguments to place into the template.</param> public static string Get(string name, string wrapper, params object[] args) { Mandrill.MandrillApi api = new Mandrill.MandrillApi(CONFIG.ApiKey); List<Mandrill.Models.TemplateInfo> templates = api.ListTemplates(); string wrapHTML = string.Empty; string fullHTML = string.Empty; // Get wrapper HTML. foreach (Mandrill.Models.TemplateInfo template in templates) { if (template.name == wrapper) { wrapHTML = template.code; } } // Get the template code from the list of templates. foreach (Mandrill.Models.TemplateInfo template in templates) { if (template.name == name) { fullHTML = String.Format(template.code, args); } } return String.Format(wrapHTML, fullHTML); }
private static async Task <string> SendEmail(EmailMessage emailMessage, EmailAddress emailAddress = null, Dictionary <string, string> dictionary = null, string emailTemplate = "") { // Sensitive information, needs to be properly encoded before deployment. Mandrill.MandrillApi api = new Mandrill.MandrillApi("rtHftXQYhGroTsPBzWBnbQ"); List <EmailResult> results = new List <EmailResult>(); if (String.IsNullOrEmpty(emailTemplate)) { Mandrill.Requests.Messages.SendMessageRequest request = new Mandrill.Requests.Messages.SendMessageRequest(emailMessage); results = await api.SendMessage(request); } else { if (dictionary != null) { foreach (var item in dictionary) { if (emailAddress != null) { emailMessage.AddRecipientVariable(emailAddress.Email, item.Key, item.Value); } } } Mandrill.Requests.Messages.SendMessageTemplateRequest request = new Mandrill.Requests.Messages.SendMessageTemplateRequest(emailMessage, emailTemplate); results = await api.SendMessageTemplate(request); } return(results.FirstOrDefault().Status.ToString()); }
/// <summary> /// Gets the template code with no parameters /// formatted into place. /// </summary> /// <param name="name">The name of the template.</param> public static string Get(string name) { Mandrill.MandrillApi api = new Mandrill.MandrillApi(CONFIG.ApiKey); List<Mandrill.Models.TemplateInfo> templates = api.ListTemplates(); foreach (Mandrill.Models.TemplateInfo template in templates) { if (template.name == name) { return template.code; } } return null; }
public async Task<ActionResult> MailTest() { var mandrill = new Mandrill.MandrillApi(this.mandrillApiKey); var email = new EmailMessage { FromEmail = this.emailSendingAddress, To = new List<EmailAddress> { new EmailAddress(this.errorEmailRecipient) }, Subject = "VCard import finished", Text = "sdfsdf", }; await mandrill.SendMessage(new SendMessageRequest(email)); return new HttpStatusCodeResult(200); }
/// <summary> /// Gets the template code with the specified /// parameters formatted into place. /// </summary> /// <param name="name">The name of the template.</param> /// <param name="args">A list of arguments to place into the template.</param> public static string Get(string name, params object[] args) { Mandrill.MandrillApi api = new Mandrill.MandrillApi(CONFIG.ApiKey); List<Mandrill.Models.TemplateInfo> templates = api.ListTemplates(); // Get the template code from the list of templates. foreach (Mandrill.Models.TemplateInfo template in templates) { if (template.name == name) { return String.Format(template.code, args); } } return null; }
public async Task <ActionResult> MailTest() { var mandrill = new Mandrill.MandrillApi(this.mandrillApiKey); var email = new EmailMessage { FromEmail = this.emailSendingAddress, To = new List <EmailAddress> { new EmailAddress(this.errorEmailRecipient) }, Subject = "VCard import finished", Text = "sdfsdf", }; await mandrill.SendMessage(new SendMessageRequest(email)); return(new HttpStatusCodeResult(200)); }
public static string SendMailByMandrill(string Host, int port, string from, string passsword, string to, string subject, string body, string SenderName) { string sendMailByMandrill = string.Empty; #region Mailsender try { Mandrill.EmailMessage message = new Mandrill.EmailMessage(); message.from_email = from; message.from_name = SenderName; message.html = body; message.subject = subject; message.to = new List<Mandrill.EmailAddress>() { new Mandrill.EmailAddress(to) }; Mandrill.MandrillApi mandrillApi = new Mandrill.MandrillApi(passsword, false); var results = mandrillApi.SendMessage(message); foreach (var result in results) { if (result.Status == Mandrill.EmailResultStatus.Invalid || result.Status == Mandrill.EmailResultStatus.Rejected) { sendMailByMandrill = "Invalid"; } else { sendMailByMandrill = "Success"; } } #endregion } catch (Exception ex) { Console.WriteLine(ex.Message); //logger.Error(ex.Message); } return sendMailByMandrill; }
public static string SendMailByMandrill(string Host, int port, string from, string passsword, string to, string subject, string body, string SenderName) { string sendMailByMandrill = string.Empty; #region Mailsender try { Mandrill.EmailMessage message = new Mandrill.EmailMessage(); message.from_email = "*****@*****.**"; message.from_name = SenderName; message.html = body; message.subject = subject; message.to = new List <Mandrill.EmailAddress>() { new Mandrill.EmailAddress(to) }; Mandrill.MandrillApi mandrillApi = new Mandrill.MandrillApi(passsword, false); var results = mandrillApi.SendMessage(message); foreach (var result in results) { if (result.Status == Mandrill.EmailResultStatus.Invalid || result.Status == Mandrill.EmailResultStatus.Rejected) { sendMailByMandrill = "Invalid"; } else { sendMailByMandrill = "Success"; } } #endregion } catch (Exception ex) { Console.WriteLine(ex.Message); //logger.Error(ex.Message); } return(sendMailByMandrill); }
protected async Task SendMandrillEmail(List <string> toAddress, string subject, string templateName, Dictionary <string, string> globalMergeVariables = null) { var email = new Mandrill.Models.EmailMessage(); email.To = toAddress.Select(s => new Mandrill.Models.EmailAddress(s)); email.Subject = subject; if (globalMergeVariables != null) { email.Merge = true; email.MergeLanguage = "mailchimp"; foreach (var kvp in globalMergeVariables) { email.AddGlobalVariable(kvp.Key, kvp.Value); } } var request = new Mandrill.Requests.Messages.SendMessageTemplateRequest(email, templateName); var api = new Mandrill.MandrillApi(MandrillApiKey); var result = await api.SendMessageTemplate(request); }
private void SendTemplate(object emailData, string template, string sendToAddress) { var api = new Mandrill.MandrillApi(AccountKey, true); var toEmail = new Mandrill.EmailAddress(sendToAddress); var message = new Mandrill.EmailMessage() { to = new Mandrill.EmailAddress[] { toEmail }, track_opens = true, track_clicks = true, merge = true, }; //Add all order confirmation email data properties to email template var data = emailData.ToDictionary <string>(); foreach (var d in data) { message.AddGlobalVariable(d.Key, d.Value); } api.SendMessageAsync(message, template, Enumerable.Empty <Mandrill.TemplateContent>()); }
public string SendMailByMandrill(string Host, int port, string from, string passsword, string to, string bcc, string cc, string subject, string body, string sendgridUserName, string sendgridPassword) { string sendMailByMandrill = string.Empty; try { Mandrill.EmailMessage message = new Mandrill.EmailMessage(); //message.from_email = from; //message.from_name = from;//"AlexPieter"; message.from_email = from; message.from_name = "Socioboard Support"; message.html = body; message.subject = subject; message.to = new List<Mandrill.EmailAddress>() { new Mandrill.EmailAddress(to) }; Mandrill.MandrillApi mandrillApi = new Mandrill.MandrillApi(sendgridPassword, false); var results = mandrillApi.SendMessage(message); foreach (var result in results) { if (result.Status != Mandrill.EmailResultStatus.Sent) { logger.Error(result.Email+" "+result.RejectReason); } // LogManager.Current.LogError(result.Email, "", "", "", null, string.Format("Email failed to send: {0}", result.RejectReason)); } sendMailByMandrill = "Success"; } catch (Exception ex) { Console.WriteLine(ex.Message); logger.Error(ex.Message); sendMailByMandrill=ex.Message; } return sendMailByMandrill; }
public async Task <ActionResult> MailWebhook() { // Initialize a mandrill client for sending emails var mandrill = new Mandrill.MandrillApi(this.mandrillApiKey); string sender = null; try { // Get the JSON payload string validJson = HttpContext.Request.Form["mandrill_events"].Replace("mandrill_events=", string.Empty); if (string.IsNullOrWhiteSpace(validJson)) { return(new HttpStatusCodeResult(400)); } var webhookEvents = JsonConvert.DeserializeObject <List <WebHookEvent> >(validJson); if (webhookEvents == null) { return(new HttpStatusCodeResult(400)); } var results = new List <string>(); foreach (var webhookEvent in webhookEvents) { sender = webhookEvent.Msg.FromEmail; var attachments = webhookEvent.Msg.Attachments; // Verify the security by checking authorized senders if (webhookEvent.Msg.Spf.Result != "pass" && webhookEvent.Msg.Dkim.Valid != true) { throw new Exception("Sender not authorized: SPF / DKIM Check failed."); } // Create an instance of the CRM service that acts in the name of the sender of the email using (var service = new CrmVcardUpdateService(sender)) { // Loop through the attachments foreach (var attachment in attachments) { // Get the VCards var filename = this.DecodeBase64Names(attachment.Key); if ((attachment.Value.Type != "text/vcard" && attachment.Value.Type != "text/x-vcard") || !filename.EndsWith(".vcf")) { results.Add(string.Format("{0}: not imported (mime-type: {1})", filename, attachment.Value.Type)); continue; } var bytes = attachment.Value.Base64 ? Convert.FromBase64String(attachment.Value.Content) : Encoding.UTF8.GetBytes(attachment.Value.Content); var memoryStream = new MemoryStream(bytes); var vcardReader = new vCardStandardReader(); using (var streamreader = new StreamReader(memoryStream)) { var vcard = vcardReader.Read(streamreader); // Import the VCards into the CRM var result = service.UpdateContactWithVcard(vcard, filename); results.Add(result); } } } } // Sends a mail with the results var email = new EmailMessage { FromEmail = this.emailSendingAddress, To = new List <EmailAddress> { new EmailAddress(sender ?? this.errorEmailRecipient) }, Subject = "VCard import finished", Text = "Results:\t\n\t\n" + string.Join("\t\n", results), }; await mandrill.SendMessage(new SendMessageRequest(email)); return(this.View((object)validJson)); } catch (Exception ex) { // Send a mail with the error var email = new EmailMessage { FromEmail = this.emailSendingAddress, To = new List <EmailAddress> { new EmailAddress(sender ?? this.errorEmailRecipient) }, Subject = "Error in VCard Import", Text = JsonConvert.SerializeObject(ex, Formatting.Indented) }; mandrill.SendMessage(new SendMessageRequest(email)); throw; } }
/// <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; } }
public async Task<ActionResult> MailWebhook() { // Initialize a mandrill client for sending emails var mandrill = new Mandrill.MandrillApi(this.mandrillApiKey); string sender = null; try { // Get the JSON payload string validJson = HttpContext.Request.Form["mandrill_events"].Replace("mandrill_events=", string.Empty); if (string.IsNullOrWhiteSpace(validJson)) { return new HttpStatusCodeResult(400); } var webhookEvents = JsonConvert.DeserializeObject<List<WebHookEvent>>(validJson); if (webhookEvents == null) { return new HttpStatusCodeResult(400); } var results = new List<string>(); foreach (var webhookEvent in webhookEvents) { sender = webhookEvent.Msg.FromEmail; var attachments = webhookEvent.Msg.Attachments; // Verify the security by checking authorized senders if (webhookEvent.Msg.Spf.Result != "pass" && webhookEvent.Msg.Dkim.Valid != true) { throw new Exception("Sender not authorized: SPF / DKIM Check failed."); } // Create an instance of the CRM service that acts in the name of the sender of the email using (var service = new CrmVcardUpdateService(sender)) { // Loop through the attachments foreach (var attachment in attachments) { // Get the VCards var filename = this.DecodeBase64Names(attachment.Key); if ((attachment.Value.Type != "text/vcard" && attachment.Value.Type != "text/x-vcard") || !filename.EndsWith(".vcf")) { results.Add(string.Format("{0}: not imported (mime-type: {1})", filename, attachment.Value.Type)); continue; } var bytes = attachment.Value.Base64 ? Convert.FromBase64String(attachment.Value.Content) : Encoding.UTF8.GetBytes(attachment.Value.Content); var memoryStream = new MemoryStream(bytes); var vcardReader = new vCardStandardReader(); using (var streamreader = new StreamReader(memoryStream)) { var vcard = vcardReader.Read(streamreader); // Import the VCards into the CRM var result = service.UpdateContactWithVcard(vcard, filename); results.Add(result); } } } } // Sends a mail with the results var email = new EmailMessage { FromEmail = this.emailSendingAddress, To = new List<EmailAddress> { new EmailAddress(sender ?? this.errorEmailRecipient) }, Subject = "VCard import finished", Text = "Results:\t\n\t\n" + string.Join("\t\n", results), }; await mandrill.SendMessage(new SendMessageRequest(email)); return this.View((object)validJson); } catch (Exception ex) { // Send a mail with the error var email = new EmailMessage { FromEmail = this.emailSendingAddress, To = new List<EmailAddress> { new EmailAddress(sender ?? this.errorEmailRecipient) }, Subject = "Error in VCard Import", Text = JsonConvert.SerializeObject(ex, Formatting.Indented) }; mandrill.SendMessage(new SendMessageRequest(email)); throw; } }