diff --git a/src/Madev.Utils.Infrastructure.Services.Mailing.Mailkit/MailkitEmailSender.cs b/src/Madev.Utils.Infrastructure.Services.Mailing.Mailkit/MailkitEmailSender.cs index 01f996e..c862864 100644 --- a/src/Madev.Utils.Infrastructure.Services.Mailing.Mailkit/MailkitEmailSender.cs +++ b/src/Madev.Utils.Infrastructure.Services.Mailing.Mailkit/MailkitEmailSender.cs @@ -96,18 +96,30 @@ private MimeMessage ConstructMessageBody(MimeMessage message, string htmlBody, s { foreach (var attachment in attachments) { - switch (attachment) + var convertedAttachment = attachment switch { - case FilepathEmailAttachment att: - var filename = Path.GetFileName(att.Path); - var content = File.ReadAllBytes(att.Path); - builder.Attachments.Add(filename, content); - break; - case ByteEmailAttachment att: - builder.Attachments.Add(att.Filename, att.Content); - break; - default: - throw new InvalidOperationException("Unknown attachment type."); + FilepathEmailAttachment att => builder.Attachments.Add( + Path.GetFileName(att.Path), + File.ReadAllBytes(att.Path), + ContentType.Parse(att.ContentType) + ), + ByteEmailAttachment att => builder.Attachments.Add( + att.Filename, + att.Content, + ContentType.Parse(att.ContentType) + ), + Base64EmailAttachment att => builder.Attachments.Add( + att.FileName, + Convert.FromBase64String(att.Content), + ContentType.Parse(att.ContentType) + ), + _ => throw new InvalidOperationException("Unknown attachment type.") + }; + + if (attachment.IsInline) + { + convertedAttachment.ContentId = attachment.ContentId; + convertedAttachment.ContentDisposition = new ContentDisposition(ContentDisposition.Inline); } } } diff --git a/src/Madev.Utils.Infrastructure.Services.Mailing.MsGraph/EmailBuilder.cs b/src/Madev.Utils.Infrastructure.Services.Mailing.MsGraph/EmailBuilder.cs index 686debb..836db9f 100644 --- a/src/Madev.Utils.Infrastructure.Services.Mailing.MsGraph/EmailBuilder.cs +++ b/src/Madev.Utils.Infrastructure.Services.Mailing.MsGraph/EmailBuilder.cs @@ -57,20 +57,36 @@ public EmailBuilder Attachments(IEnumerable attachments) private FileAttachment ConvertToFileAttachment(IEmailAttachment attachment) { - return attachment switch + var convertedAttachment = attachment switch { ByteEmailAttachment att => new FileAttachment { - Name = att.Filename, ContentBytes = att.Content, ContentType = "application/octet-stream" + Name = att.Filename, + ContentBytes = att.Content, + ContentType = attachment.ContentType }, FilepathEmailAttachment att => new FileAttachment { Name = Path.GetFileName(att.Path), ContentBytes = File.ReadAllBytes(att.Path), - ContentType = "application/octet-stream" + ContentType = attachment.ContentType + }, + Base64EmailAttachment att => new FileAttachment + { + Name = att.FileName, + ContentBytes = Convert.FromBase64String(att.Content), + ContentType = attachment.ContentType }, _ => throw new InvalidOperationException("Unknown attachment type.") }; + + if (attachment.IsInline) + { + convertedAttachment.IsInline = true; + convertedAttachment.ContentId = attachment.ContentId; + } + + return convertedAttachment; } } } \ No newline at end of file diff --git a/src/Madev.Utils.Infrastructure.Services.Mailing/Base64EmailAttachment.cs b/src/Madev.Utils.Infrastructure.Services.Mailing/Base64EmailAttachment.cs new file mode 100644 index 0000000..ef9b78f --- /dev/null +++ b/src/Madev.Utils.Infrastructure.Services.Mailing/Base64EmailAttachment.cs @@ -0,0 +1,31 @@ +using System; + +namespace Madev.Utils.Infrastructure.Services.Mailing +{ + public class Base64EmailAttachment : IEmailAttachment + { + public Base64EmailAttachment(string filename, string content, bool isInline = false, string? contentId = null, string? contentType = null) + { + FileName = filename; + Content = content; + IsInline = isInline; + + if (IsInline && contentId == null) + { + throw new ArgumentException("If an attachment is inline, ContentId must be defined"); + } + ContentId = contentId; + + if(contentType != null) + { + ContentType = contentType; + } + } + + public string FileName { get; } + public string Content { get; } + public string? ContentId { get; set; } + public bool IsInline { get; set; } + public string? ContentType { get; set; } = "application/octet-stream"; + } +} \ No newline at end of file diff --git a/src/Madev.Utils.Infrastructure.Services.Mailing/ByteEmailAttachment.cs b/src/Madev.Utils.Infrastructure.Services.Mailing/ByteEmailAttachment.cs index 1095b94..39edf30 100644 --- a/src/Madev.Utils.Infrastructure.Services.Mailing/ByteEmailAttachment.cs +++ b/src/Madev.Utils.Infrastructure.Services.Mailing/ByteEmailAttachment.cs @@ -4,13 +4,28 @@ namespace Madev.Utils.Infrastructure.Services.Mailing { public class ByteEmailAttachment : IEmailAttachment { - public ByteEmailAttachment(string filename, byte[] content) + public ByteEmailAttachment(string filename, byte[] content, bool isInline = false, string? contentId = null, string? contentType = null) { Filename = filename ?? throw new ArgumentException($"{nameof(filename)} cannot be null."); Content = content ?? throw new ArgumentException($"{nameof(content)} cannot be empty."); + IsInline = isInline; + + if(IsInline && contentId == null) + { + throw new ArgumentException("If an attachment is inline, ContentId must be defined"); + } + ContentId = contentId; + + if(contentType != null) + { + ContentType = contentType; + } } public string Filename { get; } public byte[] Content { get; } + public string? ContentId { get; set; } + public bool IsInline { get; set;} + public string? ContentType { get; set; } = "application/octet-stream"; } } \ No newline at end of file diff --git a/src/Madev.Utils.Infrastructure.Services.Mailing/FilepathEmailAttachment.cs b/src/Madev.Utils.Infrastructure.Services.Mailing/FilepathEmailAttachment.cs index 6b72566..2ff3141 100644 --- a/src/Madev.Utils.Infrastructure.Services.Mailing/FilepathEmailAttachment.cs +++ b/src/Madev.Utils.Infrastructure.Services.Mailing/FilepathEmailAttachment.cs @@ -4,11 +4,26 @@ namespace Madev.Utils.Infrastructure.Services.Mailing { public class FilepathEmailAttachment : IEmailAttachment { - public FilepathEmailAttachment(string path) + public FilepathEmailAttachment(string path, bool isInline = false, string? contentId = null, string? contentType = null) { Path = path ?? throw new ArgumentException($"{nameof(path)} cannot be empty."); + IsInline = isInline; + + if (IsInline && contentId == null) + { + throw new ArgumentException("If an attachment is inline, ContentId must be defined"); + } + ContentId = contentId; + + if(contentType != null) + { + ContentType = contentType; + } } public string Path { get; } + public string? ContentId { get; set; } + public bool IsInline { get ; set ; } + public string? ContentType { get ; set ; } = "application/octet-stream"; } } \ No newline at end of file diff --git a/src/Madev.Utils.Infrastructure.Services.Mailing/IEmailAttachment.cs b/src/Madev.Utils.Infrastructure.Services.Mailing/IEmailAttachment.cs index 973acd8..a6343e1 100644 --- a/src/Madev.Utils.Infrastructure.Services.Mailing/IEmailAttachment.cs +++ b/src/Madev.Utils.Infrastructure.Services.Mailing/IEmailAttachment.cs @@ -1,4 +1,8 @@ namespace Madev.Utils.Infrastructure.Services.Mailing { - public interface IEmailAttachment{} + public interface IEmailAttachment{ + public bool IsInline { get; set; } + public string? ContentId { get; set; } + public string? ContentType { get; set; } + } } \ No newline at end of file