FluentEmail icon indicating copy to clipboard operation
FluentEmail copied to clipboard

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

Open PratikSoni2889 opened this issue 6 years ago • 9 comments

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("[email protected]", "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("[email protected]")
                    .To("[email protected]")
                    .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.

PratikSoni2889 avatar May 17 '18 09:05 PratikSoni2889

@dapug @bjcull : Can you guide me that is there any approach that I can set inline image using "IsInline" with FluentEmail.SMTP? If not, please add approach with ContentId - if possible - in FluentEmail.SMTP / FluentEmail.Core.

Thanks in advance.

Ref: #75

PratikSoni2889 avatar May 22 '18 09:05 PratikSoni2889

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.

WadeTheFade avatar Aug 27 '18 20:08 WadeTheFade

I've got the same issue, there's no documentation at all about this implementation.

alextof avatar Sep 19 '18 08:09 alextof

@WadeTheFade, Its displays Image in Inline?

For me in Gmail, its not working

image

xts-velkumars avatar Oct 08 '21 14:10 xts-velkumars

@xts-velkumars I am running into the same issue did you ever manage to fix this?

Reier360 avatar Nov 17 '21 07:11 Reier360

So for anyone else looking for the fix. All i needed to do was add this based off the leniel change. image

Reier360 avatar Nov 17 '21 08:11 Reier360

I've found that Papercut SMTP won't show my images but Outlook will... However when I manually edit the .eml file and change the content type from

Content-Type: multipart/mixed; to Content-Type: multipart/related;

The images work fine in papercut smtp.

Maybe this is a bug with Papercut, but after pulling my hair out for nearly a day, this could help someone else who's having problems debugging why their images are not showing.

Also, maybe fluentemail could change the Content-Type when using inline attachments, or let us change the Content-Type on our own?

zimmer62 avatar Feb 15 '22 20:02 zimmer62

@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!

VictorioBerra avatar Mar 04 '22 23:03 VictorioBerra