24 lines
963 B
C#
24 lines
963 B
C#
using System.Net.Mail;
|
|
using System.Reflection;
|
|
using NejCommon.Emails;
|
|
|
|
namespace NejCommon.Services.Email;
|
|
|
|
public interface IEmailService
|
|
{
|
|
Task SendEmailAsync(string recipient, string subject, string message, List<Attachment>? attachments = null);
|
|
|
|
async Task SendDocumentAsync<T, T2>(string recipient, T2 data, List<Attachment>? attachments = null) where T : EmailBase<T, T2>
|
|
{
|
|
var func = typeof(T).GetMethod("GetHTML", BindingFlags.Static | BindingFlags.FlattenHierarchy | BindingFlags.Public);
|
|
var obj = Activator.CreateInstance<T>();
|
|
obj.Data = data;
|
|
var subject = obj.GetSubject();
|
|
var atts = obj.GetAttachments();
|
|
|
|
//invoke the GetHTML function asynchronously and wait for the result
|
|
var html = await (Task<string>)func.Invoke(null, new object[] { data });
|
|
|
|
await SendEmailAsync(recipient, subject, html, attachments == null ? atts : atts.Concat(attachments).ToList());
|
|
}
|
|
} |