|
1 | 1 | // Copyright (c) Umbraco.
|
2 | 2 | // See LICENSE for more details.
|
3 | 3 |
|
| 4 | +using System; |
| 5 | +using System.IO; |
4 | 6 | using System.Net.Mail;
|
5 | 7 | using System.Threading.Tasks;
|
| 8 | +using MailKit.Net.Smtp; |
6 | 9 | using Microsoft.Extensions.Logging;
|
7 | 10 | using Microsoft.Extensions.Options;
|
| 11 | +using MimeKit; |
| 12 | +using MimeKit.IO; |
8 | 13 | using Umbraco.Cms.Core.Configuration.Models;
|
9 | 14 | using Umbraco.Cms.Core.Events;
|
10 | 15 | using Umbraco.Cms.Core.Mail;
|
@@ -70,12 +75,56 @@ private async Task SendAsyncInternal(EmailMessage message, string emailType, boo
|
70 | 75 | }
|
71 | 76 | }
|
72 | 77 |
|
73 |
| - if (_globalSettings.IsSmtpServerConfigured == false) |
| 78 | + var isPickupDirectoryConfigured = !string.IsNullOrWhiteSpace(_globalSettings.Smtp?.PickupDirectoryLocation); |
| 79 | + |
| 80 | + if (_globalSettings.IsSmtpServerConfigured == false && !isPickupDirectoryConfigured) |
74 | 81 | {
|
75 | 82 | _logger.LogDebug("Could not send email for {Subject}. It was not handled by a notification handler and there is no SMTP configured.", message.Subject);
|
76 | 83 | return;
|
77 | 84 | }
|
78 | 85 |
|
| 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 | + |
79 | 128 | using var client = new SmtpClient();
|
80 | 129 |
|
81 | 130 | await client.ConnectAsync(_globalSettings.Smtp.Host,
|
|
0 commit comments