From 1a12427a1b50bc10b8486f187854c63232f27cdc Mon Sep 17 00:00:00 2001 From: Amith Date: Thu, 5 Jan 2023 23:05:41 +0000 Subject: [PATCH] Add support for inline images for MailKit --- .../FluentEmail.MailKit/MailKitSender.cs | 14 +++- .../MailKitSmtpSenderTests.cs | 75 +++++++++++++++++++ 2 files changed, 87 insertions(+), 2 deletions(-) diff --git a/src/Senders/FluentEmail.MailKit/MailKitSender.cs b/src/Senders/FluentEmail.MailKit/MailKitSender.cs index 8a5d1413..48ae9dc4 100644 --- a/src/Senders/FluentEmail.MailKit/MailKitSender.cs +++ b/src/Senders/FluentEmail.MailKit/MailKitSender.cs @@ -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; + } }); diff --git a/test/FluentEmail.Core.Tests/MailKitSmtpSenderTests.cs b/test/FluentEmail.Core.Tests/MailKitSmtpSenderTests.cs index 7b80921b..9ece18a8 100644 --- a/test/FluentEmail.Core.Tests/MailKitSmtpSenderTests.cs +++ b/test/FluentEmail.Core.Tests/MailKitSmtpSenderTests.cs @@ -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("Inline image here: " + + "

You should see an image without an attachment, or without a download prompt, depending on the email client.

", 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("Inline image here: " + + "

You should see an image inline without a picture attachment.

" + + "

A single .txt file should also be attached.

", 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() {