question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to bind the inline image into body content using FluentMail.SMTP

See original GitHub issue

Hi,

I’m trying to bind an embedded image to the mail body content.

Below is my code:

private void SendEmailWithInlineImages_FluentEmail()
        {
            try
            {
                //var assembly = Assembly.GetExecutingAssembly();
                //var stream = assembly.GetManifestResourceStream(@"C:\Users\a.b\Downloads\FluentLogo.png");
                //stream.Flush();

                //stream.Seek(0, SeekOrigin.Begin);

                //Setup Default sender befault sending the email.
                System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient
                {
                    Host = "smtp.office365.com",
                    Port = 587,
                    EnableSsl = true,
                    Credentials = new System.Net.NetworkCredential("a.b@gmail.com", "12345")
                };
                Email.DefaultSender = new SmtpSender(smtpClient);

                Email.DefaultRenderer = new RazorRenderer();
                string imagePath = @"C:\Users\pratik.soni\Downloads\FluentLogo.png";

                var attachment = new Core.Models.Attachment()
                {
                    IsInline = true,
                    Data = new FileStream(imagePath, FileMode.Open, FileAccess.Read),
                    ContentType = "image/png",
                    Filename = "FluentLogo.png"
                };

                var email = Email
                    .From("a.b@yahoo.com")
                    .To("a.b@gmai.com")
                    .Subject("Test with embadded image")
                    .Body("<html>Inline image here: <img src=\"cid:FluentLogo.png\">" +
                    "<p>You should see an image without an attachment, or without a download prompt, dependig on the email client.</p></html>", true)
                    .Attach(attachment);

                email.Send();
            }
            catch (Exception ex)
            {
                throw;
            }
        }

But with this code, it is getting adding this image as an attachment rather than setting it up as an inline embedded image.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:2
  • Comments:8

github_iconTop GitHub Comments

4reactions
WadeTheFadecommented, Aug 27, 2018

I am able to send inline an image using CID and SMTP. I did it in the following manner (found how to do it from the Unit Tests in the PR for Mailgun):

Not sure this is the best way, but it does it. Outlook does show an attachment icon, but it doesn’t force you to download images before showing it.

var stream = ass.GetManifestResourceStream("NAMESPACE.TO.EMBEDDED.RESOURCE.logo.png");
            stream.Flush();
            stream.Seek(0, SeekOrigin.Begin);
            var attachment = new Attachment()
            {
                IsInline = true,
                Data = stream,
                ContentType = "image/png",
                Filename = "logo.png"
            };
_emailFactory.Create()
                    .To(u.EmailAddress)
                    .Subject(subject)
                    .Body(htmlBody, true)
                    .Attach(attachment)
                    .PlaintextAlternativeBody(textBody));

and in my Razor file I just have: <img src="cid:logo.png">

My only issue is I couldn’t really find any documentation on any of this.

0reactions
VictorioBerracommented, Mar 4, 2022

@zimmer62 Thanks for posting this. You are right, multipart/related in the eml shows up in Papercut SMTP now.

Is this an issue with Papercut, or FluentEmail? Why and when should we use multipart/related over multipart/mixed?

Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to send embedded image in email using FluentEmail
I'm using FluentEmail in ASP.NET core 2.0 class library which will be sending the Email notification. Below is the sample code I have...
Read more >
Send HTML Emails with Attachments using FluentEmail, C# ...
Send HTML emails with images and buttons using SMTP and your free Gmail ... NET 5.0 sample app 3:01​ - Adding FluentEmail Packages...
Read more >
Send Email with Embedded Images in C#
To attach embedded images/pictures, SmtpMail.ImportHtmlBody and SmtpMail.ImportHtml methods are strongly recommended. With these methods, you don't have to ...
Read more >
Send email in ASP.NET C# [2023 Tutorial with Code ...
NET C# using SMTP server? How to send emails in ASP.NET C# using an API? How to send an HTML email in ASP.NET...
Read more >
Send Emails with ASP.NET Core in 5 EASY Steps - Guide
Step 1 – Create a New ASP.NET Core Project · Step 2 – Add the Required Mail Models · Step 3 – Configure...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found