1

I want to send email with image the image is the header of the email body how i attach the image to my body of the email the image is save in my pc local folder / project folder my code is

using(var client = new SmtpClient())
{
    foreach(FeeEntry student in q)
    {
        client.Connect("smtp.gmail.com", 465);
        client.Authenticate("emailaddress", "password");

        var bodyBuilder = new BodyBuilder
        {
            HtmlBody = $ "<img src='** i want to add image here **'/>" +
                $ "<h3 style = 'border:2px solid blue; margin-right: 123px ;text-align: center'>Remaing Fee Notification from(OCMWP) </h3>" +
                $ "<table style = 'border:2px solid blue' border='1'> <thead>  </thead>  <tr>" +
                $ "<th> </th><th>Name</th> <th>Fee Type </th>  <th>Fee Amount</th>  </tr> " +
                $ "<tbody> <tr> <th>1</th> <td>{student.students.StdName}</td> <td> {student.AddFeeInfo.Feetype} </td> <td> {student.AddFeeInfo.FeeAmount} </td><tr> </tbody> </table>  ",
                TextBody = "{formData.ID}\r\n{ formData.subject}\r\n{formData.body}\r\n{formData.Email}"
        };
        var message = new MimeMessage
        {
            Body = bodyBuilder.ToMessageBody()

        };
        message.From.Add(new MailboxAddress("No Reply OCMWP", "bs180201421@vu.edu.pk"));

        message.To.Add(new MailboxAddress(student.students.StdName, student.students.StdEmailAddress));

        message.Subject = "Panding ( OCMWP)";

        client.Send(message);
        client.Disconnect(true);
    }
}

i also try this code but not working please you have another way please guide me

foreach(var attachment in attachments)
{
    mailMessage.Attachments.Add(new Attachment(attachment));
}
ThomasArdal
  • 4,999
  • 4
  • 33
  • 73

2 Answers2

0

Load your image into memory, Base64 encode it and then embed it into the HTMl as a string.

byte[] fileContent = File.ReadAllBytes(fileName);
string base64 = Convert.ToBase64String(fileContent);

Then in your html

 <img src="data:image/png;base64,yourbase64stringgoeshere" alt="description" />

See https://www.w3docs.com/snippets/html/how-to-display-base64-images-in-html.html for more info

daveBM
  • 344
  • 1
  • 10
0

You can do it via AlternateView

var htmlContent = "<img src='{contentId}' />";
byte[] imageFile = GetImageBytes(); //Or you can get FileStream and do not use MemoryStream
var image = new LinkedResource(new MemoryStream(imageFile), MediaTypeNames.Image.Jpeg);
image.ContentId = Guid.NewGuid().ToString();
htmlContent = htmlContent.Replace("{contentId}", image.ContentId);
var alternateView = AlternateView.CreateAlternateViewFromString(htmlContent, Encoding.UTF8, "text/html");
alternateView.LinkedResources.Add(image);

var mailMessage = new MailMessage
{
    From = new MailAddress(senderEmail, senderName, Encoding.UTF8),
    Subject = subject,
    SubjectEncoding = Encoding.UTF8,
    IsBodyHtml = true,
    Body = htmlContent,
    Priority = MailPriority.Normal,
    BodyEncoding = Encoding.UTF8
};

mailMessage.To.Add(emailAddress);
mailMessage.AlternateViews.Add(alternateView);
Andrii Khomiak
  • 565
  • 4
  • 17