From b58a6200dcdb931a34f50769fa2b08722c33bcb4 Mon Sep 17 00:00:00 2001 From: cmendible <266546+cmendible@users.noreply.github.com> Date: Fri, 29 Mar 2024 12:01:00 +0100 Subject: [PATCH] Fixing #39 --- netDumbster/MailMessageExtensions.cs | 33 ++++++++++++++++++++++++++-- netDumbster/SmtpMessagePart.cs | 4 ++-- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/netDumbster/MailMessageExtensions.cs b/netDumbster/MailMessageExtensions.cs index 60166b6..e4fd951 100644 --- a/netDumbster/MailMessageExtensions.cs +++ b/netDumbster/MailMessageExtensions.cs @@ -17,13 +17,15 @@ public static SmtpMessagePart[] Parts(this MailMessage mailMessage) foreach (AlternateView alternateView in mailMessage.AlternateViews) { - var part = new SmtpMessagePart(alternateView.ContentType.ToString(), StreamToString(alternateView.ContentStream)); + var headers = GetAttachmentHeaders(alternateView); + var part = new SmtpMessagePart(headers, StreamToString(alternateView.ContentStream)); parts.Add(part); } foreach (Attachment attachment in mailMessage.Attachments) { - var part = new SmtpMessagePart(attachment.ContentType.ToString(), StreamToString(attachment.ContentStream)); + var headers = GetAttachmentHeaders(attachment); + var part = new SmtpMessagePart(headers, StreamToString(attachment.ContentStream)); parts.Add(part); } @@ -35,4 +37,31 @@ private static string StreamToString(Stream stream) var reader = new StreamReader(stream); return reader.ReadToEnd(); } + + private static string GetAttachmentHeaders(AttachmentBase attachmentBase) + { + var headers = new StringBuilder(); + if (!string.IsNullOrEmpty(attachmentBase.ContentType.ToString())) + { + headers.AppendLine($"Content-Type: {attachmentBase.ContentType.ToString()}"); + } + if (!string.IsNullOrEmpty(attachmentBase.TransferEncoding.ToString())) + { + headers.AppendLine($"Content-Transfer-Encoding: {attachmentBase.TransferEncoding.ToString()}"); + } + if (!string.IsNullOrEmpty(attachmentBase.ContentId.ToString())) + { + headers.AppendLine($"Content-ID: {attachmentBase.ContentId.ToString()}"); + } + + if (attachmentBase is Attachment attachment) + { + if (!string.IsNullOrEmpty(attachment.ContentDisposition.ToString())) + { + headers.AppendLine("Content-Disposition: " + attachment.ContentDisposition.ToString()); + } + } + + return headers.ToString(); + } } diff --git a/netDumbster/SmtpMessagePart.cs b/netDumbster/SmtpMessagePart.cs index 60f907e..667daa8 100644 --- a/netDumbster/SmtpMessagePart.cs +++ b/netDumbster/SmtpMessagePart.cs @@ -9,9 +9,9 @@ namespace netDumbster.smtp; /// public class SmtpMessagePart { - public SmtpMessagePart(string header, string body) + public SmtpMessagePart(string headers, string body) { - HeaderData = header; + HeaderData = headers; BodyData = body; }