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

时间:2023-02-11 21:23:55

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.

非常感谢,阿萨夫。

4 个解决方案

#1


3  

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字符串设置为图像源来实现此目的。

#2


0  

You can use OnSendingMail event to modify your email message. Let's assume your template look like this:

您可以使用OnSendingMail事件来修改电子邮件。我们假设您的模板如下所示:

<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.

应该这样做。

#3


0  

try adding a tilde "~", an id and runat="server". The tilde only gets changed to the root path when runat="server" is applied. Otherwise, the serverside code has no knowledge of the control and doesn't parse it and apply the path insertion

尝试添加代字号“〜”,id和runat =“server”。当应用runat =“server”时,波形符只会更改为根路径。否则,服务器端代码不知道控件,也不解析它并应用路径插入

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

#4


0  

Have you tried using AlternateView?

你尝试过使用AlternateView吗?

One example is here.

这里有一个例子。

#1


3  

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字符串设置为图像源来实现此目的。

#2


0  

You can use OnSendingMail event to modify your email message. Let's assume your template look like this:

您可以使用OnSendingMail事件来修改电子邮件。我们假设您的模板如下所示:

<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.

应该这样做。

#3


0  

try adding a tilde "~", an id and runat="server". The tilde only gets changed to the root path when runat="server" is applied. Otherwise, the serverside code has no knowledge of the control and doesn't parse it and apply the path insertion

尝试添加代字号“〜”,id和runat =“server”。当应用runat =“server”时,波形符只会更改为根路径。否则,服务器端代码不知道控件,也不解析它并应用路径插入

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

#4


0  

Have you tried using AlternateView?

你尝试过使用AlternateView吗?

One example is here.

这里有一个例子。