Add Image to Email Body using C#

11,364

Solution 1

You need to add it as an embedded resource for the email and create an HTML view for it, here's an example to get you started:

    private static void AddImageToEmail(MailMessage mail, Image image)
    {
        var imageStream = GetImageStream(image);

        var imageResource = new LinkedResource(imageStream, "image/png") { ContentId = "added-image-id" };
        var alternateView = AlternateView.CreateAlternateViewFromString(mail.Body, mail.BodyEncoding, MediaTypeNames.Text.Html);

        alternateView.LinkedResources.Add(imageResource);
        mail.AlternateViews.Add(alternateView);
    }

    private static Stream GetImageStream(Image image)
    {
        // Conver the image to a memory stream and return.
        var imageConverter = new ImageConverter();
        var imgaBytes = (byte[])imageConverter.ConvertTo(image, typeof(byte[]));
        var memoryStream = new MemoryStream(imgaBytes);

        return memoryStream;
    }

Solution 2

You would need to adjust your code to include an inline image.

See the following questions which have already answered this:

Link 1

Link 2

Code snippet (you will need to add something similar to achieve the same result), taken from Link 1;

protected void Page_Load(object sender, EventArgs e)
{
    string Themessage = @"<html>
                      <body>
                        <table width=""100%"">
                        <tr>
                            <td style=""font-style:arial; color:maroon; font-weight:bold"">
                           Hi! <br>
                            <img src=cid:myImageID>
                            </td>
                        </tr>
                        </table>
                        </body>
                        </html>";
    sendHtmlEmail("[email protected]", "tomailaccount", Themessage, "Scoutfoto", "Test HTML Email", "smtp.gmail.com", 25);
}

protected void sendHtmlEmail(string from_Email, string to_Email, string body, string           from_Name, string Subject, string SMTP_IP, Int32 SMTP_Server_Port)
{
    //create an instance of new mail message
    MailMessage mail = new MailMessage();

    //set the HTML format to true
    mail.IsBodyHtml = true;

    //create Alrternative HTML view
    AlternateView htmlView = AlternateView.CreateAlternateViewFromString(body, null, "text/html");

    //Add Image
    LinkedResource theEmailImage = new LinkedResource("E:\\IMG_3332.jpg");
    theEmailImage.ContentId = "myImageID";

    //Add the Image to the Alternate view
    htmlView.LinkedResources.Add(theEmailImage);

    //Add view to the Email Message
    mail.AlternateViews.Add(htmlView);

    //set the "from email" address and specify a friendly 'from' name
    mail.From = new MailAddress(from_Email, from_Name);

    //set the "to" email address
    mail.To.Add(to_Email);

    //set the Email subject
    mail.Subject = Subject;

    //set the SMTP info
    System.Net.NetworkCredential cred = new System.Net.NetworkCredential("[email protected]", "fromEmail password");
    SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
    smtp.EnableSsl = true;
    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
    smtp.UseDefaultCredentials = false;
    smtp.Credentials = cred;
    //send the email
    smtp.Send(mail);
}
Share:
11,364
Obaja Alfonsus Dhani
Author by

Obaja Alfonsus Dhani

Updated on June 04, 2022

Comments

  • Obaja Alfonsus Dhani
    Obaja Alfonsus Dhani almost 2 years

    I want to add an image from picturebox to my email body using C#. This is what I've done. Anyone can help me ?

    var fromAddress = new MailAddress("[email protected]", "aaa");
            var toAddress = new MailAddress("[email protected]", "bbb");
            const string fromPassword = "mypassword";
            const string subject = "Tes Program";
            const string body = "Bersama ini kami kirimkan QR Code sebagai sarana validasi pengiriman rekening koran Anda. Harap simpan dan tunjukkan QR Code ini saat kurir kami datang untuk mengantar rekening koran. Atas perhatiannya kami sampaikan terima kasih. Salam";
    
            var smtp = new SmtpClient
            {
                Host = "smtp.gmail.com",
                Port = 587,
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                UseDefaultCredentials = false,
                Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
            };
    
            using (var message = new MailMessage(fromAddress, toAddress)
            {
    
                Subject = subject,
                Body = body
    
    
            }
    
            )
            {
                smtp.Send(message);
            }
    

    I have picturebox1 and want to add image that shown in picturebox1 to my email body