From 78b7b059eabfeac2f4e517083192f8549cd0a2a6 Mon Sep 17 00:00:00 2001 From: Gary Sharp Date: Thu, 16 May 2024 22:11:10 +1000 Subject: [PATCH] feature: expression helpers - basic email sending --- Disco.Models/Services/Messaging/Email.cs | 2 +- .../Services/Messaging/EmailAttachment.cs | 1 + Disco.Services/Expressions/Expression.cs | 8 ++++- .../Expressions/Extensions/EmailExt.cs | 32 +++++++++++++++++++ Disco.Services/Messaging/EmailService.cs | 6 ++++ 5 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 Disco.Services/Expressions/Extensions/EmailExt.cs diff --git a/Disco.Models/Services/Messaging/Email.cs b/Disco.Models/Services/Messaging/Email.cs index e12ad07c..55f973a7 100644 --- a/Disco.Models/Services/Messaging/Email.cs +++ b/Disco.Models/Services/Messaging/Email.cs @@ -25,7 +25,7 @@ namespace Disco.Models.Services.Messaging public Email(string to, string subject, string body) : this() { - To.Add(to); + To.AddRange(ParseEmailAddresses(to)); Subject = subject; Body = body; } diff --git a/Disco.Models/Services/Messaging/EmailAttachment.cs b/Disco.Models/Services/Messaging/EmailAttachment.cs index 1e69234a..2294c078 100644 --- a/Disco.Models/Services/Messaging/EmailAttachment.cs +++ b/Disco.Models/Services/Messaging/EmailAttachment.cs @@ -4,5 +4,6 @@ { public string Name { get; set; } public byte[] Data { get; set; } + public string MediaType { get; set; } } } diff --git a/Disco.Services/Expressions/Expression.cs b/Disco.Services/Expressions/Expression.cs index 99c958b1..4d7411d6 100644 --- a/Disco.Services/Expressions/Expression.cs +++ b/Disco.Services/Expressions/Expression.cs @@ -38,9 +38,10 @@ namespace Disco.Services.Expressions public static void InitializeExpressions() { TypeRegistry.RegisterType("DataExt", typeof(Extensions.DataExt)); - TypeRegistry.RegisterType("UserExt", typeof(Extensions.UserExt)); TypeRegistry.RegisterType("DeviceExt", typeof(Extensions.DeviceExt)); + TypeRegistry.RegisterType("EmailExt", typeof(Extensions.EmailExt)); TypeRegistry.RegisterType("ImageExt", typeof(Extensions.ImageExt)); + TypeRegistry.RegisterType("UserExt", typeof(Extensions.UserExt)); } public T EvaluateFirst(object ExpressionContext, IDictionary Variables) @@ -297,6 +298,11 @@ namespace Disco.Services.Expressions typeof(Extensions.DeviceExt).AssemblyQualifiedName }, + { + "EmailExt", + typeof(Extensions.EmailExt).AssemblyQualifiedName + }, + { "ImageExt", typeof(Extensions.ImageExt).AssemblyQualifiedName diff --git a/Disco.Services/Expressions/Extensions/EmailExt.cs b/Disco.Services/Expressions/Extensions/EmailExt.cs new file mode 100644 index 00000000..9b4780ce --- /dev/null +++ b/Disco.Services/Expressions/Extensions/EmailExt.cs @@ -0,0 +1,32 @@ +using Disco.Models.Repository; +using Disco.Models.Services.Messaging; +using Disco.Services.Messaging; +using System; + +namespace Disco.Services.Expressions.Extensions +{ + public static class EmailExt + { + public static void SendToUser(User user, string subject, string body) + { + var to = user.EmailAddress; + + if (string.IsNullOrWhiteSpace(to)) + throw new Exception($"User ({user.UserId}) does not have an email address"); + + Send(to, subject, body); + } + + public static void Send(string to, string subject, string body) + { + var email = new Email() + { + Subject = subject, + Body = body, + }; + email.To.Add(to); + + EmailService.SendEmail(email); + } + } +} diff --git a/Disco.Services/Messaging/EmailService.cs b/Disco.Services/Messaging/EmailService.cs index 86e6ff45..ee8e47ae 100644 --- a/Disco.Services/Messaging/EmailService.cs +++ b/Disco.Services/Messaging/EmailService.cs @@ -2,6 +2,7 @@ using Disco.Data.Repository; using Disco.Models.Services.Messaging; using System; +using System.IO; using System.Net; using System.Net.Mail; @@ -98,6 +99,11 @@ namespace Disco.Services.Messaging foreach (var recipient in email.BCC) message.Bcc.Add(recipient); } + if (email.Attachments.Count > 0) + { + foreach (var attachment in email.Attachments) + message.Attachments.Add(new Attachment(new MemoryStream(attachment.Data), attachment.Name, attachment.MediaType)); + } using (var smtpClient = new SmtpClient(smtpServer, smtpPort)) {