How send email with embedded image in base64 for Gmail?

Jair Onofre 1 Reputation point
2021-11-01T20:16:29.4+00:00

if i send image in base64, i received this in email:

145574-image.png

I try to send a base64 image how embedded image, but gmail doesn't accept it, does anyone know if there is another way I can do this? (Outlook Accept)

Developer technologies C#
{count} votes

1 answer

Sort by: Most helpful
  1. Jack J Jun 25,296 Reputation points
    2021-11-02T05:35:58.27+00:00

    @Jair Onofre , as others said, there is no need to encode it in Base64 If you want to send email with embedded image by using c#.

    Here is a code example you could refer to.

    static void Main(string[] args)  
            {  
                SendMessageWithEmbeddedImages();  
            }  
            public static void SendMessageWithEmbeddedImages()  
            {  
                string htmlMessage = @"<html>  
                             <body>  
                             <img src='cid:EmbeddedContent_1' />  
                             </body>  
                             </html>";  
                SmtpClient client = new SmtpClient("smtp.gmail.com");  
                client.Port = 587;  
                client.Credentials = new NetworkCredential("******@gmail.com", "Password");  
                client.EnableSsl = true;  
                MailMessage msg = new MailMessage("******@gmail.com",  
                                                  "******@hotmail.com");  
                // Create the HTML view  
                AlternateView htmlView = AlternateView.CreateAlternateViewFromString(  
                                                             htmlMessage,  
                                                             Encoding.UTF8,  
                                                             MediaTypeNames.Text.Html);  
                // Create a plain text message for client that don't support HTML  
      
                string mediaType = MediaTypeNames.Image.Jpeg;  
                LinkedResource img = new LinkedResource(@"D:\2.jpg", mediaType);  
                // Make sure you set all these values!!!  
                img.ContentId = "EmbeddedContent_1";  
                img.ContentType.MediaType = mediaType;  
                img.TransferEncoding = TransferEncoding.Base64;  
                img.ContentType.Name = img.ContentId;  
                img.ContentLink = new Uri("cid:" + img.ContentId);  
                htmlView.LinkedResources.Add(img);  
                msg.AlternateViews.Add(htmlView);  
                msg.IsBodyHtml = true;  
                msg.Subject = "Some subject";  
                client.Send(msg);  
            }  
    

    Based on my test, the above method can send the email with embedded image successfully.

    Please note that we need to enable Less secure app access in the link: Enable Less secure app.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.