Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/Senders/FluentEmail.Smtp/SmtpSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using FluentEmail.Core.Interfaces;
using FluentEmail.Core.Models;
using System;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading;
Expand Down Expand Up @@ -77,6 +78,7 @@ private MailMessage CreateMailMessage(IFluentEmail email)
{
var data = email.Data;
MailMessage message = null;
AlternateView htmlView = null;

// Smtp seems to require the HTML version as the alternative.
if (!string.IsNullOrEmpty(data.PlaintextAlternativeBody))
Expand All @@ -92,6 +94,22 @@ private MailMessage CreateMailMessage(IFluentEmail email)
var mimeType = new System.Net.Mime.ContentType("text/html; charset=UTF-8");
AlternateView alternate = AlternateView.CreateAlternateViewFromString(data.Body, mimeType);
message.AlternateViews.Add(alternate);
htmlView = alternate;
}
else if (data.IsHtml && data.Attachments.Any(IsLinkedResource))
{
// Inline images can only be embedded through an AlternateView's
// LinkedResources, so the HTML body goes into one.
message = new MailMessage
{
Subject = data.Subject,
SubjectEncoding = Encoding.UTF8,
From = new MailAddress(data.FromAddress.EmailAddress, data.FromAddress.Name)
};

var mimeType = new System.Net.Mime.ContentType("text/html; charset=UTF-8");
htmlView = AlternateView.CreateAlternateViewFromString(data.Body, mimeType);
message.AlternateViews.Add(htmlView);
}
else
{
Expand Down Expand Up @@ -146,6 +164,22 @@ private MailMessage CreateMailMessage(IFluentEmail email)

data.Attachments.ForEach(x =>
{
// An inline attachment referenced from the HTML (<img src="cid:...">)
// becomes a LinkedResource of the HTML view, which produces
// multipart/related. Added as a regular attachment it would be sent
// as multipart/mixed with "Content-Disposition: attachment", and
// mail clients show it as a separate attachment.
if (htmlView != null && IsLinkedResource(x))
{
var resource = x.ContentType == null
? new LinkedResource(x.Data)
: new LinkedResource(x.Data, x.ContentType);
resource.ContentId = x.ContentId;
resource.ContentType.Name = x.Filename;
htmlView.LinkedResources.Add(resource);
return;
}

System.Net.Mail.Attachment a = new System.Net.Mail.Attachment(x.Data, x.Filename, x.ContentType);

a.ContentId = x.ContentId;
Expand All @@ -155,6 +189,9 @@ private MailMessage CreateMailMessage(IFluentEmail email)

return message;
}

private static bool IsLinkedResource(FluentEmail.Core.Models.Attachment attachment) =>
attachment.IsInline && !string.IsNullOrEmpty(attachment.ContentId);
}

// Taken from https://stackoverflow.com/questions/28333396/smtpclient-sendmailasync-causes-deadlock-when-throwing-a-specific-exception/28445791#28445791
Expand Down
82 changes: 82 additions & 0 deletions test/FluentEmail.Core.Tests/SmtpSenderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,88 @@ public void CanSendHtmlAndPlaintextTogether()
(response.Successful).Should().BeTrue();
}

private static Attachment InlineImage() => new()
{
Data = new MemoryStream([0x89, 0x50, 0x4E, 0x47]),
ContentType = "image/png",
Filename = "logo.png",
ContentId = "logo",
IsInline = true
};

private static string ReadSentEmail(string tempDirectory) =>
File.ReadAllText(Directory.EnumerateFiles(tempDirectory, "*.eml").Single());

[Fact]
public async Task InlineAttachmentIsEmbeddedInHtmlBody()
{
var email = TestEmail
.Body("<p><img src=\"cid:logo\"></p>", true)
.Attach(InlineImage());

email.Sender = GetSender(out var s);

var response = await email.SendAsync();
var eml = ReadSentEmail(s);

DeleteTemp(s);

(response.Successful).Should().BeTrue();
eml.Should().Contain("Content-Type: multipart/related");
eml.Should().Contain("Content-ID: <logo>");
eml.Should().NotContain("Content-Disposition: attachment");
}

[Fact]
public async Task InlineAttachmentIsEmbeddedInHtmlAlternateView()
{
var email = TestEmail
.Body("<p><img src=\"cid:logo\"></p>", true)
.PlaintextAlternativeBody("Plain text version")
.Attach(InlineImage());

email.Sender = GetSender(out var s);

var response = await email.SendAsync();
var eml = ReadSentEmail(s);

DeleteTemp(s);

(response.Successful).Should().BeTrue();
eml.Should().Contain("Content-Type: multipart/alternative");
eml.Should().Contain("Content-Type: multipart/related");
eml.Should().Contain("Content-ID: <logo>");
eml.Should().NotContain("Content-Disposition: attachment");
}

[Fact]
public async Task RegularAttachmentStaysAttachmentNextToInlineAttachment()
{
var email = TestEmail
.Body("<p><img src=\"cid:logo\"></p>", true)
.Attach(InlineImage())
.Attach(new Attachment
{
Data = new MemoryStream("some text"u8.ToArray()),
ContentType = "text/plain",
Filename = "report.txt"
});

email.Sender = GetSender(out var s);

var response = await email.SendAsync();
var eml = ReadSentEmail(s);

DeleteTemp(s);

(response.Successful).Should().BeTrue();
eml.Should().Contain("Content-Type: multipart/mixed");
eml.Should().Contain("Content-Type: multipart/related");
eml.Should().Contain("Content-ID: <logo>");
eml.Should().Contain("Content-Type: text/plain; name=report.txt");
eml.Split("Content-Disposition: attachment").Length.Should().Be(2, "only report.txt is a regular attachment");
}

[Fact]
public void CancelSendIfCancellationRequested()
{
Expand Down