使用c#asp.net显示从网站发送的电子邮件内的图像

时间:2015-07-22 06:21:49

标签: c# asp.net email

是否可以在用户网站发送的电子邮件中显示一张图片?并且此图像存在并在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>
&#13;
&#13;
&#13;

  

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模板发送到电子邮件时,内部电子邮件我无法显示图片。请帮助我。

1 个答案:

答案 0 :(得分:0)

如果图像是通过localhost托管的,那么唯一可行的方法是在个人用户计算机的本地主机上打开电子邮件。无法在其他计算机上解析本地主机。 (电脑,手机,平板电脑等)

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