asp.net 发送提醒电子邮件的日常程序未显示嵌入式图像[重复]

qq24tv8q  于 12个月前  发布在  .NET
关注(0)|答案(1)|浏览(138)

此问题在此处已有答案

Send inline image in email(13个回答)
Add Image to Email Body using C#(2个答案)
Adding Image to System.Net.Mail Message(5个回答)
上个月关门了。
我有一个程序去关闭每天发送提醒电子邮件的人注册的事件.一切工作,除了图像一直显示为破碎.我使用C# .NET核心.下面是html.
文件结构为:

  • 服务
  • 电子邮件
  • ReminderEmail.cs
  • wwwroot
  • img
  • email-logo.png
string body = @"
<html>
    <body>
        <img src='wwwroot/img/email-logo.png' width=300 height=100 />
        <p style='color: black;'>Just a reminder, the program you are scheduled to attend is starting soon, the details for your group are below. We look forward to working with you.</p>
            
        <p style='color: black;'><strong>FULL HOCKEY EQUIPMENT</strong>, including your hockey stick, is required. Please arrive approximately 30 minutes prior to the program on the first day so we can get you checked in, and you have time to get dressed. Please bring a water bottle with you and set it on the bench during the session.</p>
            
        <p style='color: black;'>If you are interested in our <strong>testing system</strong> or <strong>video analysis</strong>, please see the instructor at your program.<br />    
        <a href='https://prostrideskating.com/ProgramEvents/Testing'>Learn More About Testing System</a><br />
        <a href='https://prostrideskating.com/VideoAnalysis'>Learn More About Video Analysis</a></p>

            
        <p><mark><strong>Program</strong><br />
        Set Name: {programSetName}<br />
        Event Start: {formattedStartDate}<br />
        Event End: {formattedEndDate}<br />
        Details: {details}</mark></p>
         
        <p><mark><strong>Location</strong><br />
        {locationName}<br />
        {address}<br />
        {city}, {state} {postalCode}<br />
        {country}</mark></p>
            
        <p style='color: black;'>If for any reason you cannot attend, please refer to our <a href='https://www.prostrideskating.com/Terms'>cancellation policy</a> and contact us prior to the start of the program.</p

        <hr />

        <p style='color: black;'>Pro Stride Elite Skating<br />
        <a href='www.prostrideskating.com'>www.prostrideskating.com</a><br />
        Office: 508-406-7552<br />
        Email: <a href='mailto:[email protected]'>[email protected]</a></p>
    </body>
</html>";"

字符串
下面是发送电子邮件的代码:

var mail = new MimeMessage();
                mail.From.Add(new MailboxAddress("name", username));
                mail.To.Add(new MailboxAddress(email, email));
                //mail.Cc.Add(new MailboxAddress("name", "[email protected]"));
                mail.Subject = "Subject"
                mail.Body = new TextPart("html")
                {
                    Text = body
                };


我测试了路径,以确保它是正确的,它看起来是正确的。我尝试将它转换为base64字符串,如下所示:

string imagePath = "wwwroot/img/email-logo.png";

// Read the content of the image file
byte[] imageBytes = System.IO.File.ReadAllBytes(imagePath);

// Convert the byte array to a base64 string
string base64String = Convert.ToBase64String(imageBytes);

//html code
<html>
  <body>
    <img src='data:image/png;base64,base64String' width=100 height=30 />
... rest of code


电子邮件显示base64字符串而不是图像,这也不正确。

9jyewag0

9jyewag01#

你应该在电子邮件中发送图像作为外部路径而不是项目文档根。在本地html中,你可能会发现项目正在运行的路径,但对于外部网络,图像应该托管在外部URL上,你需要在img src中传递该URL

<img src='wwwroot/img/email-logo.png' width=300 height=100 />

字符串

<img src='http://yourdomain.com/img/email-logo.png' width=300 height=100 />


这应该工作
还要求

mail.IsBodyHtml = true;


在你的C#代码中发送邮件,以HTML格式显示图像。

相关问题