public static void sendEmail(string subject, string tomailid, string title, string bodycontent, string imagepath)
{
try
{
string body = string.Empty;
using (StreamReader reader = new StreamReader(HostingEnvironment.MapPath("~/emailtemplates/email.html")))
{
body = reader.ReadToEnd();
}
body = body.Replace("{Title}", title);
body = body.Replace("{Body}", bodycontent);
string mainbody = body;
using (MailMessage mm = new MailMessage())
{
mm.From = new MailAddress("Email-id");
mm.To.Add(new MailAddress(tomailid));
////var imagepath1= @"~/Images\NoDataAvailable.png";
// var imagepath1= HttpContext.Current.Server.MapPath("~/Images/NoDataAvailable.png");
var stream = new WebClient().OpenRead(imagepath);
//Attachment attachement = new Attachment(stream, "");
var filename = Path.GetFileName(imagepath);
mm.Attachments.Add(new Attachment(stream, "filename"));
mm.Subject = subject;
mm.Body = mainbody;
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential("Emailid", "PAssword");
smtp.UseDefaultCredentials = false;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
}
}
catch { }
}
This is right code