使用图像作为电子邮件发送listview或gridview

时间:2013-11-18 09:31:43

标签: c# asp.net email listview smtp

如何将包含图像的ListView发送到邮件。我可以将列表视图作为电子邮件发送,但图像没有显示,请问我如何在Listview中使用嵌入图像或资源链接。图像来自我的列表视图数据库。

源代码。

<asp:ListView ID="ListView1" runat="server" ItemPlaceholderID="PlaceHolder1">
   <ItemTemplate>
    <table>
   <tr><td><asp:Image ID="Image3" runat="server" ImageUrl='<%#"Handler.ashx?ImID="+ Eval("route_Name") %>' Height="125px" Width="250px"/></td>
   <td><asp:Label ID="Label9" runat="server" Text='<%#Eval("Description") %>'></asp:Label></td>
   </tr>
    </table>
    </ItemTemplate>
    </asp:ListView>

SMTP代码。

    StringBuilder sb = new StringBuilder();
    StringWriter sw = new StringWriter(sb);
    HtmlTextWriter htmlTW = new HtmlTextWriter(sw);
    Panel1.RenderControl(htmlTW);
    string panelBody = sb.ToString();

    string fromEmail = lblempemailid.Text;
    string passwrd = lblemppaswrd.Text;
    string toMail = txtto.Text;
    MailMessage message = new MailMessage();

    message.To.Add(toMail);
    message.From = new MailAddress(fromEmail,"Company");
    message.Subject = txtmailsubject.Text;
    message.Body = panelBody;
    message.IsBodyHtml = true;
    message.BodyEncoding = Encoding.UTF8;
    SmtpClient smtp = new SmtpClient();
    smtp.Host = "smtp.gmail.com";
    smtp.Port = 587;
    smtp.Credentials = new System.Net.NetworkCredential(fromEmail, passwrd);
    smtp.EnableSsl = true;
    smtp.Send(message);
    message = null;

1 个答案:

答案 0 :(得分:1)

图像需要是绝对路径,因此不是:

/images/some_image.jpg

应该是

http://site_url/images/some_image.jpg