Beispiel #1
0
        /// <summary>
        /// Send an email to the referee
        /// </summary>
        /// <param name="timer"></param>
        /// <returns></returns>
        public async Task <bool> SendRefereeEmailAsync(Timer timer)
        {
            //null check
            if (timer == null)
            {
                throw new ArgumentNullException(nameof(timer));
            }

            //check all information exists
            if (timer.RefereeEmail == null)
            {
                throw new ArgumentNullException(nameof(timer.RefereeEmail));
            }

            //create the confirmation code
            var confirmationCode = await _refereeCodesService.CreateRefereeCodeAsync(timer.Id);

            //get the template
            var subject      = _config["Email:Templates:Referee:Subject"];
            var sendTemplate = _config["Email:Templates:Referee:Body"];
            var fromAddress  = _config["Email:Sender:Address"];
            var fromName     = _config["Email:Sender:Name"];

            //links template and variables
            var linkTemplate      = _config["Email:Templates:Referee:LinkConfirmationTemplate"];
            var unsubLinkTemplate = _config["Email:Templates:Referee:LinkUnsubscribe"];

            var linkVars = new Dictionary <string, string>()
            {
                ["TimerId"]          = timer.Id.ToString(),
                ["ConfirmationCode"] = confirmationCode,
                ["OnTimeValue"]      = "true",
                ["RefereeEmail"]     = timer.RefereeEmail.Address
            };
            var ontimeLink      = EmailTemplateEngine.ReplaceTokens(linkTemplate, linkVars);
            var unsubscribeLink = EmailTemplateEngine.ReplaceTokens(unsubLinkTemplate, linkVars);

            //create late link
            linkVars["OnTimeValue"] = "false";
            var lateLink = EmailTemplateEngine.ReplaceTokens(linkTemplate, linkVars);


            //fill out the variables for email
            var vars = new Dictionary <string, string>
            {
                ["LinkOnTime"]      = ontimeLink,
                ["LinkLate"]        = lateLink,
                ["LinkHomepage"]    = _config["Email:Templates:Referee:LinkHomepage"],
                ["LinkUnsubscribe"] = unsubscribeLink,
                ["CustomerName"]    = timer.CustomerName
            };

            var email = EmailTemplateEngine.GenerateMessage(new MailAddress(fromAddress, fromName), timer.RefereeEmail, subject, sendTemplate,
                                                            vars);

            return(await _emailService.SendEmail(email));
        }
Beispiel #2
0
        public static void Execute(String templateLocation)
        {
            IEmailTemplateContentReader templateReader = new FileSystemEmailTemplateContentReader(templateLocation);
            IEmailTemplateEngine        templateEngine = new EmailTemplateEngine(templateReader);

            IEmailSender sender = new EmailSender
            {
                // replace with RealSmtpClient() to test with a real mail server
                CreateClientFactory = () => new SmtpClientWrapper(RealSmtpClient())
            };

            var subsystem = new EmailSubsystem(templateEngine, sender);

            subsystem.SendMail(
                "NewTaskEmailCommand",
                new NewTaskEmailCommand
            {
                To   = "*****@*****.**",
                From = "*****@*****.**"
            });
        }