1

I am trying to send an email which has embedded images in the body of the message.

The images are encoded as Base64 string. The contents looks as follows:

"<p><span>My Name</span></p><img src=\"data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEA..." width=\"167\" height=\"167\" />"

The base64 string has been cut off for this example.

I am using the following code to send the html as attachment, However, the image in the example is still been refused to be decoded by the emails providers (gmail/outlook). I do not know if i am not doing it properly.

    string htmlBody = "<p><span>My Name</span></p><img src=\"data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEA..." width=\"167\" height=\"167\" />";

   AlternateView avHtml = AlternateView.CreateAlternateViewFromString
               (htmlBody, null, MediaTypeNames.Text.Html);


  var mailTo = Config.Debug ? new MailAddress(Config.DebugEmailAddress) : new MailAddress("myemail@gmail.com");
  var mailFrom = new MailAddress("myemail@gmail.com");
  var mailMessage = new MailMessage(mailFrom, mailTo) { Subject = "hola q tal", Body = "holaaa", IsBodyHtml = true };

  mailMessage.To.Add("myemail@gmail.com");

  mailMessage.AlternateViews.Add(avHtml);

 var sender1 = new SmtpClient
     {
         Host = Config.SmtpHost,
         Port = 25,
         Credentials = new NetworkCredential(Config.SmtpHostUserName, Config.SmtpHostPassword)
            };

      sender1.Send(mailMessage);

I would appreciate any sugesstions.

D.B
  • 4,009
  • 14
  • 46
  • 83

0 Answers0