Пример #1
0
        public async Task Should_Not_Require_Template_Content()
        {
            // Setup
            string apiKey          = ConfigurationManager.AppSettings["APIKey"];
            string toEmail         = ConfigurationManager.AppSettings["ValidToEmail"];
            string fromEmail       = ConfigurationManager.AppSettings["FromEMail"];
            string templateExample = ConfigurationManager.AppSettings["TemplateExample"];

            // Exercise
            var api = new MandrillApi(apiKey);

            List <EmailResult> result = await api.SendMessageTemplate(new SendMessageTemplateRequest
                                                                      (new EmailMessage
            {
                To =
                    new List <EmailAddress> {
                    new EmailAddress {
                        Email = toEmail, Name = ""
                    }
                },
                FromEmail = fromEmail,
                Subject = "Mandrill Integration Test",
            },
                                                                       templateExample));

            // Verify
            Assert.AreEqual(1, result.Count);
            Assert.AreEqual(toEmail, result.First().Email);
            Assert.AreEqual(EmailResultStatus.Sent, result.First().Status);
        }
    public async Task Should_Send_Email_Message_With_Template_With_Send_At()
    {
      if (!IsPaidAccount)
        Assert.Ignore("Not a paid account");

      // Setup
      string apiKey = ConfigurationManager.AppSettings["APIKey"];
      string toEmail = ConfigurationManager.AppSettings["ValidToEmail"];
      string fromEmail = ConfigurationManager.AppSettings["FromEMail"];
      string templateExample = ConfigurationManager.AppSettings["TemplateExample"];

      // Exercise
      var api = new MandrillApi(apiKey);

      List<EmailResult> result = await api.SendMessageTemplate(new SendMessageTemplateRequest(new EmailMessage
        {
          To =
            new List<EmailAddress> {new EmailAddress {Email = toEmail, Name = ""}},
          FromEmail = fromEmail,
          Subject = "Mandrill Scheduled Integration Test",
        },
        templateExample,
        new List<TemplateContent>
        {
          new TemplateContent {Name = "model1", Content = "Content1"},
          new TemplateContent {Name = "model2", Content = "Content2"}
        }){
        SendAt = DateTime.Now.AddMinutes(5)
      });

      // Verify
      Assert.AreEqual(1, result.Count);
      Assert.AreEqual(toEmail, result.First().Email);
      Assert.AreEqual(EmailResultStatus.Sent, result.First().Status);
    }
Пример #3
0
        /// <summary>
        /// Sending Email through Mandril API (Using Mandril Templates)
        /// </summary>
        /// <param name="fromEmail"></param>
        /// <param name="fromName"></param>
        /// <param name="toMails"></param>
        /// <param name="subject"></param>
        /// <param name="templateName"></param>
        /// <param name="MergeVariables"></param>
        /// <returns></returns>
        public static List <EmailResult> SendMail(string fromEmail, string fromName, List <string> toMails, string subject, string templateName, Dictionary <string, dynamic> MergeVariables)
        {
            try
            {
                var api = new MandrillApi(Config.Key);

                var email = new EmailMessage()
                {
                    FromEmail = fromEmail,
                    FromName  = fromName,
                    Subject   = subject,
                };
                email.Merge         = true;
                email.MergeLanguage = "mailchimp";
                //email.Headers.Add("Reply-To", "*****@*****.**");

                foreach (var item in MergeVariables)
                {
                    email.AddGlobalVariable(item.Key, item.Value);
                }


                var to = toMails.Select(mailTo => new EmailAddress(mailTo)).ToList();
                email.To = to;

                // Send email
                var smReq  = new SendMessageTemplateRequest(email, templateName);
                var output = api.SendMessageTemplate(smReq).Result;
                return(output);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Пример #4
0
        public async Task SendEmailTemplateAsync(string emailAddress, string template, Dictionary <string, string> templateParameters)
        {
            var emailMessage = new EmailMessage();

            emailMessage.To = new[] { new EmailAddress(emailAddress) };

            foreach (var item in templateParameters)
            {
                emailMessage.AddGlobalVariable(item.Key, item.Value);
            }

            var messageTemplateRequest      = new SendMessageTemplateRequest(emailMessage, template);
            List <EmailResult> emailResults = await mandrillApi_.SendMessageTemplate(messageTemplateRequest);

            foreach (var item in emailResults)
            {
                if (item.Status == EmailResultStatus.Rejected)
                {
                    throw new ServerErrorException(
                              $"Sending email template {template} to {item.Email} with parameters: {string.Join(",", templateParameters)} failed with reason: {item.RejectReason}");
                }

                if (item.Status == EmailResultStatus.Invalid)
                {
                    throw new InvalidArgumentException(
                              $"Sending email template {template} to {item.Email} with parameters: {string.Join(",", templateParameters)} is invalid");
                }
            }
        }
Пример #5
0
        public async Task <IActionResult> SendKeysViaEmail(string listId)
        {
            var existingKeys = await new AccessKey().RetrieveAllAsync();

            var mailChimp = new MailChimpManager("3f4978dbd632a4471650eeeebe681ce3-us12");
            var mandrill  = new MandrillApi("DQCr_ZMk5Ga5slGv9uWGPQ");

            List mailChimpList;

            try { mailChimpList = await mailChimp.Lists.GetAsync(listId); }
            catch (MailChimpException) { return(ErrorView($"MailChimp list with ID {listId} doesn't exist.")); }

            var resultsList = new List <string> {
                $"Scanned MailChimp list {mailChimpList.Name} and sent an access key for each member:"
            };

            var listMembers = await mailChimp.Members.GetAllAsync(listId);

            foreach (var listMember in listMembers)
            {
                var accessKey = await new AccessKey().AddAsync(existingKeys, listMember.EmailAddress);
                existingKeys.Add(accessKey);

                var emailMessage = new EmailMessage()
                {
                    To = new List <EmailAddress> {
                        new EmailAddress(listMember.EmailAddress)
                    }
                };
                var accessKeyContent = new TemplateContent()
                {
                    Name = "acces-key", Content = accessKey.Id
                };
                var sendRequest = new SendMessageTemplateRequest(emailMessage, "Access Key", new List <TemplateContent> {
                    accessKeyContent
                });

                var sendResults = await mandrill.SendMessageTemplate(sendRequest);

                var result = sendResults.First();
                resultsList.Add($" - Sent key {accessKey.Id} to {result.Email} with status {result.Status}. "
                                + (result.Status != EmailResultStatus.Sent ? $"Reject reason: {result.RejectReason}" : string.Empty));
            }

            return(View("SendAccessKeysResult", resultsList));
        }
Пример #6
0
        public async Task Should_Send_Email_Message_With_Template_With_Send_At()
        {
            if (!IsPaidAccount)
            {
                Assert.Ignore("Not a paid account");
            }

            // Setup
            string apiKey          = ConfigurationManager.AppSettings["APIKey"];
            string toEmail         = ConfigurationManager.AppSettings["ValidToEmail"];
            string fromEmail       = ConfigurationManager.AppSettings["FromEMail"];
            string templateExample = ConfigurationManager.AppSettings["TemplateExample"];

            // Exercise
            var api = new MandrillApi(apiKey);

            List <EmailResult> result = await api.SendMessageTemplate(new SendMessageTemplateRequest(new EmailMessage
            {
                To =
                    new List <EmailAddress> {
                    new EmailAddress {
                        Email = toEmail, Name = ""
                    }
                },
                FromEmail = fromEmail,
                Subject = "Mandrill Scheduled Integration Test",
            },
                                                                                                     templateExample,
                                                                                                     new List <TemplateContent>
            {
                new TemplateContent {
                    Name = "model1", Content = "Content1"
                },
                new TemplateContent {
                    Name = "model2", Content = "Content2"
                }
            }){
                SendAt = DateTime.Now.AddMinutes(5)
            });

            // Verify
            Assert.AreEqual(1, result.Count);
            Assert.AreEqual(toEmail, result.First().Email);
            Assert.AreEqual(EmailResultStatus.Sent, result.First().Status);
        }
Пример #7
0
        private static void SendEmail(MandrillApi api, SendInfo info, string subj, string content)
        {
            LogManager.GetLogger(typeof(EmailWorker)).Info("Sending email to:" + info.UserEmail);

            var _task = api.SendMessageTemplate(
                new SendMessageTemplateRequest(new EmailMessage
            {
                To          = new[] { new EmailAddress(info.UserEmail) },
                FromEmail   = "*****@*****.**",
                Subject     = subj,
                TrackClicks = false,
                TrackOpens  = false
            },
                                               MANDRILL_TEMPLATE,
                                               new[]
            {
                new TemplateContent
                {
                    Name    = "main",
                    Content = content
                }
            }));
        }
        public Task SendWelcomeEmail(string firstName, string email)
        {
            const string subject = "Bienvenue dans eTransfert";

            var emailMessage = new EmailMessage
            {
                FromEmail = EmailFromAddress,
                FromName  = EmailFromName,
                Subject   = subject,
                To        = new List <Mandrill.Models.EmailAddress> {
                    new EmailAddress(email)
                },
                Merge = true,
            };

            emailMessage.AddGlobalVariable("subject", subject);
            emailMessage.AddGlobalVariable("first_name", firstName);

            var task = _mandrill.SendMessageTemplate(new SendMessageTemplateRequest(emailMessage, "welcome_chimp", null));

            task.Wait();

            return(task);
        }
    public async Task Should_Not_Require_Template_Content()
    {
      // Setup
      string apiKey = ConfigurationManager.AppSettings["APIKey"];
      string toEmail = ConfigurationManager.AppSettings["ValidToEmail"];
      string fromEmail = ConfigurationManager.AppSettings["FromEMail"];
      string templateExample = ConfigurationManager.AppSettings["TemplateExample"];

      // Exercise
      var api = new MandrillApi(apiKey);

      List<EmailResult> result = await api.SendMessageTemplate(new SendMessageTemplateRequest
      (new EmailMessage
        {
          To =
            new List<EmailAddress> {new EmailAddress {Email = toEmail, Name = ""}},
          FromEmail = fromEmail,
          Subject = "Mandrill Integration Test",
        },
        templateExample));

      // Verify
      Assert.AreEqual(1, result.Count);
      Assert.AreEqual(toEmail, result.First().Email);
      Assert.AreEqual(EmailResultStatus.Sent, result.First().Status);
    }