简体   繁体   English

如何将图像插入电子邮件模板

[英]how to insert image into email template

I'm trying to use the PasswordRecovery of ASP.NET. 我正在尝试使用ASP.NET的PasswordRecovery。

Everything works fine, however I am using Email template. 一切正常,但我使用电子邮件模板。 Within this email I'm trying to insert an image as follows: 在这封电子邮件中,我正在尝试插入如下图像:

<html xmlns="http://www.w3.org/1999/xhtml">

<body>

<img alt="blabla" src="/Images/blabla-logo.png" align="middle"/><br/><br/>
bla bla:<%Password%><br /><br />
</body>

</html>

As I said, the email is being sent fine but the image is not inserted. 正如我所说,电子邮件正在发送,但没有插入图像。 I tried: src="~/Images/blabla-logo.png", but with no success. 我试过:src =“〜/ Images / blabla-logo.png”,但没有成功。

Idea anyone? 有谁的想法?

Thanks a lot, Assaf. 非常感谢,阿萨夫。

For email you should not give relative path like "/Images/blabla-logo.png" the only works for the internal website pages, instead of this you should give the complete path like 对于电子邮件,你不应该给像“/Images/blabla-logo.png”这样的相对路径是内部网站页面的唯一作品,而不是你应该给出完整的路径

http://youserver/youapp/Images/blabla-logo.png HTTP://youserver/youapp/Images/blabla-logo.png

I will suggest you not to include image using the path instead of this try embedding the image in your email. 我建议您不要使用路径包含图像,而不是尝试在您的电子邮件中嵌入图像。 You can achieve this by converting your images to base64 string and set the base64 string as the source of the image. 您可以通过将图像转换为base64字符串并将base64字符串设置为图像源来实现此目的。

You can use OnSendingMail event to modify your email message. 您可以使用OnSendingMail事件来修改您的电子邮件。 Let's assume your template look like this: 我们假设您的模板如下所示:

<html xmlns="http://www.w3.org/1999/xhtml">
  <body>
    <img alt="blabla" src="{0}" align="middle"/><br/><br/> 
    bla bla:<%Password%><br /><br /> 
  </body>
</html>

You PasswordRecovery markup should look like this: 您的PasswordRecovery标记应如下所示:

<asp:PasswordRecovery ID="prPasswordRecovery" runat="server" OnSendingMail="prPasswordRecovery_SendingMail">
  <MailDefinition BodyFileName="~/passwordRecoveryEmailTemplate.txt" IsBodyHtml="true" Priority="High" Subject="bla bla"/>
</asp:PasswordRecovery>

Last thing to do is to write prPasswordRecovery_SendingMail method in code behind: 最后要做的是在代码后面编写prPasswordRecovery_SendingMail方法:

protected void prPasswordRecovery_SendingMail(object sender, MailMessageEventArgs e)
{
  e.Message.Body = String.Format(e.Message.Body, ResolveClientUrl("~/Images/blabla-logo.png"));
}

That should do it. 应该这样做。

try adding a tilde "~", an id and runat="server". 尝试添加代字号“〜”,id和runat =“server”。 The tilde only gets changed to the root path when runat="server" is applied. 当应用runat =“server”时,波形符只会更改为根路径。 Otherwise, the serverside code has no knowledge of the control and doesn't parse it and apply the path insertion 否则,服务器端代码不知道控件,也不解析它并应用路径插入

 <img alt="blabla" src="~/Images/blabla-logo.png" 
 align="middle" id="img" runat="server"/>

Have you tried using AlternateView? 你尝试过使用AlternateView吗?

One example is here . 这里有一个例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM