Skip to content

Commit e7da855

Browse files
Added support for Smtp PickupDirectory (#11253)
* Added support for Smtp PickupDirectory * Add reference to the implementation outline by MailKit Co-authored-by: Bjarke Berg <[email protected]> Co-authored-by: Bjarke Berg <[email protected]>
1 parent 79fdb9a commit e7da855

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

src/Umbraco.Infrastructure/Mail/EmailSender.cs

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
// Copyright (c) Umbraco.
22
// See LICENSE for more details.
33

4+
using System;
5+
using System.IO;
46
using System.Net.Mail;
57
using System.Threading.Tasks;
8+
using MailKit.Net.Smtp;
69
using Microsoft.Extensions.Logging;
710
using Microsoft.Extensions.Options;
11+
using MimeKit;
12+
using MimeKit.IO;
813
using Umbraco.Cms.Core.Configuration.Models;
914
using Umbraco.Cms.Core.Events;
1015
using Umbraco.Cms.Core.Mail;
@@ -70,12 +75,56 @@ private async Task SendAsyncInternal(EmailMessage message, string emailType, boo
7075
}
7176
}
7277

73-
if (_globalSettings.IsSmtpServerConfigured == false)
78+
var isPickupDirectoryConfigured = !string.IsNullOrWhiteSpace(_globalSettings.Smtp?.PickupDirectoryLocation);
79+
80+
if (_globalSettings.IsSmtpServerConfigured == false && !isPickupDirectoryConfigured)
7481
{
7582
_logger.LogDebug("Could not send email for {Subject}. It was not handled by a notification handler and there is no SMTP configured.", message.Subject);
7683
return;
7784
}
7885

86+
if (isPickupDirectoryConfigured && !string.IsNullOrWhiteSpace(_globalSettings.Smtp?.From))
87+
{
88+
// The following code snippet is the recommended way to handle PickupDirectoryLocation.
89+
// See more https://github.com/jstedfast/MailKit/blob/master/FAQ.md#q-how-can-i-send-email-to-a-specifiedpickupdirectory
90+
do {
91+
var path = Path.Combine(_globalSettings.Smtp?.PickupDirectoryLocation, Guid.NewGuid () + ".eml");
92+
Stream stream;
93+
94+
try
95+
{
96+
stream = File.Open(path, FileMode.CreateNew);
97+
}
98+
catch (IOException)
99+
{
100+
if (File.Exists(path))
101+
{
102+
continue;
103+
}
104+
throw;
105+
}
106+
107+
try {
108+
using (stream)
109+
{
110+
using var filtered = new FilteredStream(stream);
111+
filtered.Add(new SmtpDataFilter());
112+
113+
FormatOptions options = FormatOptions.Default.Clone();
114+
options.NewLineFormat = NewLineFormat.Dos;
115+
116+
await message.ToMimeMessage(_globalSettings.Smtp?.From).WriteToAsync(options, filtered);
117+
filtered.Flush();
118+
return;
119+
120+
}
121+
} catch {
122+
File.Delete(path);
123+
throw;
124+
}
125+
} while (true);
126+
}
127+
79128
using var client = new SmtpClient();
80129

81130
await client.ConnectAsync(_globalSettings.Smtp.Host,

0 commit comments

Comments
 (0)