Skip to content

Commit

Permalink
Fixing #39
Browse files Browse the repository at this point in the history
  • Loading branch information
cmendible committed Mar 29, 2024
1 parent d379a13 commit b58a620
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
33 changes: 31 additions & 2 deletions netDumbster/MailMessageExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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();
}
}
4 changes: 2 additions & 2 deletions netDumbster/SmtpMessagePart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ namespace netDumbster.smtp;
/// </summary>
public class SmtpMessagePart
{
public SmtpMessagePart(string header, string body)
public SmtpMessagePart(string headers, string body)
{
HeaderData = header;
HeaderData = headers;
BodyData = body;
}

Expand Down

0 comments on commit b58a620

Please sign in to comment.