Add support for email attachments in messaging service
Introduced the `EmailAttachmentContext` class to represent email attachments, with properties for file name, content, content type, inline display behavior, and content ID. Used conditional compilation to support both .NET Framework and .NET versions. Updated the `EmailContext` class to include an `Attachments` property, enabling emails to include attachments as byte arrays or file paths. Enhanced the `LimilabsEmailService` class to handle attachments: - Added the `AddAttachments` method to process inline and regular attachments. - Integrated attachment handling into the email-building process.
This commit is contained in:
@@ -46,6 +46,8 @@ public class LimilabsEmailService(
|
||||
else
|
||||
builder.Text = context.Body;
|
||||
|
||||
AddAttachments(builder, context.Attachments);
|
||||
|
||||
var mail = builder.Create();
|
||||
|
||||
result = await smtp.SendMessageAsync(mail, cancellationToken);
|
||||
@@ -108,4 +110,33 @@ public class LimilabsEmailService(
|
||||
|
||||
return message.ToString();
|
||||
}
|
||||
|
||||
private static void AddAttachments(MailBuilder builder, IEnumerable<EmailAttachmentContext> attachments)
|
||||
{
|
||||
foreach (var attachment in attachments)
|
||||
{
|
||||
if (attachment.IsInline)
|
||||
{
|
||||
var visual = builder.AddVisual(attachment.Content);
|
||||
visual.FileName = attachment.FileName;
|
||||
visual.ContentId = string.IsNullOrWhiteSpace(attachment.ContentId)
|
||||
? attachment.FileName
|
||||
: attachment.ContentId;
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(attachment.ContentType))
|
||||
visual.ContentType = ContentType.Parse(attachment.ContentType);
|
||||
}
|
||||
else
|
||||
{
|
||||
var part = builder.AddAttachment(attachment.Content);
|
||||
part.FileName = attachment.FileName;
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(attachment.ContentType))
|
||||
part.ContentType = ContentType.Parse(attachment.ContentType);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(attachment.ContentId))
|
||||
part.ContentId = attachment.ContentId;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user