简体   繁体   English

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

[英]Display image inside email message sent from website using c# asp.net

Is this possible to display one image inside email message which is sent from user's website ? 这是否可以在用户网站发送的电子邮件中显示一个图像? and this image is present and run in localhost. 此图像存在并在localhost中运行。

If yes ,then how ?I have tried once but unable to display the image.I am sending the below html template to my email from my app. 如果是,那怎么样?我曾尝试过但无法显示图像。我将以下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: 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");
                }


            }
        }

When the above html template has sent to email,Inside email message i am unable to display image.Please help me. 当上面的html模板发送到电子邮件时,内部电子邮件消息我无法显示图像。请帮助我。

If the image is hosted via localhost, the only time it will work is if the email is opened on the local host of the individual user's machine. 如果图像是通过localhost托管的,那么唯一可行的方法是在个人用户的计算机的本地主机上打开电子邮件。 Local host is not able to be resolved on a different machine. 无法在其他计算机上解析本地主机。 (Computer, Phone, Tablet, etc.) (电脑,手机,平板电脑等)

If you wanted to make it work elsewhere, the server would have to be exposed with a URL/IP that could be resolved. 如果您想让它在其他地方工作,则必须使用可以解析的URL / IP公开服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM