是否可以在电子邮件正文中发送aspx页面?

时间:2012-04-05 18:20:50

标签: c# asp.net email c#-4.0 smtp

<div class="popup-holder">
            <div class="popup-frame">
                <div class="container">
                    <div class="mail-block">
                        <div class="mail-holder">
                            <strong class="mail-logo"><a href="#">abc website</a></strong>
                            <div class="mail-page">
                                <div class="mail-page-holder">
                                    <div class="mail-page-frame">
                                        <p>Hello &lt;<asp:Label ID="lblfriend" runat="server" Text="Label"></asp:Label>>!,</p>
                                        <p>Great News!  &lt;One of your FB Friends/<asp:Label ID="lblmyname" runat="server" Text="Label"></asp:Label>>  has invited you to BlissLink.</p>
                                        <p>(<asp:Label ID="lblmyname1" runat="server" Text="Label"></asp:Label>) says:  &lt;<asp:Label
                                            ID="lblmess" runat="server" Text="abc is fun ,"></asp:Label>><br /><br /></p>
                                        <p></p>
                                        <ul class="f-list">
                                            <li>
                                                <asp:Image ID="Image3" runat="server" width="48" height="48" />
                                                <strong class="title"><span><asp:Label ID="Label2" runat="server" ></asp:Label></span></strong>
                                            </li>
                                            <li>
                                                <asp:Image ID="Image4" runat="server" width="48" height="48" />
                                                <strong class="title"><span> 
                                                    <asp:Label ID="Label3" runat="server" ></asp:Label></span></strong>
                                            </li>
                                            <li>
                                                <asp:Image ID="Image5" runat="server" width="48" height="48" />
                                                <strong class="title"><span><asp:Label ID="Label4" runat="server" ></asp:Label></span></strong>
                                            </li>
                                            <li>
                                                <asp:Image ID="Image6" runat="server" width="48" height="48" />
                                                <strong class="title"><span><asp:Label ID="Label5" runat="server" ></asp:Label></span></strong>
                                            </li>
                                            <li>
                                                <asp:Image ID="Image7" runat="server" width="48" height="48" />
                                                <strong class="title"><span> <asp:Label ID="Label6" runat="server"></asp:Label></span></strong>
                                            </li>
                                            <li>
                                                <asp:Image ID="Image8" runat="server" width="48" height="48" />
                                                <strong class="title"><span> <asp:Label ID="Label7" runat="server" ></asp:Label></span></strong>
                                            </li>
                                            <li>
                                                <asp:Image ID="Image9" runat="server" width="48" height="48" />
                                                <strong class="title"><span><asp:Label ID="Label8" runat="server" ></asp:Label></span></strong>
                                            </li>
                                            <li>
                                                <asp:Image ID="Image10" runat="server" width="48" height="48" />
                                                <strong class="title"><span> <asp:Label ID="Label9" runat="server" ></asp:Label></span></strong>
                                            </li>
                                            <li>
                                                <asp:Image ID="Image11" runat="server" width="48" height="48" />
                                                <strong class="title"><span><asp:Label ID="Label10" runat="server" ></asp:Label></span></strong>
                                            </li>
                                            <li>
                                                <asp:Image ID="Image12" runat="server" width="48" height="48" />
                                                <strong class="title"><span><asp:Label ID="Label11" runat="server" ></asp:Label></span></strong>
                                            </li>
                                        </ul>
                                        <asp:Button ID="Button6" runat="server" Text="Expand Your Network"  CssClass="btn" />
                                        <span class="bot-text">Cheers,<br />Your abc Team</span>
                                    </div>
                                </div>
                            </div>
                            <span class="un">If you have no more interest in receiving our news, <a href="#">unsubscribe.</a></span>
                        </div>
                    </div>
                </div>
            </div>
        </div>

这些图像是在运行时决定的,无论如何都将它嵌入到电子邮件正文中

请帮助

3 个答案:

答案 0 :(得分:6)

如果您只想要电子邮件中标记的那一部分,那么将其放在用户控件(myEmailControl.ascx)中。然后,您可以从发送电子邮件的脚本调用控件上的Render(),并拦截控件的html输出。然后,您可以将该HTML放入电子邮件正文中。 (您的图片也需要绝对的网址,以便它们显示在电子邮件中,但我相信您已经考虑过了)

发送电子邮件的页面/处理程序:

    StringBuilder htmlResponse = new StringBuilder();
    using (StringWriter sw = new StringWriter(htmlResponse))
    {
        using (HtmlTextWriter textWriter = new HtmlTextWriter(sw))
        {
            Control emailBody = Page.LoadControl("myEmailControl.ascx");
            emailBody.RenderControl(textWriter);
        }
    }
    string emailHtml = htmlResponse.ToString();

答案 1 :(得分:3)

您可以从页面下载呈现的HTML并发送:

WebRequest request = WebRequest.Create("http://server.com/path/to/page.aspx");
WebResponse response = request.GetResponse();

StringReader reader = new StringReader(response.GetResponseStream());

string html = reader.ReadToEnd();

现在,您只需在电子邮件中发送html的内容即可。

答案 2 :(得分:0)

您最好的选择可能是在HTML中将图像输出为Base 64。

<img src="data:image/png;base64,iVBrkJggg==" alt="Base 64 encoded!" /> 

在您的代码中,获取图像,将其转换为Base 64并将其打印为src!