开发者

how to insert image into email template

I'm trying to use the PasswordRecovery of ASP.NET.

Everything works fine, however I am using Email template. Within this email I'm tr开发者_如何学运维ying 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.

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

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.


You can use OnSendingMail event to modify your email message. 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:

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

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

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


Have you tried using AlternateView?

One example is here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜