0

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();
        }


    }
}
Jamie Rees
  • 7,973
  • 2
  • 45
  • 83
Teka
  • 3
  • 2
  • You want add image as attachment? Also you can convert image into base64 and print in body. – daremachine Jul 29 '19 at 13:21
  • The image can also be hosted externally and you can provide a `img` tag with a `src` property with the link – Jamie Rees Jul 29 '19 at 13:22
  • Possible duplicate of [Mvc .Net.Mail: How to send email with image (logo)](https://stackoverflow.com/questions/28178580/mvc-net-mail-how-to-send-email-with-image-logo) – Heretic Monkey Jul 29 '19 at 13:26

2 Answers2

2

You have to put image in your body string.

var body = "<img src='~/images/sample.jpg' /> <font color='red'>" + "<font size='20px'>" + message + "<br />"  + "<br />" + "<font color='blue'>" + from + "</font>" + "</font>" + "</font>";

Make sure the images folder and sample.jpg exists.

Apologies, I realize the above won't work and you will get the broken image in the output because the string won't know that it has to render the image as well.

New solution, even better for cleaner body creation: use this method. https://lastloop.blogspot.com/2019/07/send-email-from-c.html

Akshay
  • 1,412
  • 2
  • 17
  • 51
1

You will add the image tag as we add normally in the html but since you are sending it in the email, the image you want to add, has to be hosted somewhere. For example, like this google logo:

<img srce='https://www.google.com.pk/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png'/>
ArslanIqbal
  • 569
  • 1
  • 6
  • 19