Skip to content

Commit

Permalink
Add support for verification codes
Browse files Browse the repository at this point in the history
  • Loading branch information
jm-mailosaur committed Mar 8, 2022
1 parent 74617ad commit e0383f2
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 21 deletions.
10 changes: 10 additions & 0 deletions Mailosaur.Test/EmailsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)
Expand Down
39 changes: 20 additions & 19 deletions Mailosaur.Test/Mailer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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());
}
Expand Down
10 changes: 9 additions & 1 deletion Mailosaur.Test/Resources/testEmail.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@
<img src="cid:ii_1435fadb31d523f6" alt="Inline image 1">
</a>
</div>
<div>
Your verification code is 123456
<br>
</div>
<div>
Your special ID is G3H1Y2
<br>
</div>
<div>
<br>
</div>
Expand All @@ -25,4 +33,4 @@
</div>
</div>
</div>
</div>
</div>
6 changes: 5 additions & 1 deletion Mailosaur.Test/Resources/testEmail.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ REPLACED_DURING_TEST text

this is an image:[image: Inline image 1] <https://mailosaur.com/>

this is an invalid link: invalid
Your verification code is 654321

Your special ID is 5H0Y2

this is an invalid link: invalid
7 changes: 7 additions & 0 deletions Mailosaur/Models/Code.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Mailosaur.Models
{
public class Code
{
public string Value { get; set; }
}
}
1 change: 1 addition & 0 deletions Mailosaur/Models/MessageContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace Mailosaur.Models
public class MessageContent
{
public IList<Link> Links { get; set; }
public IList<Code> Codes { get; set; }
public IList<Image> Images { get; set; }
public string Body { get; set; }

Expand Down

0 comments on commit e0383f2

Please sign in to comment.