You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET发送HTML邮件模板图片无法加载问题求助

问题描述

我有一个ASP.NET Web应用用于发送邮件,用HTML文件模板发邮件能正常发送,但模板里的图片只有最后一张公网图片能加载,本地相对路径(Images/image1.jpg)和localhost路径(http://localhost:1114/WebBody/Images/image2.jpg)的图片都显示不出来。

以下是我的代码:

SendEmail方法代码

public void SendEmail(string EmailAddress)    
{

      try
        {

            MailMessage mail = new MailMessage();
            mail.To.Add(EmailAddress);
            mail.From = new MailAddress("myemail@gmail.com");
            mail.Subject = txtSubject.Text;
            
            string FilePath = Server.MapPath("templateHtml.html");
            StreamReader str = new StreamReader(FilePath);
            string body = str.ReadToEnd();
            //string body = Server.HtmlEncode(str.ReadToEnd());

            mail.IsBodyHtml = true;
            mail.Body = body;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = smtpClient;
            smtp.Credentials = new NetworkCredential(email, password);
            
            smtp.EnableSsl = true;
            smtp.Send(mail);
            str.Close();
            Response.Write("ok!")
        }
    catch
        {                
            Response.Write("so bad");                               
        }

 }

HTML模板代码

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>

<br />
<p>This is an image</p>
<img src="Images/image1.jpg">

<p>This is an image</p>
<img src="http://localhost:1114/WebBody/Images/image2.jpg">

<p>image from net</p>
<img src="https://cdn.mos.cms.futurecdn.net/FVqUjfbiHS9imyJiRiM53-970-80.jpg.webp" width="40%">

</body>
</html>

解决办法

方案一:把本地图片作为嵌入资源发送

邮件客户端没法访问你的本地服务器或相对路径的图片,因为这些资源只在你的服务器本地能用。可以把图片作为邮件的嵌入资源,用ContentID关联到HTML里。

修改后的SendEmail方法:

public void SendEmail(string EmailAddress)    
{
    try
    {
        MailMessage mail = new MailMessage();
        mail.To.Add(EmailAddress);
        mail.From = new MailAddress("myemail@gmail.com");
        mail.Subject = txtSubject.Text;
        
        string FilePath = Server.MapPath("templateHtml.html");
        StreamReader str = new StreamReader(FilePath);
        string body = str.ReadToEnd();
        str.Close();

        // 处理第一张本地图片
        string image1Path = Server.MapPath("Images/image1.jpg");
        LinkedResource image1 = new LinkedResource(image1Path, "image/jpeg");
        image1.ContentId = "image1Id"; // 自定义唯一ID
        body = body.Replace("Images/image1.jpg", "cid:image1Id"); // 替换HTML中的图片路径

        // 处理第二张localhost图片
        string image2Path = Server.MapPath("WebBody/Images/image2.jpg");
        LinkedResource image2 = new LinkedResource(image2Path, "image/jpeg");
        image2.ContentId = "image2Id";
        body = body.Replace("http://localhost:1114/WebBody/Images/image2.jpg", "cid:image2Id");

        // 创建HTML视图并添加嵌入资源
        AlternateView htmlView = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
        htmlView.LinkedResources.Add(image1);
        htmlView.LinkedResources.Add(image2);
        mail.AlternateViews.Add(htmlView);

        mail.IsBodyHtml = true;
        mail.Body = body;

        SmtpClient smtp = new SmtpClient();
        smtp.Host = smtpClient;
        smtp.Credentials = new NetworkCredential(email, password);
        smtp.EnableSsl = true;
        smtp.Send(mail);

        Response.Write("ok!");
    }
    catch
    {                
        Response.Write("so bad");                               
    }
}

方案二:把本地图片放到公网可访问地址

把图片上传到云存储或者你的公网服务器,然后把HTML里的图片src改成公网能访问的URL,比如https://你的域名/Images/image1.jpg,这样邮件客户端就能直接加载图片了。

为什么本地路径和localhost路径不行?

  • 相对路径Images/image1.jpg:邮件客户端不知道这个路径的基准地址,找不到你的服务器资源。
  • localhost路径:这个地址只有你本地机器能访问,收件人在其他网络环境下连不上你的本地服务器。

内容的提问来源于stack exchange,提问作者sandro vieira

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 10:30:53