FluentEmail
FluentEmail copied to clipboard
Unable to bind the inline image into body content using FluentMail.SMTP
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.
@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
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.
I've got the same issue, there's no documentation at all about this implementation.
@WadeTheFade, Its displays Image in Inline?
For me in Gmail, its not working
@xts-velkumars I am running into the same issue did you ever manage to fix this?
So for anyone else looking for the fix. All i needed to do was add this based off the leniel change.
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 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!