https://github.com/umbraco/Umbraco-CMS/pull/9252 - Changed EmailMessage to be completely immutable

Signed-off-by: Bjarke Berg <mail@bergmania.dk>
This commit is contained in:
Bjarke Berg
2020-10-23 12:37:23 +02:00
parent d7eca3d0d5
commit 5fe849c8e2
5 changed files with 20 additions and 19 deletions

View File

@@ -8,19 +8,28 @@ namespace Umbraco.Core.Models
public string To { get; }
public string Subject { get; }
public string Body { get; }
public bool IsBodyHtml { get; set; } = false;
public bool IsBodyHtml { get; }
public EmailMessage(string from, string to, string subject, string body)
public EmailMessage(string from, string to, string subject, string body, bool isBodyHtml)
{
if (string.IsNullOrEmpty(from)) throw new ArgumentException("Value cannot be null or empty.", nameof(from));
if (string.IsNullOrEmpty(to)) throw new ArgumentException("Value cannot be null or empty.", nameof(to));
if (string.IsNullOrEmpty(subject)) throw new ArgumentException("Value cannot be null or empty.", nameof(subject));
if (string.IsNullOrEmpty(body)) throw new ArgumentException("Value cannot be null or empty.", nameof(body));
if (from == null) throw new ArgumentNullException(nameof(from));
if (from.Length == 0) throw new ArgumentException("Value cannot be empty.", nameof(from));
if (to == null) throw new ArgumentNullException(nameof(to));
if (to.Length == 0) throw new ArgumentException("Value cannot be empty.", nameof(to));
if (subject == null) throw new ArgumentNullException(nameof(subject));
if (subject.Length == 0) throw new ArgumentException("Value cannot be empty.", nameof(subject));
if (body == null) throw new ArgumentNullException(nameof(body));
if (body.Length == 0) throw new ArgumentException("Value cannot be empty.", nameof(body));
From = from;
To = to;
Subject = subject;
Body = body;
IsBodyHtml = isBodyHtml;
}
}
}

View File

@@ -82,10 +82,8 @@ namespace Umbraco.Web.HealthCheck.NotificationMethods
if (string.IsNullOrWhiteSpace(subject))
subject = "Umbraco Health Check Status";
return new EmailMessage(to, RecipientEmail, subject, message)
{
IsBodyHtml = message.IsNullOrWhiteSpace() == false && message.Contains("<") && message.Contains("</")
};
var isBodyHtml = message.IsNullOrWhiteSpace() == false && message.Contains("<") && message.Contains("</");
return new EmailMessage(to, RecipientEmail, subject, message, isBodyHtml);
}
}
}

View File

@@ -435,7 +435,7 @@ namespace Umbraco.Core.Services.Implement
}
// create the mail message
var mail = new EmailMessage(fromMail, mailingUser.Email, subject, body) {IsBodyHtml = isBodyHtml};
var mail = new EmailMessage(fromMail, mailingUser.Email, subject, body, isBodyHtml);
return new NotificationRequest(mail, actionName, mailingUser.Name, mailingUser.Email);
}

View File

@@ -322,10 +322,7 @@ namespace Umbraco.Web.BackOffice.Controllers
// Ensure the culture of the found user is used for the email!
UmbracoUserExtensions.GetUserCulture(identityUser.Culture, _textService, _globalSettings));
var mailMessage = new EmailMessage(from, user.Email, subject, message)
{
IsBodyHtml = true
};
var mailMessage = new EmailMessage(from, user.Email, subject, message, true);
await _emailSender.SendAsync(mailMessage);

View File

@@ -542,10 +542,7 @@ namespace Umbraco.Web.BackOffice.Controllers
UmbracoUserExtensions.GetUserCulture(to.Language, _localizedTextService, _globalSettings),
new[] { userDisplay.Name, from, message, inviteUri.ToString(), fromEmail });
var mailMessage = new EmailMessage(fromEmail, to.Email, emailSubject, emailBody)
{
IsBodyHtml = true
};
var mailMessage = new EmailMessage(fromEmail, to.Email, emailSubject, emailBody, true);
await _emailSender.SendAsync(mailMessage);
}