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
6 changes: 4 additions & 2 deletions src/Senders/FluentEmail.MailKit/MailKitSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,13 @@ private async Task SaveToPickupDirectory(MimeMessage message, string pickupDirec
}

/// <summary>
/// Create a MimMessage so MailKit can send it
/// Create a MimeMessage so MailKit can send it.
/// Virtual so subclasses can preprocess the message before sending
/// (e.g. DKIM-sign it) — see https://github.com/lukencode/FluentEmail/issues/388
/// </summary>
/// <returns>The mail message.</returns>
/// <param name="email">Email data.</param>
private MimeMessage CreateMailMessage(IFluentEmail email)
protected virtual MimeMessage CreateMailMessage(IFluentEmail email)
{
var data = email.Data;

Expand Down
48 changes: 48 additions & 0 deletions test/FluentEmail.Core.Tests/MailKitSmtpSenderTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using FluentEmail.Core;
using FluentEmail.Core.Interfaces;
using FluentEmail.MailKitSmtp;
using MimeKit;
using NUnit.Framework;
using Attachment = FluentEmail.Core.Models.Attachment;

Expand Down Expand Up @@ -119,5 +122,50 @@ public void CanSendHtmlAndPlaintextTogether()

Assert.IsTrue(response.Successful);
}

[Test]
public void CanPreprocessMessageByOverridingCreateMailMessage()
{
var sender = new PreprocessingMailKitSender(new SmtpClientOptions
{
Server = "localhost",
Port = 25,
UseSsl = false,
RequiresAuthentication = false,
UsePickupDirectory = true,
MailPickupDirectory = tempDirectory
});
Email.DefaultSender = sender;

var email = Email
.From(fromEmail)
.To(toEmail)
.Subject(subject)
.Body(body);

var response = email.Send();

Assert.IsTrue(response.Successful);
var eml = Directory.EnumerateFiles(tempDirectory, "*.eml").First();
var content = File.ReadAllText(eml);
Assert.IsTrue(content.Contains("X-Preprocessed: yes"),
"Override of CreateMailMessage should be able to modify the message before sending.");
}

/// <summary>
/// Simulates e.g. DKIM-signing: preprocess the MimeMessage after creation.
/// Possible since CreateMailMessage is protected virtual (issue #388).
/// </summary>
private class PreprocessingMailKitSender : MailKitSender
{
public PreprocessingMailKitSender(SmtpClientOptions options) : base(options) { }

protected override MimeMessage CreateMailMessage(IFluentEmail email)
{
var message = base.CreateMailMessage(email);
message.Headers.Add("X-Preprocessed", "yes");
return message;
}
}
}
}