Skip to content

Commit 4b2d919

Browse files
add-csharp-smtp
1 parent ea34a15 commit 4b2d919

File tree

8 files changed

+136
-0
lines changed

8 files changed

+136
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
*.sln.DotSettings.user
2+
.idea
3+
bin
4+
obj
5+
[Dd]ebug/
6+
[Dd]ebugPublic/
7+
[Rr]elease/
8+
[Rr]eleases/
9+
x64/
10+
x86/
11+
build/
12+
bld/
13+
[Bb]in/
14+
[Oo]bj/
15+
msbuild.log
16+
msbuild.err
17+
msbuild.wrn

csharp-smtp-client-xunit/Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-include ../.env
2+
3+
install:
4+
dotnet restore
5+
dotnet build
6+
7+
test:
8+
API_KEY=$(API_KEY) dotnet test --logger "console;verbosity=detailed"
9+

csharp-smtp-client-xunit/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# CSharp SMTP Client Test
2+
Send email and receive using MailSlurp SMPT server.
3+
4+
## Install
5+
6+
![dotnet-install-mailslurp](nuget-add-mailslurp.png)
7+
8+
9+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
7+
<IsPackable>false</IsPackable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="FluentAssertions" Version="6.4.0"/>
12+
<PackageReference Include="mailslurp" Version="15.5.0"/>
13+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0"/>
14+
<PackageReference Include="xunit" Version="2.4.1"/>
15+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
16+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
17+
<PrivateAssets>all</PrivateAssets>
18+
</PackageReference>
19+
<PackageReference Include="coverlet.collector" Version="3.1.0">
20+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
21+
<PrivateAssets>all</PrivateAssets>
22+
</PackageReference>
23+
</ItemGroup>
24+
25+
<ItemGroup>
26+
<ProjectReference Include="..\SmtpService\SmtpService.csproj"/>
27+
</ItemGroup>
28+
29+
</Project>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System;
2+
using System.Net;
3+
using System.Net.Mail;
4+
using mailslurp.Api;
5+
using mailslurp.Client;
6+
using mailslurp.Model;
7+
using Xunit;
8+
9+
namespace SmtpService.Tests
10+
{
11+
public class UnitTest1
12+
{
13+
[Fact]
14+
public void CanSendEmailWithMailSlurpSmtp()
15+
{
16+
var apiKey = Environment.GetEnvironmentVariable("API_KEY")
17+
?? throw new ArgumentNullException("Missing API_KEY environment variable containing MailSlurp key");
18+
19+
// configure client
20+
var config = new Configuration();
21+
config.ApiKey.Add("x-api-key", apiKey);
22+
var inboxController = new InboxControllerApi(config);
23+
24+
// create an smtp inbox
25+
var inbox = inboxController.CreateInboxWithOptions(new CreateInboxDto(
26+
inboxType: CreateInboxDto.InboxTypeEnum.SMTPINBOX
27+
));
28+
Assert.Contains("@mailslurp.mx", inbox.EmailAddress);
29+
30+
// get smtp host, port, password, username etc
31+
var imapSmtpAccessDetails = inboxController.GetImapSmtpAccess();
32+
var smtpClient = new SmtpClient(imapSmtpAccessDetails.SmtpServerHost)
33+
{
34+
Port = imapSmtpAccessDetails.SmtpServerPort,
35+
Credentials = new NetworkCredential(userName: imapSmtpAccessDetails.SmtpUsername, password: imapSmtpAccessDetails.SmtpPassword),
36+
// disable ssl recommended
37+
EnableSsl = false
38+
};
39+
40+
// send email to inbox
41+
smtpClient.Send(from: "[email protected]", recipients: inbox.EmailAddress, subject: "This inbound", body: "Hello");
42+
43+
// wait for email to arrive
44+
var waitController = new WaitForControllerApi(config);
45+
waitController.WaitForLatestEmail(inboxId: inbox.Id, timeout: 30_000);
46+
}
47+
}
48+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
#
4+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SmtpService.Tests", "SmtpService.Tests\SmtpService.Tests.csproj", "{8D9AA216-A769-4921-A814-6D330B92B31D}"
5+
EndProject
6+
Global
7+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
8+
Debug|Any CPU = Debug|Any CPU
9+
Release|Any CPU = Release|Any CPU
10+
EndGlobalSection
11+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
12+
{8D9AA216-A769-4921-A814-6D330B92B31D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
13+
{8D9AA216-A769-4921-A814-6D330B92B31D}.Debug|Any CPU.Build.0 = Debug|Any CPU
14+
{8D9AA216-A769-4921-A814-6D330B92B31D}.Release|Any CPU.ActiveCfg = Release|Any CPU
15+
{8D9AA216-A769-4921-A814-6D330B92B31D}.Release|Any CPU.Build.0 = Release|Any CPU
16+
EndGlobalSection
17+
EndGlobal
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"sdk": {
3+
"version": "6.0",
4+
"rollForward": "latestMajor",
5+
"allowPrerelease": true
6+
}
7+
}
68.7 KB
Loading

0 commit comments

Comments
 (0)