Skip to content

Commit cea7ffe

Browse files
authored
Merge pull request #44 from geeklearningio/develop
0.6.0
2 parents a672ec1 + b5c09a9 commit cea7ffe

36 files changed

+5241
-197
lines changed

GeekLearning.Email.Unit.Test/Datas.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
namespace GeekLearning.Email.Unit.Test
2+
{
3+
using Microsoft.Extensions.Options;
4+
using System.Collections.Generic;
5+
6+
public class Datas
7+
{
8+
public const string FirstRecipient = "no-reply@test.geeklearning.io";
9+
public const string SecondRecipient = "no-reply2@test.geeklearning.io";
10+
public const string ThirdRecipient = "no-reply3@test.geeklearning.io";
11+
public const string MockedUpRecipient = "mockedup@test.geeklearning.io";
12+
13+
public static Internal.EmailAddress DefaultSender = new Internal.EmailAddress
14+
{
15+
DisplayName = "test user",
16+
Email = "no-reply@test.geeklearning.io"
17+
};
18+
19+
public static IOptions<EmailOptions> GetMockupOptions()
20+
{
21+
return Microsoft.Extensions.Options.Options.Create(new EmailOptions
22+
{
23+
Provider = new EmailProviderOptions
24+
{
25+
Type = "Fake",
26+
},
27+
DefaultSender = DefaultSender,
28+
Mockup = new MockupOptions
29+
{
30+
Disclaimer = "Warning, recipients are mocked.",
31+
Exceptions = new MockupExceptionsOptions
32+
{
33+
Domains = new List<string>(),
34+
Emails = new List<string>(),
35+
},
36+
Recipients = new List<string> { MockedUpRecipient }
37+
}
38+
});
39+
}
40+
}
41+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.1</TargetFramework>
5+
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
11+
<PackageReference Include="Moq" Version="4.10.0" />
12+
<PackageReference Include="xunit" Version="2.3.0" />
13+
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<ProjectReference Include="..\src\GeekLearning.Email\GeekLearning.Email.csproj" />
18+
</ItemGroup>
19+
20+
</Project>
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
namespace GeekLearning.Email.Unit.Test
2+
{
3+
using GeekLearning.Email.Internal;
4+
using GeekLearning.Storage;
5+
using GeekLearning.Templating;
6+
using Moq;
7+
using System.Collections.Generic;
8+
using System.Linq;
9+
using Xunit;
10+
11+
public class Mockups
12+
{
13+
[Fact]
14+
public void TestMockupRecipients()
15+
{
16+
var emailProvider = new Mock<IEmailProvider>();
17+
var emailProviderTypes = new List<IEmailProviderType> { new TestProviderType(emailProvider.Object) };
18+
19+
var emailSender = new EmailSender(emailProviderTypes, Datas.GetMockupOptions(), new Mock<IStorageFactory>().Object, new Mock<ITemplateLoaderFactory>().Object);
20+
emailSender.SendEmailAsync("test", "test", new EmailAddress("test@test.fr", "test"));
21+
22+
emailProvider.Verify(e => e.SendEmailAsync(
23+
Datas.DefaultSender,
24+
It.Is<IEnumerable<IEmailAddress>>(list => list.Any(a => a.DisplayName == "Mockup Recipient" && a.Email == Datas.MockedUpRecipient)),
25+
It.IsAny<IEnumerable<IEmailAddress>>(),
26+
It.IsAny<IEnumerable<IEmailAddress>>(),
27+
It.IsAny<string>(),
28+
It.IsAny<string>(),
29+
It.IsAny<string>(),
30+
Enumerable.Empty<IEmailAttachment>(),
31+
null), Times.Once);
32+
}
33+
34+
[Fact]
35+
public void TestMockupRecipientsCc()
36+
{
37+
var emailProvider = new Mock<IEmailProvider>();
38+
var emailProviderTypes = new List<IEmailProviderType> { new TestProviderType(emailProvider.Object) };
39+
40+
var emailSender = new EmailSender(emailProviderTypes, Datas.GetMockupOptions(), new Mock<IStorageFactory>().Object, new Mock<ITemplateLoaderFactory>().Object);
41+
emailSender.SendEmailAsync(Datas.DefaultSender, "test", "test", Enumerable.Empty<IEmailAttachment>(), new EmailAddress("test@test.fr", "test").Yield(), new EmailAddress("test@test.fr", "test").Yield(), new EmailAddress[0]);
42+
43+
emailProvider.Verify(e => e.SendEmailAsync(
44+
Datas.DefaultSender,
45+
It.Is<IEnumerable<IEmailAddress>>(list => list.All(a => a.DisplayName == "Mockup Recipient" && a.Email == Datas.MockedUpRecipient)),
46+
It.Is<IEnumerable<IEmailAddress>>(list => list.All(a => a.DisplayName == "Mockup Recipient" && a.Email == Datas.MockedUpRecipient)),
47+
It.IsAny<IEnumerable<IEmailAddress>>(),
48+
It.IsAny<string>(),
49+
It.IsAny<string>(),
50+
It.IsAny<string>(),
51+
Enumerable.Empty<IEmailAttachment>(),
52+
null), Times.Once);
53+
}
54+
}
55+
56+
public class TestProviderType : IEmailProviderType
57+
{
58+
public string Name => "Fake";
59+
60+
public IEmailProvider EmailProvider { get; }
61+
62+
public TestProviderType(IEmailProvider emailProvider)
63+
{
64+
EmailProvider = emailProvider;
65+
}
66+
67+
public IEmailProvider BuildProvider(IEmailProviderOptions providerOptions)
68+
{
69+
return EmailProvider;
70+
}
71+
}
72+
}

GeekLearning.Email.Unit.Test/Utils.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace GeekLearning.Email.Unit.Test
2+
{
3+
public static class Utils
4+
{
5+
public static T[] Yield<T>(this T item)
6+
{
7+
return new T[1] { item };
8+
}
9+
}
10+
}

GeekLearning.Email.sln

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GeekLearning.Email.InMemory
3030
EndProject
3131
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GeekLearning.Email.Smtp", "src\GeekLearning.Email.Smtp\GeekLearning.Email.Smtp.csproj", "{707CA417-5EED-467A-A449-73A606089835}"
3232
EndProject
33+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GeekLearning.Email.Unit.Test", "GeekLearning.Email.Unit.Test\GeekLearning.Email.Unit.Test.csproj", "{DEC2C13C-FB32-424E-92AA-404709D4FB17}"
34+
EndProject
3335
Global
3436
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3537
Debug|Any CPU = Debug|Any CPU
@@ -60,6 +62,10 @@ Global
6062
{707CA417-5EED-467A-A449-73A606089835}.Debug|Any CPU.Build.0 = Debug|Any CPU
6163
{707CA417-5EED-467A-A449-73A606089835}.Release|Any CPU.ActiveCfg = Release|Any CPU
6264
{707CA417-5EED-467A-A449-73A606089835}.Release|Any CPU.Build.0 = Release|Any CPU
65+
{DEC2C13C-FB32-424E-92AA-404709D4FB17}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
66+
{DEC2C13C-FB32-424E-92AA-404709D4FB17}.Debug|Any CPU.Build.0 = Debug|Any CPU
67+
{DEC2C13C-FB32-424E-92AA-404709D4FB17}.Release|Any CPU.ActiveCfg = Release|Any CPU
68+
{DEC2C13C-FB32-424E-92AA-404709D4FB17}.Release|Any CPU.Build.0 = Release|Any CPU
6369
EndGlobalSection
6470
GlobalSection(SolutionProperties) = preSolution
6571
HideSolutionNode = FALSE
@@ -71,5 +77,9 @@ Global
7177
{8701FCC7-0420-4E57-9FD5-5B3943FD255E} = {01E85AC0-EFEF-4564-9EE9-9A124776B72E}
7278
{8C83A791-0A38-4E42-8261-3614B336737E} = {01E85AC0-EFEF-4564-9EE9-9A124776B72E}
7379
{707CA417-5EED-467A-A449-73A606089835} = {01E85AC0-EFEF-4564-9EE9-9A124776B72E}
80+
{DEC2C13C-FB32-424E-92AA-404709D4FB17} = {8DF5B138-1052-4991-88EF-FF23AE1DCC34}
81+
EndGlobalSection
82+
GlobalSection(ExtensibilityGlobals) = postSolution
83+
SolutionGuid = {AD78B963-AC6B-4921-8160-0CA909F7D37C}
7484
EndGlobalSection
7585
EndGlobal

samples/GeekLearning.Email.Samples/Controllers/HomeController.cs

Lines changed: 113 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
5-
using Microsoft.AspNetCore.Mvc;
6-
7-
namespace GeekLearning.Email.Samples.Controllers
1+
namespace GeekLearning.Email.Samples.Controllers
82
{
3+
using GeekLearning.Email.Internal;
4+
using Microsoft.AspNetCore.Mvc;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Threading.Tasks;
8+
99
public class HomeController : Controller
1010
{
11-
private IEmailSender emailSender;
11+
private readonly IEmailSender emailSender;
1212

1313
public HomeController(IEmailSender emailSender)
1414
{
@@ -39,6 +39,111 @@ public async Task<IActionResult> SendEmail()
3939
return RedirectToAction("Index");
4040
}
4141

42+
[Route("send-cc")]
43+
public async Task<IActionResult> SendEmailWithCc()
44+
{
45+
var user = new User
46+
{
47+
Email = "john@doe.me",
48+
DisplayName = "John Doe"
49+
};
50+
51+
var userCc = new User
52+
{
53+
Email = "contensa@contenso.me",
54+
DisplayName = "Maria Contensa"
55+
};
56+
57+
var context = new
58+
{
59+
ApplicationName = "Email Sender Sample",
60+
User = user
61+
};
62+
63+
var sender = new EmailAddress("defaultsender@doe.me", "Sender");
64+
await this.emailSender.SendTemplatedEmailAsync(sender, "Invitation", context, Enumerable.Empty<IEmailAttachment>(), new IEmailAddress[] { user }, new IEmailAddress[] { userCc }, new IEmailAddress[0]);
65+
66+
return RedirectToAction("Index");
67+
}
68+
69+
[Route("send-bcc")]
70+
public async Task<IActionResult> SendEmailWithBcc()
71+
{
72+
var user = new User
73+
{
74+
Email = "john@doe.me",
75+
DisplayName = "John Doe"
76+
};
77+
78+
var userBcc = new User
79+
{
80+
Email = "contensa@contenso.me",
81+
DisplayName = "Maria Contensa"
82+
};
83+
84+
var context = new
85+
{
86+
ApplicationName = "Email Sender Sample",
87+
User = user
88+
};
89+
90+
var sender = new EmailAddress("defaultsender@doe.me", "Sender");
91+
await this.emailSender.SendTemplatedEmailAsync(sender, "Invitation", context, Enumerable.Empty<IEmailAttachment>(), new IEmailAddress[] { user }, new IEmailAddress[0], new IEmailAddress[] { userBcc });
92+
93+
return RedirectToAction("Index");
94+
}
95+
96+
[Route("send-chars")]
97+
public async Task<IActionResult> SendSpecialCharactersEmail()
98+
{
99+
var user = new User
100+
{
101+
Email = "john@doe.me",
102+
DisplayName = "John Doe"
103+
};
104+
105+
var context = new
106+
{
107+
ApplicationName = "Email Sender Sample",
108+
User = user
109+
};
110+
111+
await this.emailSender.SendTemplatedEmailAsync("SpecialChar", context, user);
112+
113+
return RedirectToAction("Index");
114+
}
115+
116+
[Route("send-attachments")]
117+
public async Task<IActionResult> SendEmailAttachments()
118+
{
119+
var user = new User
120+
{
121+
Email = "john@doe.me",
122+
DisplayName = "John Doe"
123+
};
124+
125+
var context = new
126+
{
127+
ApplicationName = "Email Sender Sample",
128+
User = user
129+
};
130+
131+
var data = System.IO.File.ReadAllBytes(@"Files\beach.jpeg");
132+
var image = new EmailAttachment("Beach.jpeg", data, "image", "jpeg");
133+
134+
data = System.IO.File.ReadAllBytes(@"Files\sample.pdf");
135+
var pdf = new EmailAttachment("Sample.pdf", data, "application", "pdf");
136+
137+
await this.emailSender.SendTemplatedEmailAsync(
138+
new EmailAddress("defaultsender@doe.me", "Sender"),
139+
"Invitation",
140+
context,
141+
new List<IEmailAttachment> { image, pdf },
142+
user);
143+
144+
return RedirectToAction("Index");
145+
}
146+
42147
public IActionResult Error()
43148
{
44149
return View();
Loading
Binary file not shown.

samples/GeekLearning.Email.Samples/GeekLearning.Email.Samples.csproj

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp1.1</TargetFramework>
4+
<TargetFramework>netcoreapp2</TargetFramework>
55
<PreserveCompilationContext>true</PreserveCompilationContext>
66
<AssemblyName>GeekLearning.Email.Samples</AssemblyName>
77
<OutputType>Exe</OutputType>
88
<PackageId>GeekLearning.Email.Samples</PackageId>
9-
<RuntimeFrameworkVersion>1.1.1</RuntimeFrameworkVersion>
10-
<PackageTargetFallback>$(PackageTargetFallback);dotnet5.6;portable-net45+win8</PackageTargetFallback>
119
</PropertyGroup>
1210

1311
<ItemGroup>
@@ -24,20 +22,29 @@
2422
</ItemGroup>
2523

2624
<ItemGroup>
27-
<PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="1.1.2" />
28-
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3" />
29-
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="1.1.2" />
30-
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.1.2" />
31-
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.2" />
32-
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="1.1.2" />
33-
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.2" />
34-
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.1.2" />
35-
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.2" />
36-
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.2" />
37-
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="1.1.2" />
38-
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="1.1.2" />
39-
<PackageReference Include="GeekLearning.Storage.FileSystem" Version="0.7.0" />
40-
<PackageReference Include="GeekLearning.Templating.Handlebars" Version="0.5.1" />
25+
<PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="2.0.0" />
26+
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.0" />
27+
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="2.0.0" />
28+
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.0.0" />
29+
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.0.0" />
30+
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.0.0" />
31+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.0" />
32+
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.0" />
33+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.0.0" />
34+
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.0.0" />
35+
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="2.0.0" />
36+
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.0.0" />
37+
<PackageReference Include="GeekLearning.Storage.FileSystem" Version="0.8.0-alpha0003" />
38+
<PackageReference Include="GeekLearning.Templating.Handlebars" Version="0.6.0-alpha0009" />
39+
</ItemGroup>
40+
41+
<ItemGroup>
42+
<None Update="Files\beach.jpeg">
43+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
44+
</None>
45+
<None Update="Files\sample.pdf">
46+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
47+
</None>
4148
</ItemGroup>
4249

4350
<Target Name="PrepublishScript" BeforeTargets="PrepareForPublish">
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<div style="margin:0;">
2+
<font face="Calibri,sans-serif" size="2">
3+
<span style="font-size:10pt;">Bonjour {{User.DisplayName}},</span>
4+
</font>
5+
</div>
6+
<div style="margin:0;">
7+
<font face="Calibri,sans-serif" size="2">
8+
<span style="font-size:10pt;">&nbsp;</span>
9+
</font>
10+
</div>
11+
<div style="margin:0;">
12+
<font face="Calibri,sans-serif" size="2">
13+
<span style="font-size:10pt;">Vous êtes cordialemente invité à la soirée spéciale "bonheur" par {{ApplicationName}}.</span>
14+
</font>
15+
</div>

0 commit comments

Comments
 (0)