Display image inside email message sent from website using c # asp.net
Is it possible to display a single image inside an email message that is sent from a user's website? and this image is present and run on localhost.
If so, how? I tried once but couldn't display the image. I am posting below html template to my email from my application.
<!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">
</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 is emailed, inside the email I cannot display the image. Please help me.
+3
source to share
1 answer
If the image is hosted via localhost, the only time it will work will be if the email is opened on the local host of a separate user machine. The local host cannot be resolved on another machine. (Computer, phone, tablet, etc.)
If you wanted it to work elsewhere, the server would have to open the URL / IP, which could be resolved.
0
source to share