コード例 #1
0
        /// <summary>
        /// Confirmation email sent to the sender...
        /// </summary>
        /// <param name="toSenderEmail"></param>
        /// <returns></returns>
        public async Task SendGeneralConfirmationEmailAsync(EmailToSendModel toSenderEmail)
        {
            // Sender template (No body)
            var templateText = await ReturnTemplateFromFileAsync("SendMail.Templates.GeneralWebsiteTemplateToSender.html");

            // Confirmation text for sender
            var title    = $"Hey {toSenderEmail.Sender.Name},<br/>Thanks for getting in touch!";
            var subtitle = "I'll try to reply as soon as I can.";

            // New subject title for confirmation email
            toSenderEmail.Email.Subject = "Thanks for getting in touch, " + toSenderEmail.Sender.Name + "!";

            // Replace variables in template with response text and set to Html body
            toSenderEmail.Email.MessageHtml = ReplaceVariablesInTemplate(templateText, title, subtitle, toSenderEmail.Email.DateSent, string.Empty);

            // Set text message in body with confirmation text
            toSenderEmail.Email.MessageText = $"Hey {toSenderEmail.Sender.Name},\n"
                                              + $"Thanks for getting in touch!\n"
                                              + subtitle;

            // Set to true to send Html content
            toSenderEmail.IsHtml = true;

            // Swap the sender and receiver with deep clone
            var tempSender = EmailSenderHelper.DeepCopy(toSenderEmail.Sender);

            toSenderEmail.Sender   = toSenderEmail.Receiver;
            toSenderEmail.Receiver = tempSender;

            // Send confirmation email to Sender
            var response = await _emailSender.SendEmailAsync(toSenderEmail);
        }
コード例 #2
0
        public async Task <EmailSentResponse> SendGeneralEmailAsync(EmailToSendModel email, string title, string subtitle, string body)
        {
            var templateText = await ReturnTemplateFromFileAsync("SendMail.Templates.GeneralWebsiteTemplate.html");

            // Deep clone template and email
            var toReceiverTemplate = string.Copy(templateText);
            var toReceiverEmail    = EmailSenderHelper.DeepCopy(email);

            // Format date to FullDateTime
            var formattedDate = string.Format("{0:F}", email.Email.DateSent);

            // Replace variables in template with email text and set to message Html body
            toReceiverEmail.Email.MessageHtml = ReplaceVariablesInTemplate(toReceiverTemplate, title, subtitle, formattedDate, body);

            // Set to true to send Html content
            toReceiverEmail.IsHtml = true;

            // Send email to Receiver
            var response = await _emailSender.SendEmailAsync(toReceiverEmail);

            // If email was sent successfully...
            if (response.Successful)
            {
                // Make another clone of the original email
                var toSenderEmail = EmailSenderHelper.DeepCopy(email);

                // Send confirmation email to sender
                await SendGeneralConfirmationEmailAsync(toSenderEmail);
            }

            // Return response from email to receiver
            return(response);
        }
コード例 #3
0
        public void ConstructEmailToSend_ShouldReturnConstructedEmail()
        {
            var emailSender = new EmailSender();

            // Act
            var emailToTest = new EmailToSendModel()
            {
                Sender = new EmailAddressModel()
                {
                    Name  = "Jimbo",
                    Email = "*****@*****.**"
                },
                Receiver = new EmailAddressModel()
                {
                    Name  = "Mike",
                    Email = "*****@*****.**"
                },
                Email = new EmailMessageModel()
                {
                    Subject     = "Test email Subject",
                    MessageText = "Some text",
                    MessageHtml = "Some Html",
                    DateSent    = "Today's Date"
                }
            };

            var actualEmailToSend = emailSender.ConstructEmailToSend(emailToTest);

            // Assert
            Assert.True(actualEmailToSend != null);
            Assert.Equal(new MailboxAddress(emailToTest.Sender.Name, emailToTest.Sender.Email), actualEmailToSend.From[0]);
            Assert.Equal(new MailboxAddress(emailToTest.Receiver.Name, emailToTest.Receiver.Email), actualEmailToSend.To[0]);
            Assert.Equal(emailToTest.Email.Subject, actualEmailToSend.Subject);
        }
コード例 #4
0
        public async Task <EmailSentResponse> SendEmailAsync(EmailToSendModel email)
        {
            // Create a MIME message object for the email
            var message = ConstructEmailToSend(email);

            // Uses the disposable instance of the Smtp client
            using (var client = new SmtpClient())
            {
                try
                {
                    client.ServerCertificateValidationCallback = (s, c, h, e) => true;
                    // Sets up correct smtp server and port
                    await client.ConnectAsync(
                        Environment.GetEnvironmentVariable("SmtpServer"),
                        int.Parse(Environment.GetEnvironmentVariable("SmtpPort")),
                        true
                        ).ConfigureAwait(false);

                    // Auth step to connect with the mailing server
                    await client.AuthenticateAsync(
                        Environment.GetEnvironmentVariable("EMAIL_ADDRESS"),
                        Environment.GetEnvironmentVariable("EMAIL_PASSWORD")
                        ).ConfigureAwait(false);

                    // Send message
                    await client.SendAsync(message).ConfigureAwait(false);

                    await client.DisconnectAsync(true).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    // If email sending failed...
                    return(new EmailSentResponse()
                    {
                        ErrorMessage = new List <string>()
                        {
                            ex.Message
                        }
                    });
                }
            }

            // If email was sent successfully...
            return(new EmailSentResponse()
            {
                ErrorMessage = null,
                Email = email
            });
        }
コード例 #5
0
        public async Task <EmailSentResponse> SendEmailAsync(EmailToSendModel email)
        {
            // Create a MIME message object for the email
            var message = ConstructEmailToSend(email);

            using (var client = new SmtpClient())
            {
                try
                {
                    client.ServerCertificateValidationCallback = (s, c, h, e) => true;
                    await client.ConnectAsync(
                        Startup.Configuration["EmailConfiguration:SmtpServer"],
                        int.Parse(Startup.Configuration["EmailConfiguration:SmtpPort"]),
                        true
                        ).ConfigureAwait(false);

                    // Note: only needed if the SMTP server requires authentication
                    await client.AuthenticateAsync(
                        Environment.GetEnvironmentVariable("EMAIL_ADDRESS"),
                        Environment.GetEnvironmentVariable("EMAIL_PASSWORD")
                        ).ConfigureAwait(false);

                    // Send message
                    await client.SendAsync(message).ConfigureAwait(false);

                    await client.DisconnectAsync(true).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    // If email sending failed...
                    return(new EmailSentResponse()
                    {
                        ErrorMessage = new List <string>()
                        {
                            ex.Message
                        }
                    });
                }
            }

            // If email was sent successfully...
            return(new EmailSentResponse()
            {
                ErrorMessage = null,
                Email = email
            });
        }
コード例 #6
0
        public MimeMessage ConstructEmailToSend(EmailToSendModel email)
        {
            var message = new MimeMessage();

            //From
            message.From.Add(new MailboxAddress(email.Sender.Name, email.Sender.Email));

            // To
            message.To.Add(new MailboxAddress(email.Receiver.Name, email.Receiver.Email));

            // Subject
            message.Subject = email.Email.Subject;

            // Body (text/Html)
            message.Body = new BodyBuilder()
            {
                TextBody = $@"{email.Email.MessageText} 
                            Email was sent at: 
                            {email.Email.DateSent}",
                HtmlBody = email.IsHtml ? email.Email.MessageHtml : null
            }.ToMessageBody();

            return(message);
        }