简体   繁体   English

如何从 SmtpClient() asp.net 核心 MVC 中的文件夹中发送带有图像(作为 email 的主体)的 email

[英]how to send email with image (as body of the email ) attached form the folder in SmtpClient() asp.net core MVC

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 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));
                    }

Load your image into memory, Base64 encode it and then embed it into the HTMl as a string.将图像加载到 memory 中,Base64 对其进行编码,然后将其作为字符串嵌入到 HTMl 中。

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

Then in your html然后在你的 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有关详细信息,请参阅https://www.w3docs.com/snippets/html/how-to-display-base64-images-in-html.html

You can do it via AlternateView您可以通过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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM