3

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.

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.

<!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");
                }


            }
        }

When the above html template has sent to email,Inside email message i am unable to display image.Please help me.

satya
  • 3,508
  • 11
  • 50
  • 130

1 Answers1

0

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. 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.

Mitchel Sellers
  • 62,228
  • 14
  • 110
  • 173