【问题标题】:Unable to send embedded image in email using FluentEmail无法使用 FluentEmail 在电子邮件中发送嵌入图像
【发布时间】:2018-10-27 08:18:00
【问题描述】:

我在 ASP.NET core 2.0 类库中使用FluentEmail,它将发送电子邮件通知。

下面是我尝试过的示例代码:

using FluentEmail.Core;
using FluentEmail.Razor;
using FluentEmail.Smtp;
using System;
using System.IO;
using System.Net.Mail;
using System.Net.Mime;

namespace FluentEmail
{
    public class EmailNotification : IEmailNotification
    {
        public bool SendEmailNotification()
        {
            try
            {
                //Setup Default sender befault sending the email.
                SmtpClient smtpClient = new SmtpClient
                {
                   Host = "smtp.office365.com",
                   Port = 587,
                   EnableSsl = true,
                   Credentials = new System.Net.NetworkCredential("username", "Password")
                };
                Email.DefaultSender = new SmtpSender(smtpClient);

                Email.DefaultRenderer = new RazorRenderer();

                string imagePath = @"C:\Users\pratik.soni\Downloads\FluentLogo.png";

                Stream stream = new FileStream(imagePath, FileMode.Open, FileAccess.Read);
                stream.Flush();
                stream.Seek(0, SeekOrigin.Begin);

                var attachment = new Core.Models.Attachment()
                {
                    Data = stream,
                    ContentType = "image/png",
                    Filename = "FluentLogo.png",
                    IsInline = true
                };

                var email = Email
                  .From("pratik.soni@1rivet.com")
                  .To("pratik.soni10@gmail.com")
                  .Subject("Test")
                  .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);

                email.Attach(attachment);
                email.Send();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }

        }
    }
}

我的 HTML 文件如下:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>FluentEmail - Test with template and attachment</title>
</head>
<body style="margin: 0; padding: 0; background-color: #e5e5e5;" marginheight="0" topmargin="0" marginwidth="0" leftmargin="0">
<!--100% body table-->
<table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
        <td bgcolor="#d8e7ea" style="background-color: #e5e5e5;">
            <!--intro-->
            <table width="620" border="0" align="center" cellpadding="0" cellspacing="0">
                <tr>
                    <td valign="middle" width="11" height="100"></td>
                    <td valign="middle" height="100">
                        <!--break-->
                        <table width="100%" border="0" cellspacing="0" cellpadding="0">
                            <tr>
                                <td height="25"></td>
                            </tr>
                        </table>
                        <!--/break-->
                        <table width="100%" border="0" cellspacing="0" cellpadding="0">
                            <tr>
                                <td width="59%" height="100">
                                    <table width="280" border="0" cellspacing="0" cellpadding="20">
                                        <tr>
                                            <td bgcolor="#333333">
                                                <h1 style="font-family: Arial, Helvetica, sans-serif; font-size: 42px; margin: 0; padding: 0; color: #fff;">Typographic</h1>
                                                <p style="text-transform: uppercase; font-size: 14px;  color: #fff; margin: 0; padding: 0; font-family: Arial, Helvetica, sans-serif;">
                                                    <currentdayname>
                                                    <currentday>
                                                    <currentmonthname>
                                                    <currentyear>
                                                </p>
                                            </td>
                                        </tr>
                                    </table>
                                </td>
                                <td width="41%" height="100" valign="top">
                                    <table width="100%" border="0" cellspacing="0" cellpadding="0">
                                        <tr>
                                            <td valign="bottom" height="70">
                                                <p style="font-family: Arial, Helvetica, sans-serif; font-size: 14px; color: #333; text-transform: uppercase; margin: 0; padding: 0;"> Email not looking beautiful?<br>
                                                    <webversion style="text-decoration: none; color: #cc0000">View it in your browser</webversion>
                                                </p>
                                            </td>
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                        </table>
                        <!--break-->
                        <table width="100%" border="0" cellspacing="0" cellpadding="0">
                            <tr>
                                <td height="25"></td>
                            </tr>
                        </table>
                        <!--/break-->
                    </td>
                </tr>
            </table>
            <!--/intro-->
            <!--content section-->
            <table width="620" border="0" align="center" cellpadding="0" cellspacing="0">
                <tr>
                    <td height="82" width="11" valign="middle"></td>
                    <td height="82" bgcolor="#FFFFFF" valign="middle">
                        <table width="100%" border="0" cellspacing="0" cellpadding="0">
                            <tr>
                                <td valign="middle" height="37" bgcolor="#fef041">
                                    <table width="100%" border="0" cellspacing="0" cellpadding="0">
                                        <tr>
                                            <td bgcolor="#ffffff" width="25" height="37"></td>
                                            <td width="15" height="37"></td>
                                            <td>
                                                <h2 style="color: #333333 !important; font-size: 21px; font-family: Arial, Helvetica, sans-serif; margin: 0; padding: 0; text-shadow: 1px 1px 1px #fff;"> @Model.Title</h2>
                                            </td>
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
                <tr>
                    <td valign="top"></td>
                    <td bgcolor="#FFFFFF" valign="top">
                        <table width="560" border="0" align="center" cellpadding="0" cellspacing="0">
                            <tr>
                                <td valign="top">
                                    <p style="font-size: 14px; color: #333333; margin: 0; padding: 0; font-family: Arial, Helvetica, sans-serif;"> Dear @Model.Name, You are totally @Model.Compliment. </p>
                                    <br>
                                    <table bgcolor="#000000" width="540" border="0" cellspacing="10" cellpadding="0">
                                        <tr>
											<td>
												<img src=\"cid:@Model.ImgSrc\">
											</td>
										</tr>
										<tr>
                                            <td width="540" height="158"><img style="margin: 0; padding: 0; display: block;" border="0" src="C:\Users\pratik.soni\Downloads\2016-fluent-logo-design-digital-marketing-20nine-4.png" width="540" height="158" alt="img1"></td>
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
</table>
<!--/100% body table-->
</body>
</html>

我也尝试过只将图像路径传递给 html 中的 imgsrc,但这也不起作用。

请让我知道我在这里缺少什么。

另外,有没有像 FluentEmail 这样的包可以为 ASP.NET Core 2.0 提供所有功能?

【问题讨论】:

  • 即使能够将图像嵌入到电子邮件中,对渲染的支持也非常不完整。活动监视器有一篇文章here,其中显示了每个客户端以及它是否正确呈现嵌入的图像。
  • 是的,这是正确的,但如果客户想要一些东西,我们必须提供,对吗? :) 但是,谢谢@Syfer 分享信息。
  • 您可以提供,但在提供之前,您必须告诉他们他们是否坚持并且只想要嵌入图像,然后他们可以告别使用不受支持的电子邮件客户端的用户,因为他们不会看到图像(s)。作为电子邮件开发人员,我们的工作是教育他们并告诉他们哪些有效,哪些无效(你知道我知道)。

标签: email html-email asp.net-core-2.0 fluent-interface


【解决方案1】:

您必须使用 LinkedResource;看看这个

using (LinkedResource image = new LinkedResource(@"c:\assets\image.jpg", "image/jpeg") { ContentId = "myimage" })
using (MailMessage mail = new MailMessage())
using (SmtpClient smtpClient = new SmtpClient())
{
    smtpClient.Host = "smtp.alfki.com";

    String body = @"
    <html>
        <head></head>
        <body>    
        <img src=""cid:myimage"" />      
        </body>
    </html>
    ";  

    AlternateView view = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
    view.LinkedResources.Add(image);

    mail.IsBodyHtml = true;
    mail.AlternateViews.Add(view);
    mail.From = new MailAddress("John.Doe@alfki.com");
    mail.To.Add("Jane.Doe@alfki.com");
    mail.Subject = "An email with an inline image";

    smtpClient.Send(mail);
}

编辑:

它适用于 .NET Core/Standard。

我不能代表 FluentMail;也许你可以不用?

【讨论】:

  • 是的,我知道不用 FluentEmail 也能做到。但是这里的问题是它必须使用 FluentEmail 来完成,否则,我已经准备好使用其他方法的示例代码了。
  • 好的,但我读到“有没有像...这样的包”,因此决定让你知道它可以不用。有什么好的理由坚持使用 FluentMail?
  • 客户要求!没办法。他们希望它“流利”。无论如何,我找到了其他一些流畅的选项,即 EmaFluent。我面临的唯一问题不是全部都支持 Asp.Net Core 2.0 的所有功能。虽然它为我提供了我想要的足够选择。问题是它会在包裹上显示一条小的警告消息。
  • 如果可以选择商业图书馆;查看支持 .NET Core 2.0+ 和 .NET Standard 2.0+ 的 Limilabs Mail.dll。有一个流利的例子可以嵌入图片here
  • 广告不是选项。我也试过了。 :) 即使在免费包中,我也建议 MailKit / MimeKit 允许所有设施免费(据我所知)。
猜你喜欢
  • 2020-09-21
  • 2011-04-12
  • 2013-08-23
  • 1970-01-01
  • 1970-01-01
  • 2012-04-07
相关资源
最近更新 更多