【问题标题】:I'd like to embed an image in my emails我想在我的电子邮件中嵌入一张图片
【发布时间】:2018-11-01 07:46:13
【问题描述】:

我一直在尝试许多不同的东西,但我似乎无法弄清楚如何在我的代码中嵌入图像。我的计划是在字符串正文中发送带有图像的邮件。图像保存在内容文件夹中。

mm.Subject = "CompanyName ";
string body = "Dear " + CustomerMasterObj.CustomerName + ", \n";
                    body += "\n";
                    body += "\n";    
                    body += EmailTemplateObj.EmailMsg;
                    body += "\n";
                    body += Userurl;
                    body += "\n";
                    body += "Thank you in advance. \n";
                    body += "\n";
                    body += " Yours sincerely, \n";
                    body += "\n";
                    body += "John Doe \n";
                    mm.Body = body;    
                    mm.IsBodyHtml = false;

【问题讨论】:

    标签: c# css image email embed


    【解决方案1】:

    您可以使用 HTML 添加 <img> 标签,但这意味着将您的图像转换为 base64 或托管它:

    这里有一些伪代码来实现:

    MailMessage mm = new MailMessage
       {
         IsBodyHtml = true,
         Subject = "CompanyName",
       };
    string body = "HTML OPENING TAG .... YOUR CONTENT";
    body += "<img src=\"data:image/png;base64," + Convert.ToBase64String(File.ReadAllBytes("path")) + "\" alt=\"myimage\" />";
    body += "MORE HTML + HTML CLOSING TAG";
    mm.Body = body;
    

    【讨论】:

      【解决方案2】:

      尝试以下是否使用了 LinkedResource 对象,因为在某些情况下,在没有 LinkedResource 的移动电子邮件应用程序上,消息的接收者不会出现图像。

      using System;
      using System.IO;
      using System.Net.Mail;
      
      namespace PayneInMyButt
      {
          public class EmailImage
          {
              public void DemoForImage()
              {
                  using (MailMessage mail = new MailMessage())
                  {
                      mail.From = new MailAddress("TODO");
                      mail.To.Add("TODO");
                      mail.Subject = "This is an email";
      
                      AlternateView PlainMessage = AlternateView.CreateAlternateViewFromString("Hello, plain text", null, 
                          "text/plain");
      
                      AlternateView HtmlMessage = AlternateView.CreateAlternateViewFromString(
                          "This is an automated email, please do not respond<br><br>This is a test <br>" + 
                          "<span style=\"font-weight: bold; padding-left: 20px;padding-right:5px\">Some bold text</span>" + 
                          "Non bold<br><span style=\"font-weight: bold; padding-left: 5px;padding-right:5px\">Second line</span>" + 
                          "This is Kate below<br><span style=\"font-weight: bold; padding-left: 70px;padding-right:5px\">" + 
                          "<img src=cid:Image1>", null, "text/html");
      
                      LinkedResource Logo = new LinkedResource(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, 
                          "LogoImage.jpg"), 
                          "image/jpeg");
      
                      Logo.ContentId = "Image1";
      
                      mail.AlternateViews.Add(PlainMessage);
                      mail.AlternateViews.Add(HtmlMessage);
                      using (SmtpClient smtp = new SmtpClient("TODO"))
                      {
                          smtp.Send(mail);
                      }
                  }
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2011-12-07
        • 1970-01-01
        • 2016-08-23
        • 1970-01-01
        • 2023-03-22
        • 2015-11-22
        • 2014-05-15
        • 1970-01-01
        • 2018-03-15
        相关资源
        最近更新 更多