It took a while to find a library which provides a SMTP server. There are a lot of SMTP clients out there, but the number of SMTP servers seems very limited.
Finally, I found a library SmtpServer with an SMTP server and several hooks to extend the functionality.
Open port 25
The documentation is straightforward and well-documented.
var options = new SmtpServerOptionsBuilder()
.ServerName("localhost")
.Port(25, 587)
.Port(465, isSecure: true)
.Certificate(CreateX509Certificate2())
.Build();
// this is used to intercept the processing and inject hooks
var serviceProvider = new ServiceProvider();
serviceProvider.Add(new SampleMessageStore());
serviceProvider.Add(new SampleMailboxFilter());
serviceProvider.Add(new SampleUserAuthenticator());
// lets start the server and wait
var smtpServer = new SmtpServer.SmtpServer(options, serviceProvider);
await smtpServer.StartAsync(CancellationToken.None);
Wait for Mail
This example reads the MimePart of the attachment and shows the attachments filename.
public class SampleMessageStore : MessageStore
{
public override async Task<SmtpResponse> SaveAsync(
ISessionContext context,
IMessageTransaction transaction,
ReadOnlySequence<byte> buffer,
CancellationToken cancellationToken)
{
await using var stream = new MemoryStream();
var position = buffer.GetPosition(0);
while (buffer.TryGet(ref position, out var memory))
{
await stream.WriteAsync(memory, cancellationToken);
}
stream.Position = 0;
var message = await MimeKit.MimeMessage.LoadAsync(stream, cancellationToken);
Console.WriteLine("Mail received: " + message.Subject);
foreach (var attachment in message.Attachments)
{
Console.WriteLine("...Attachment found: " + (attachment as MimePart)?.FileName);
}
return SmtpResponse.Ok;
}
}
Send Mail
For sending am email, a PowerShell commandlet can be used.
Send-MailMessage -To foo@bar.de -Subject "Hello User" -Body "This is the message" -SmtpServer "localhost" -From "bar@foo.de" -Attachments .\Documents\IMG-20220425-WA0058.jpg
Schreibe einen Kommentar