【问题标题】:Display image inside email message sent from website using c# asp.net使用 c#asp.net 在从网站发送的电子邮件中显示图像
【发布时间】:2015-10-11 20:53:25
【问题描述】:

这是否可以在从用户网站发送的电子邮件消息中显示一张图像?并且此图像存在并在 localhost 中运行。

如果是,那么如何?我尝试过一次但无法显示图像。我正在从我的应用程序将以下 html 模板发送到我的电子邮件。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Reset your password</title>
</head>
<body>
    <img src="http://localhost:3440/img/blue/logo.png" alt="Odiya doctor" /><br />
    <br />
    <div style="border-top: 3px solid #22BCE5">
        &nbsp;</div>
    <span style="font-family: Arial; font-size: 10pt">Hello <b>User</b>,<br />
        <br />
        For reset your password please click on below link<br />
        <br />
        <a style="color: #22BCE5" href="{Url}">Click me to reset your password</a><br />
        <br />
        <br />
        Thanks<br />
        For contacting us.<br />
        <a href="http://localhost:3440/localhost:3440/index.aspx" style="color: Green;">Odiya
            doctor</a> </span>
</body>
</html>

index.aspx.cs:

protected void userPass_Click(object sender, EventArgs e)
{
  string body= this.GetBody("http://localhost:3440/resetPassword.aspx");
  this.sendEmailToUser(respassemail.Text.Trim(), body);
}
private string GetBody(string url)
        {
            string body = string.Empty;
            using (StreamReader reader= new StreamReader(Server.MapPath("~/emailBodyPart.htm")))
            {
                body = reader.ReadToEnd();
            }

            body = body.Replace("{Url}", url);
            return body;
        }
        private void sendEmailToUser(string recepientEmail, string body)
        {
            using (MailMessage mailMessage = new MailMessage())
            {
                try
                {
                    mailMessage.From = new MailAddress(ConfigurationManager.AppSettings["UserName"]);
                    mailMessage.Subject = "Password Reset";
                    mailMessage.Body = body;
                    mailMessage.IsBodyHtml = true;
                    mailMessage.To.Add(new MailAddress(recepientEmail));
                    SmtpClient smtp = new SmtpClient();
                    smtp.Host = ConfigurationManager.AppSettings["Host"];
                    smtp.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableSsl"]);
                    System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
                    NetworkCred.UserName = ConfigurationManager.AppSettings["UserName"];
                    NetworkCred.Password = ConfigurationManager.AppSettings["Password"];
                    smtp.UseDefaultCredentials = true;
                    smtp.Credentials = NetworkCred;
                    smtp.Port = int.Parse(ConfigurationManager.AppSettings["Port"]);
                    smtp.Send(mailMessage);
                    resetText.InnerText = "";
                    resetText.InnerText = "Check your email to reset your password.In case you did not find in inbox of your email please chcek the spam.";
                    resetText.Style.Add("color", "green");
                }
                catch (Exception e)
                {
                    resetText.InnerText = "";
                    resetText.InnerText = "Email sending failed due to :"+e.Message;
                    resetText.Style.Add("color", "red");
                }


            }
        }

当上面的 html 模板发送到电子邮件时,在电子邮件中我无法显示图像。请帮助我。

【问题讨论】:

  • @ Amit :阅读您提供的链接后我感到困惑。我正在再次更新我的代码。您能帮我解决这个问题吗? ?
  • src 值应该是客户端可以解析的值。
  • 你可以再看看我的帖子。
  • 现在我处于无法使用SMTP 发送邮件的环境中。

标签: c# asp.net email


【解决方案1】:

如果图像是通过 localhost 托管的,那么它唯一有效的情况是电子邮件是在单个用户计算机的本地主机上打开的。本地主机无法在另一台机器上解析。 (电脑、手机、平板电脑等)

如果您想让它在其他地方工作,则必须使用可以解析的 URL/IP 公开服务器。

【讨论】:

  • 但是网上图片来了。
  • @satya:在线图片已经保存在服务器上,带有一些特定的 IPURL,因此可以从任何设备看到。
  • @Mitchel :我只想在我的系统中进行测试,所以我现在应该做什么。
猜你喜欢
  • 1970-01-01
  • 2011-12-22
  • 1970-01-01
  • 1970-01-01
  • 2012-07-09
  • 1970-01-01
  • 1970-01-01
  • 2012-09-16
  • 2017-08-13
相关资源
最近更新 更多