case 4310

This commit is contained in:
2022-11-21 00:25:14 +00:00
parent 334e00ba43
commit f412e6a66b
2 changed files with 27 additions and 19 deletions

View File

@@ -243,8 +243,18 @@ namespace AyaNova.Api.Controllers
if (!ModelState.IsValid) if (!ModelState.IsValid)
return BadRequest(new ApiErrorResponse(ModelState)); return BadRequest(new ApiErrorResponse(ModelState));
if (!ServerGlobalOpsSettingsCache.Notify.SmtpDeliveryActive)
return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_MISSING_PROPERTY, null, "Email notifications are set to OFF at server, unable to send 'on request' type SMTP notification"));
//validate //for now I'm allowing any authenticated user to call this route intentionally as the use case is for our own internal messaging from the Customer form only or for API users who want to send
//email when rendering a report (case 4310).
//validate incoming data
if (string.IsNullOrWhiteSpace(notifyDirectSMTP.Subject))
return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_MISSING_PROPERTY, "Subject", "Subject field is required"));
if (string.IsNullOrWhiteSpace(notifyDirectSMTP.TextBody) && string.IsNullOrWhiteSpace(notifyDirectSMTP.HTMLBody))
return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_MISSING_PROPERTY, null, "TextBody or HTMLBody field is required"));
if (string.IsNullOrWhiteSpace(notifyDirectSMTP.ToAddress)) if (string.IsNullOrWhiteSpace(notifyDirectSMTP.ToAddress))
{ {
@@ -277,19 +287,7 @@ namespace AyaNova.Api.Controllers
IMailer m = AyaNova.Util.ServiceProviderProvider.Mailer; IMailer m = AyaNova.Util.ServiceProviderProvider.Mailer;
try try
{ {
if (!ServerGlobalOpsSettingsCache.Notify.SmtpDeliveryActive) await m.SendEmailAsync(notifyDirectSMTP.ToAddress, "Test from Notification system", "This is a test to confirm notification system is working", ServerGlobalOpsSettingsCache.Notify);
{
await NotifyEventHelper.AddGeneralNotifyEvent(NotifyEventType.GeneralNotification,
$"Email notifications are set to OFF at server, unable to send 'on request' type SMTP notification subject:{notifyDirectSMTP.Subject}",
"Error",
null,
UserId);
log.LogInformation($"** WARNING: SMTP notification is currently set to Active=False; unable to send 'on request' type SMTP notification subject:{notifyDirectSMTP.Subject} **");
}
else
await m.SendEmailAsync(notifyDirectSMTP.ToAddress, "Test from Notification system", "This is a test to confirm notification system is working", ServerGlobalOpsSettingsCache.Notify);
} }
catch (Exception ex) catch (Exception ex)
{ {

View File

@@ -9,7 +9,7 @@ namespace AyaNova.Util
{ {
public interface IMailer public interface IMailer
{ {
Task SendEmailAsync(string email, string subject, string body, AyaNova.Models.GlobalOpsNotificationSettings smtpSettings, string attachPDF = null, string forceFileName = null); Task SendEmailAsync(string email, string subject, string body, AyaNova.Models.GlobalOpsNotificationSettings smtpSettings, string attachPDF = null, string forceFileName = null, string htmlBody = null);
} }
public class Mailer : IMailer public class Mailer : IMailer
@@ -19,7 +19,7 @@ namespace AyaNova.Util
} }
public async Task SendEmailAsync(string email, string subject, string body, AyaNova.Models.GlobalOpsNotificationSettings smtpSettings, string attachPDFPath = null, string forceFileName = null) public async Task SendEmailAsync(string email, string subject, string body, AyaNova.Models.GlobalOpsNotificationSettings smtpSettings, string attachPDFPath = null, string forceFileName = null, string htmlBody = null)
{ {
try try
{ {
@@ -49,8 +49,18 @@ namespace AyaNova.Util
} }
else else
{ {
// if (!string.IsNullOrWhiteSpace(body))
// message.Body = new TextPart(TextFormat.Plain) { Text = body };
MimeKit.BodyBuilder builder = new BodyBuilder();
if (!string.IsNullOrWhiteSpace(body)) if (!string.IsNullOrWhiteSpace(body))
message.Body = new TextPart(TextFormat.Plain) { Text = body }; builder.TextBody = body;
if (!string.IsNullOrWhiteSpace(htmlBody))
builder.HtmlBody = htmlBody;
message.Body = builder.ToMessageBody();
} }
using (var client = new SmtpClient()) using (var client = new SmtpClient())