I want to be able to send an email with an image in the body. The below code works but I can't figure out how to add image to it. Thanks for any help!
namespace Identity.Areas.Birthdays.Controllers
{
public class EmailController : ApplicationBaseController
{
private EmployeeInfoEntities db = new EmployeeInfoEntities();
public ActionResult SendEmail(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
EmployeeInfo employeeInfo = db.EmployeeInfoes.Find(id);
if (employeeInfo == null)
{
return HttpNotFound();
}
return View(employeeInfo);
}
[HttpPost]
public ActionResult SendEmail(string receiver, string subject, string message, string from)
{
try
{
if (ModelState.IsValid)
{
var senderEmail = new MailAddress("test@gmail.com");
var receiverEmail = new MailAddress(receiver, "Receiver");
var password = "*********";
var sub = subject;
var body = "<font color='red'>" + "<font size='20px'>" + message + "<br />" + "<br />" + "<font color='blue'>" + from + "</font>" + "</font>" + "</font>";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(senderEmail.Address, password)
};
using (var mess = new MailMessage(senderEmail, receiverEmail)
{
Subject = subject,
Body = body
})
{
mess.IsBodyHtml = true;
smtp.Send(mess);
ViewBag.Message = "Message Has Been Sent!";
}
return View();
}
}
catch (Exception)
{
ViewBag.Error = "An Error Has Occurred!";
}
return View();
}
}
}