Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions Examples/SampleTests/SampleMailClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using MailKit.Net.Smtp;
using MimeKit;

namespace SampleTests;

public static class SampleMailClient
{
public static void Send(
string from = null,
string to = null,
string subject = null,
string user = null,
string password = null,
MimeEntity body = null,
int count = 1,
bool useSsl = false,
int port = 9025)
{
//var message = MimeMessage.Load(@"C:\Dev\Cain\Temp\message.eml");
var message = new MimeMessage();

message.From.Add(MailboxAddress.Parse(from ?? "from@sample.com"));
message.To.Add(MailboxAddress.Parse(to ?? "to@sample.com"));
message.Subject = subject ?? "Hello";
message.Body = body ?? new TextPart("plain")
{
Text = "Hello World"
};

using var client = new SmtpClient();

client.Connect("localhost", port, useSsl);

if (user != null && password != null)
{
client.Authenticate(user, password);
}

while (count-- > 0)
{
client.Send(message);
}

client.Disconnect(true);
}
}
23 changes: 23 additions & 0 deletions Examples/SampleTests/SampleTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.1" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.8" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Src\SmtpServer.Tests\SmtpServer.Tests.csproj" />
<ProjectReference Include="..\..\Src\SmtpServer\SmtpServer.csproj" />
</ItemGroup>

</Project>
51 changes: 51 additions & 0 deletions Examples/SampleTests/SendingEmailTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using SmtpServer;
using SmtpServer.ComponentModel;
using SmtpServer.Tests.Mocks;

namespace SampleTests
{
[TestClass]
public class SendingEmailTests
{
MockMessageStore TestMessageStore = new MockMessageStore();

[TestMethod]
public void SendEmail()
{
// Arrange
var cancellationTokenSource = new CancellationTokenSource();

var options = new SmtpServerOptionsBuilder()
.ServerName("SmtpServer SampleApp")
.Port(25)
.Build();

var serviceProvider = new ServiceProvider();
serviceProvider.Add(TestMessageStore);

var server = new SmtpServer.SmtpServer(options, serviceProvider);
var serverTask = server.StartAsync(cancellationTokenSource.Token);

// Act
SampleMailClient.Send(port: 25);

// Assert
cancellationTokenSource.Cancel();
try
{
serverTask.Wait();
}
catch (AggregateException e)
{
e.Handle(exception => exception is OperationCanceledException);
}

Assert.AreEqual(1, this.TestMessageStore.Messages.Count);
var message = this.TestMessageStore.Messages.First();
Assert.AreEqual(1, message.MimeMessage.From.Count);
var fromAddress = message.MimeMessage.From.Mailboxes.Single();
Assert.AreEqual("", fromAddress.Name);
Assert.AreEqual("from@sample.com", fromAddress.Address);
}
}
}
1 change: 1 addition & 0 deletions Examples/SampleTests/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Microsoft.VisualStudio.TestTools.UnitTesting;
11 changes: 9 additions & 2 deletions Src/SmtpServer.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.28803.352
# Visual Studio Version 17
VisualStudioVersion = 17.8.34004.107
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SmtpServer", "SmtpServer\SmtpServer.csproj", "{0A7CFC3D-305C-4018-9052-3A7A8B5DD104}"
EndProject
Expand All @@ -15,6 +15,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Examples", "Examples", "{6B
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SampleApp", "..\Examples\SampleApp\SampleApp.csproj", "{DB671922-7280-4854-9C4F-0BA073B5F1E2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleTests", "..\Examples\SampleTests\SampleTests.csproj", "{121B7063-DB75-48E2-86B4-9B2956C9A12C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -41,13 +43,18 @@ Global
{DB671922-7280-4854-9C4F-0BA073B5F1E2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DB671922-7280-4854-9C4F-0BA073B5F1E2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DB671922-7280-4854-9C4F-0BA073B5F1E2}.Release|Any CPU.Build.0 = Release|Any CPU
{121B7063-DB75-48E2-86B4-9B2956C9A12C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{121B7063-DB75-48E2-86B4-9B2956C9A12C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{121B7063-DB75-48E2-86B4-9B2956C9A12C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{121B7063-DB75-48E2-86B4-9B2956C9A12C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{EE0C474F-8404-4FB6-865F-A034B5DB77FE} = {6BAD2430-FA6B-4929-8BD7-66663CA02207}
{DB671922-7280-4854-9C4F-0BA073B5F1E2} = {6BAD2430-FA6B-4929-8BD7-66663CA02207}
{121B7063-DB75-48E2-86B4-9B2956C9A12C} = {6BAD2430-FA6B-4929-8BD7-66663CA02207}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3050CFB6-1836-4A06-8AA4-C9E75A9B9147}
Expand Down