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
14 changes: 12 additions & 2 deletions src/Senders/FluentEmail.MailKit/MailKitSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,18 @@ private MimeMessage CreateMailMessage(IFluentEmail email)

data.Attachments.ForEach(x =>
{
var attachment = builder.Attachments.Add(x.Filename, x.Data, ContentType.Parse(x.ContentType));
attachment.ContentId = x.ContentId;
var contentType = ContentType.Parse(x.ContentType);

if (x.IsInline)
{
var attachment = builder.LinkedResources.Add(x.Filename, x.Data, contentType);
attachment.ContentId = x.ContentId ?? x.Filename;
}
else
{
var attachment = builder.Attachments.Add(x.Filename, x.Data, contentType);
attachment.ContentId = x.ContentId;
}
});


Expand Down
75 changes: 75 additions & 0 deletions test/FluentEmail.Core.Tests/MailKitSmtpSenderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,81 @@ public async Task CanSendEmailWithAttachments()
Assert.IsNotEmpty(files);
}

[Test]
[TestCase]
[TestCase("logotest.png")]
public async Task CanSendEmailWithInlineImages(string contentId = null)
{
using (var stream = File.OpenRead($"{Path.Combine(Directory.GetCurrentDirectory(), "logotest.png")}"))
{
var attachment = new Attachment
{
IsInline = true,
Data = stream,
ContentType = "image/png",
Filename = "logotest.png",
ContentId = contentId
};

var email = Email
.From(fromEmail)
.To(toEmail)
.Subject(subject)
.Body("<html>Inline image here: <img src=\"cid:logotest.png\">" +
"<p>You should see an image without an attachment, or without a download prompt, depending on the email client.</p></html>", true)
.Attach(attachment);

var response = await email.SendAsync();

var files = Directory.EnumerateFiles(tempDirectory, "*.eml");
Assert.IsTrue(response.Successful);
Assert.IsNotEmpty(files);
}
}

[Test]
public async Task CanSendEmailWithInlineImagesAndAttachmentTogether()
{
var attachmentStream = new MemoryStream();
var sw = new StreamWriter(attachmentStream);
sw.WriteLine("Hey this is some text in an attachment");
sw.Flush();
attachmentStream.Seek(0, SeekOrigin.Begin);

var attachment = new Attachment
{
Data = attachmentStream,
ContentType = "text/plain",
Filename = "MailKitAttachment.txt",
};

using var inlineStream = File.OpenRead($"{Path.Combine(Directory.GetCurrentDirectory(), "logotest.png")}");

var attachmentInline = new Attachment
{
IsInline = true,
Data = inlineStream,
ContentType = "image/png",
Filename = "logotest.png",
};

var email = Email
.From(fromEmail)
.To(toEmail)
.Subject(subject)
.Body("<html>Inline image here: <img src=\"cid:logotest.png\">" +
"<p>You should see an image inline without a picture attachment.</p>" +
"<p>A single .txt file should also be attached.</p></html>", true)
.Attach(attachment)
.Attach(attachmentInline);

var response = await email.SendAsync();

var files = Directory.EnumerateFiles(tempDirectory, "*.eml");
Assert.IsTrue(response.Successful);
Assert.IsNotEmpty(files);
}

[Test]
public async Task CanSendAsyncHtmlAndPlaintextTogether()
{
Expand Down