From e0383f2a0fa4a9b6291235be13926d6e7428bca8 Mon Sep 17 00:00:00 2001 From: Jon M Date: Tue, 8 Mar 2022 14:20:26 +0000 Subject: [PATCH] Add support for verification codes --- Mailosaur.Test/EmailsTests.cs | 10 +++++++ Mailosaur.Test/Mailer.cs | 39 +++++++++++++------------ Mailosaur.Test/Resources/testEmail.html | 10 ++++++- Mailosaur.Test/Resources/testEmail.txt | 6 +++- Mailosaur/Models/Code.cs | 7 +++++ Mailosaur/Models/MessageContent.cs | 1 + 6 files changed, 52 insertions(+), 21 deletions(-) create mode 100644 Mailosaur/Models/Code.cs diff --git a/Mailosaur.Test/EmailsTests.cs b/Mailosaur.Test/EmailsTests.cs index 68ef7c2..e90182d 100644 --- a/Mailosaur.Test/EmailsTests.cs +++ b/Mailosaur.Test/EmailsTests.cs @@ -527,6 +527,11 @@ private void ValidateHtml(Message email) Assert.Equal("http://invalid/", email.Html.Links[2].Href); Assert.Equal("invalid", email.Html.Links[2].Text); + // Html.Codes + Assert.Equal(2, email.Html.Codes.Count); + Assert.Equal("123456", email.Html.Codes[0].Value); + Assert.Equal("G3H1Y2", email.Html.Codes[1].Value); + // Html.Images Assert.StartsWith("cid:", email.Html.Images[1].Src); Assert.Equal("Inline image 1", email.Html.Images[1].Alt); @@ -543,6 +548,11 @@ private void ValidateText(Message email) Assert.Equal(email.Text.Links[0].Href, email.Text.Links[0].Text); Assert.Equal("https://mailosaur.com/", email.Text.Links[1].Href); Assert.Equal(email.Text.Links[1].Href, email.Text.Links[1].Text); + + // Text.Codes + Assert.Equal(2, email.Text.Codes.Count); + Assert.Equal("654321", email.Text.Codes[0].Value); + Assert.Equal("5H0Y2", email.Text.Codes[1].Value); } private void ValidateHeaders(Message email) diff --git a/Mailosaur.Test/Mailer.cs b/Mailosaur.Test/Mailer.cs index 0636219..05aea26 100644 --- a/Mailosaur.Test/Mailer.cs +++ b/Mailosaur.Test/Mailer.cs @@ -18,15 +18,16 @@ public static class Mailer private static readonly string s_VerifiedDomain = Environment.GetEnvironmentVariable("MAILOSAUR_VERIFIED_DOMAIN") ?? "mailosaur.net"; private static Random s_Random = new Random(); - public static void SendEmails(MailosaurClient client, string server, int quantity) { - for (var i = 0; i < quantity; i++) + public static void SendEmails(MailosaurClient client, string server, int quantity) + { + for (var i = 0; i < quantity; i++) SendEmail(client, server); } public static void SendEmail(MailosaurClient client, string server, string sendToAddress = null) { var host = Environment.GetEnvironmentVariable("MAILOSAUR_SMTP_HOST") ?? "mailosaur.net"; - var port = Environment.GetEnvironmentVariable("MAILOSAUR_SMTP_PORT") ?? "25"; + var port = Environment.GetEnvironmentVariable("MAILOSAUR_SMTP_PORT") ?? "25"; var message = new MailMessage(); @@ -41,36 +42,36 @@ public static void SendEmail(MailosaurClient client, string server, string sendT message.To.Add($"{randomString} {randomString} <{randomToAddress}>"); // Text body - message.Body = s_Text.Replace("REPLACED_DURING_TEST", randomString); - message.IsBodyHtml = false; - message.BodyEncoding = Encoding.UTF8; + message.Body = s_Text.Replace("REPLACED_DURING_TEST", randomString); + message.IsBodyHtml = false; + message.BodyEncoding = Encoding.UTF8; - // Html body + // Html body var htmlString = s_Html.Replace("REPLACED_DURING_TEST", randomString); - var htmlView = AlternateView.CreateAlternateViewFromString(htmlString, + var htmlView = AlternateView.CreateAlternateViewFromString(htmlString, new ContentType(MediaTypeNames.Text.Html)); - htmlView.TransferEncoding = TransferEncoding.Base64; - message.AlternateViews.Add(htmlView); + htmlView.TransferEncoding = TransferEncoding.Base64; + message.AlternateViews.Add(htmlView); - var image = new LinkedResource(Path.Combine("Resources", "cat.png")); - image.ContentId = "ii_1435fadb31d523f6"; + var image = new LinkedResource(Path.Combine("Resources", "cat.png")); + image.ContentId = "ii_1435fadb31d523f6"; image.ContentType = new ContentType("image/png"); htmlView.LinkedResources.Add(image); var attachment = new System.Net.Mail.Attachment(Path.Combine("Resources", "dog.png")); attachment.ContentType = new ContentType("image/png"); - message.Attachments.Add(attachment); + message.Attachments.Add(attachment); - var smtp = new SmtpClient(); - smtp.Host = host; - smtp.Port = int.Parse(port); + var smtp = new SmtpClient(); + smtp.Host = host; + smtp.Port = int.Parse(port); - smtp.Send(message); + smtp.Send(message); } - private static string RandomString() + private static string RandomString() { - const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; return new string(Enumerable.Repeat(chars, 10) .Select(s => s[s_Random.Next(s.Length)]).ToArray()); } diff --git a/Mailosaur.Test/Resources/testEmail.html b/Mailosaur.Test/Resources/testEmail.html index abce10b..60379d7 100644 --- a/Mailosaur.Test/Resources/testEmail.html +++ b/Mailosaur.Test/Resources/testEmail.html @@ -17,6 +17,14 @@ Inline image 1 +
+ Your verification code is 123456 +
+
+
+ Your special ID is G3H1Y2 +
+

@@ -25,4 +33,4 @@ - \ No newline at end of file + diff --git a/Mailosaur.Test/Resources/testEmail.txt b/Mailosaur.Test/Resources/testEmail.txt index 773df2e..b3de48a 100644 --- a/Mailosaur.Test/Resources/testEmail.txt +++ b/Mailosaur.Test/Resources/testEmail.txt @@ -6,4 +6,8 @@ REPLACED_DURING_TEST text this is an image:[image: Inline image 1] -this is an invalid link: invalid \ No newline at end of file +Your verification code is 654321 + +Your special ID is 5H0Y2 + +this is an invalid link: invalid diff --git a/Mailosaur/Models/Code.cs b/Mailosaur/Models/Code.cs new file mode 100644 index 0000000..28569e6 --- /dev/null +++ b/Mailosaur/Models/Code.cs @@ -0,0 +1,7 @@ +namespace Mailosaur.Models +{ + public class Code + { + public string Value { get; set; } + } +} diff --git a/Mailosaur/Models/MessageContent.cs b/Mailosaur/Models/MessageContent.cs index 6adc819..5dacc88 100644 --- a/Mailosaur/Models/MessageContent.cs +++ b/Mailosaur/Models/MessageContent.cs @@ -5,6 +5,7 @@ namespace Mailosaur.Models public class MessageContent { public IList Links { get; set; } + public IList Codes { get; set; } public IList Images { get; set; } public string Body { get; set; }