sending e-mail with base64 image using a page result

246 views Asked by At

I want to send an image of a page output (html + css) as an e-mail. I wrote the code below for this, e-mail is sent but the image cannot be displayed in body. where am I doing wrong? can you please help?

public ActionResult Index()
{
    ViewBag.Title = "Home Page";
    MailMessage ms = new MailMessage("********@outlook.com", "******@outlook.com");
    ms.Subject = "Hello";

    var webClient = new WebClient();
    byte[] imageBytes = webClient.DownloadData("https://www.google.com/");

    ms.Body = @"<img src=""data:image/png;base64," + Convert.ToBase64String(imageBytes, 0, imageBytes.Length) + @""" alt =""Google"" width=""500"" height=""600"">";

    ms.IsBodyHtml = true;

    SmtpClient smtp = new SmtpClient("smtp.office365.com", 587);
    smtp.EnableSsl = true;
    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

    System.Net.NetworkCredential nc = new System.Net.NetworkCredential("*****@outlook.com", "********");
    smtp.UseDefaultCredentials = false;
    smtp.Credentials = nc;

    smtp.Send(ms);
    return View();
}
0

There are 0 answers