【问题标题】:Send Html document(contianing Images) as an email attachment C#将 Html 文档(包含图像)作为电子邮件附件发送 C#
【发布时间】:2010-12-29 03:49:22
【问题描述】:

我正在使用 C# 在运行时将 Html 文档(包含图像)作为电子邮件附件发送。
但是当我检查收到的电子邮件时,发送的 html 文档不包含任何图像。

您能否指导我如何确保 html 文档与图像一起发送。

        MailMessage objMailMessage = new MailMessage();
        objMailMessage.From = new MailAddress("aa@gmail.com");

        string[] emailIds = objReportRequest.EmailIds.Split(',');
        foreach (string emailId in emailIds)
        {
            objMailMessage.To.Add(new MailAddress(emailId));
        }

        objMailMessage.IsBodyHtml = true; 
        objMailMessage.Body = messageBody;
        objMailMessage.Subject = "Test Service";
        objMailMessage.Attachments.Add(new Attachment(filePath));

        SmtpClient objSmtpClient = new SmtpClient("smtp.gmail.com");
        objSmtpClient.Credentials = new NetworkCredential("aaa@gmail.com", "aaa");
        objSmtpClient.EnableSsl = true;
        objSmtpClient.Send(objMailMessage);

我收到了 html 文档作为电子邮件附件,但没有显示图像。

谢谢!

【问题讨论】:

  • 介意向我们展示您尝试使用的代码吗?

标签: c# email attachment


【解决方案1】:
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(message, null, "text/html");
 LinkedResource image = new LinkedResource(@".\images\sample.jpg");
 image.ContentId = "Image";
 message = "<img src=cid:Image>" + message;
 htmlView = AlternateView.CreateAlternateViewFromString(message, null, "text/html");
 htmlView.LinkedResources.Add(image);

您将如何将图像放入 HTML 电子邮件中,根据 MSDN 的 html 附件使用相同的对象,但我没有专门的代码。

【讨论】:

  • 为了让这个工作正常进行,我必须在 LinkedResource("image/png") 的创建中包含 MIME 内容。我使用.Net 4.5
【解决方案2】:

【讨论】:

    【解决方案3】:

    如果 HTML 作为附件发送,我认为您不能在其中嵌入图像。毕竟,HTML 是文本。

    如果图像是静态的,我认为最好的办法是通过绝对路径引用图像,并将图像托管在您的一台服务器上。

    【讨论】:

    • HTML 可以包含指向其他附件的链接,例如图像。用于引用其他附件的“content-id”URL 格式在 RFC 2111 mhonarc.org/~ehood/MIME/rfc2111.txt 中进行了描述
    【解决方案4】:

    由于您将 HTML 作为附件发送而不是作为正文发送,因此没有简单的方法来执行此操作。

    这里有几个选项:

    1. 最好的方法是实际构建一个 MHT(或 MHTML)文档,并将其作为附件发送。

    2. 您可能希望使用 HTML 填充电子邮件正文,然后嵌入图像,而不是将 HTML 作为附件添加。

    3. 另一种选择是简单地构建电子邮件,将图像嵌入该电子邮件中,然后将电子邮件添加为附件。

    4. 如果收件人将使用更新的浏览器,您可以使用数据 URL 方案将图像直接嵌入标签中。 http://en.wikipedia.org/wiki/Data_URI_scheme

    5. 像另一位海报建议的那样,在标签中使用绝对链接,并将图像托管在某处。

    【讨论】:

      猜你喜欢
      • 2013-02-26
      • 2018-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-26
      • 2023-03-23
      • 1970-01-01
      相关资源
      最近更新 更多