Skip to content

Commit 6ae61af

Browse files
Bugfix for e-mail sending (#115)
* Features/upgrate to dotnet 6 (#108) * Moved files to src; Adapted C# code to a more modern approach; Minor refactoring everywhere * Removed old project folders/files * Removed temporary old solution * Refactored code; Set some members to nullable * Changed usage on TryUpdateModelAsync * Features/fix email bugs (#110) * Moved files to src; Adapted C# code to a more modern approach; Minor refactoring everywhere * Removed old project folders/files * Removed temporary old solution * Refactored code; Set some members to nullable * Changed usage on TryUpdateModelAsync * Fixed e-mail issues * Features/fix email bugs (#112) * Moved files to src; Adapted C# code to a more modern approach; Minor refactoring everywhere * Removed old project folders/files * Removed temporary old solution * Refactored code; Set some members to nullable * Changed usage on TryUpdateModelAsync * Fixed e-mail issues * Feature/fix email send issues (#114) * Removed development files * Updated .gitignore * Added MailKit library * Implemented e-mail sender with MailKit
1 parent cc65b8f commit 6ae61af

File tree

6 files changed

+40
-83
lines changed

6 files changed

+40
-83
lines changed

.gitignore

+2-3
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,7 @@ paket-files/
257257
# CmsEngine
258258
PublishOutput/
259259
package-lock.json
260-
/CmsEngine.Ui/certificate.Development.json
261-
/CmsEngine.Ui/emailsettings.Development.json
260+
*.Development.json
262261
[Rr]eadme.md
263-
/CmsEngine.Ui/_logs
262+
CmsEngine.Ui/_logs
264263
global.json

src/CmsEngine.Application/CmsEngine.Application.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10+
<PackageReference Include="MailKit" Version="3.1.1" />
1011
<PackageReference Include="Microsoft.AspNetCore.Authentication.Abstractions" Version="2.2.0" />
1112
<PackageReference Include="Microsoft.AspNetCore.Mvc.ViewFeatures" Version="2.2.0" />
1213
<PackageReference Include="System.Drawing.Common" Version="6.0.0" />

src/CmsEngine.Application/GlobalUsings.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
global using System.Globalization;
66
global using System.Linq.Expressions;
77
global using System.Net;
8-
global using System.Net.Mail;
98
global using System.Reflection;
10-
global using System.Text;
119
global using System.Text.Encodings.Web;
1210
global using System.Xml.Linq;
1311
global using CmsEngine.Application.Attributes;
@@ -25,12 +23,14 @@
2523
global using CmsEngine.Core.Utils;
2624
global using CmsEngine.Data;
2725
global using CmsEngine.Data.Entities;
26+
global using MailKit.Net.Smtp;
2827
global using Microsoft.AspNetCore.Authentication;
2928
global using Microsoft.AspNetCore.Http;
3029
global using Microsoft.AspNetCore.Identity;
3130
global using Microsoft.AspNetCore.Mvc.Rendering;
3231
global using Microsoft.Extensions.Caching.Memory;
3332
global using Microsoft.Extensions.Logging;
3433
global using Microsoft.Extensions.Options;
34+
global using MimeKit;
3535
global using Newtonsoft.Json;
3636
global using Newtonsoft.Json.Linq;

src/CmsEngine.Application/Helpers/Email/CmsEngineEmailSender.cs

+35-37
Original file line numberDiff line numberDiff line change
@@ -20,54 +20,52 @@ private async Task ExecuteAsync(ContactForm contactForm)
2020
{
2121
_logger.LogDebug("SendEmailAsync(contactForm: {0})", contactForm.ToString());
2222

23-
var from = contactForm.From ?? _emailSettings.Username;
24-
var body = $"From: {from}\r\nTo: {contactForm.To}\r\n-----\r\n\r\n{contactForm.Message}";
23+
var message = PrepareMailMessage(contactForm);
2524

2625
try
2726
{
28-
var message = new MailMessage
27+
using (var smtpClient = new SmtpClient())
2928
{
30-
From = new MailAddress(from),
31-
Subject = $"🌐 CmsEngine - {contactForm.Subject}",
32-
SubjectEncoding = Encoding.UTF8,
33-
IsBodyHtml = false,
34-
Body = body,
35-
BodyEncoding = Encoding.UTF8,
36-
Priority = MailPriority.Normal
37-
};
38-
39-
if (!string.IsNullOrWhiteSpace(contactForm.To))
40-
{
41-
message.To.Add(contactForm.To);
29+
await smtpClient.ConnectAsync(_emailSettings.Domain, _emailSettings.Port, true);
30+
await smtpClient.AuthenticateAsync(_emailSettings.Username, _emailSettings.Password);
31+
await smtpClient.SendAsync(message);
32+
await smtpClient.DisconnectAsync(true);
4233
}
4334

44-
if (!string.IsNullOrWhiteSpace(_emailSettings.CcEmail))
45-
{
46-
message.CC.Add(_emailSettings.CcEmail);
47-
}
48-
49-
if (!string.IsNullOrWhiteSpace(_emailSettings.BccEmail))
50-
{
51-
message.Bcc.Add(_emailSettings.BccEmail);
52-
}
53-
54-
using (var smtp = new SmtpClient(_emailSettings.Domain, _emailSettings.Port))
55-
{
56-
smtp.EnableSsl = true;
57-
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
58-
smtp.UseDefaultCredentials = false;
59-
smtp.Credentials = new NetworkCredential(_emailSettings.Username, _emailSettings.Password);
60-
61-
_logger.LogDebug("Message {0}", message.ToString());
62-
await smtp.SendMailAsync(message);
63-
}
64-
65-
_logger.LogDebug("Email sent from {0} to {1}", message.From, message.To[0]);
35+
_logger.LogDebug("Email sent from {0} to {1}", message.From[0], message.To[0]);
6636
}
6737
catch (Exception ex)
6838
{
6939
_logger.LogError(ex, "Error when sending e-mail");
7040
throw new EmailException("Error when sending e-mail", ex);
7141
}
7242
}
43+
44+
private MimeMessage PrepareMailMessage(ContactForm contactForm)
45+
{
46+
var from = contactForm.From;
47+
var body = contactForm.Message;
48+
49+
if (string.IsNullOrWhiteSpace(from))
50+
{
51+
from = _emailSettings.Username;
52+
body = $"From: {from}\r\nTo: {contactForm.To}\r\n-----\r\n\r\n{body}";
53+
}
54+
55+
var message = new MimeMessage();
56+
message.From.Add(new MailboxAddress(from, from));
57+
message.Subject = $"🌐 CmsEngine - {contactForm.Subject}";
58+
59+
message.Body = new TextPart("plain")
60+
{
61+
Text = body
62+
};
63+
64+
if (!string.IsNullOrWhiteSpace(contactForm.To))
65+
{
66+
message.To.Add(new MailboxAddress(contactForm.To, contactForm.To));
67+
}
68+
69+
return message;
70+
}
7371
}

src/CmsEngine.Ui/appsettings.Development.json

-18
This file was deleted.

src/CmsEngine.Ui/emailsettings.development.json

-23
This file was deleted.

0 commit comments

Comments
 (0)