【问题标题】:Embed image to email message in System.Web.Mail.MailMessage将图像嵌入到 System.Web.Mail.MailMessage 中的电子邮件消息
【发布时间】:2016-05-16 07:34:29
【问题描述】:

我想使用 System.Web.Mail.MailMessage 和 System.Web.Mail.SmtpMail 类发送电子邮件。 我知道这是一个已弃用的类,但使用 System.Net.Mail 对我来说不是一个选项。

到目前为止,我能够发送 html 格式的邮件,但我无法在发送时将图像嵌入到我的电子邮件中

这是我迄今为止尝试过的;

private static void SendMailMethod(string mailServer, int mailServerPort, string userName, string password)
    {
        const string SMTP_SERVER = "http://schemas.microsoft.com/cdo/configuration/smtpserver";
        const string SMTP_SERVER_PORT =
            "http://schemas.microsoft.com/cdo/configuration/smtpserverport";

        const string SEND_USING = "http://schemas.microsoft.com/cdo/configuration/sendusing";

        const string SMTP_USE_SSL = "http://schemas.microsoft.com/cdo/configuration/smtpusessl";


        const string SMTP_AUTHENTICATE =
           "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate";


        const string SEND_USERNAME =
            "http://schemas.microsoft.com/cdo/configuration/sendusername";
        const string SEND_PASSWORD =
            "http://schemas.microsoft.com/cdo/configuration/sendpassword";


        var mailMessage = new System.Web.Mail.MailMessage();

        mailMessage.Fields[SMTP_SERVER] = mailServer;
        mailMessage.Fields[SMTP_SERVER_PORT] = mailServerPort;
        mailMessage.Fields[SEND_USING] = 2;
        mailMessage.Fields[SMTP_USE_SSL] = false;
        mailMessage.Fields[SMTP_AUTHENTICATE] = 0;
        mailMessage.Fields[SEND_USERNAME] = userName;
        mailMessage.Fields[SEND_PASSWORD] = password;

        mailMessage.From = "abc@xyz.com";
        mailMessage.To = "abc@xyz.com";
        mailMessage.Subject = "Test mail:";
        mailMessage.BodyFormat = MailFormat.Html;

        var mailAttachment = new MailAttachment("E:\\imageToEmbed.jpg");
        mailMessage.Attachments.Add(mailAttachment);
        mailMessage.Attachments.Add(new MailAttachment("E:\\TestAttachmentFile.txt"));

        var htmlBody =
            "<!DOCTYPE html PUBLIC \" -//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">" +
            "<html xmlns = \"http://www.w3.org/1999/xhtml\" >" +
            " <head >" +
            "<meta http - equiv = \"content-type\" content = \"text/html; charset=UTF-8\" />" +
            "</head >" +
            "<body style = \"font-family: Segoe UI; text-align:left;\" >" +
            "Following is an embedded image:" +
            "<br />" +
            "<img alt = \"\" src = \"imageToEmbed.jpg\" />" +
            "</body >" +
            "</html >";

        mailMessage.Body = htmlBody;

        try
        {
            SmtpMail.Send(mailMessage);

            Console.WriteLine("Mail sent");
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error:" + ex.ToString());
            throw ex;
        }
    }

此代码发送电子邮件,但不是显示图像,而是显示带有十字的小方块。 如何将此“imageToEmbed.jpg”嵌入到我的电子邮件中。

【问题讨论】:

  • @photowalker,您分享的链接中的答案对我不起作用。因为我没有使用 System.Net.Mail 名称空间。我正在使用 System.Web.Mail.MailMessage 类发送电子邮件
  • 为什么你被限制在那个过时的类?您在使用 .Net 1.1 吗?
  • 我仅限于这个过时的类,因为 System.Net.Mail 中的新类不允许我使用 SSL 安全性发送邮件。我已经问过这个here

标签: c# email


【解决方案1】:

您无法使用类 System.Web.Mail 实现这一点,因为该实现抽象了对底层 Collaboration Data Objects component 的许多功能的访问。

与其使用System.Web.Mail.Message 提供的包装器,不如直接使用CDO COM 对象。在引用对话框的 COM 选项卡中添加对 Microsoft CDO for Windows 2000 Library 的引用。

之后,您可以使用以下代码创建并发送带有嵌入图像的 html 电子邮件:

var cdo = new CDO.Message();
// configuration
var cfg = cdo.Configuration;
cfg.Fields[SMTP_SERVER].Value = "smtp.server.com";
cfg.Fields[SMTP_SERVER_PORT].Value = 22;
cfg.Fields[SEND_USING].Value = 2;
cfg.Fields[SMTP_USE_SSL].Value = true;
cfg.Fields[SMTP_AUTHENTICATE].Value = 1;
cfg.Fields[SEND_USERNAME].Value = "user@example.com";
cfg.Fields[SEND_PASSWORD].Value = "password";
cfg.Fields.Update();

cdo.To = "awesome@example.com";
cdo.Sender = "me@example.com";

// attachment
var cdoatt = cdo.AddAttachment("file:///E:/imageToEmbed.jpg");
//this is why System.Web.Mail can't embed images
cdoatt.Fields["urn:schemas:mailheader:content-id"].Value = Guid.NewGuid().ToString("N");
cdoatt.Fields.Update();

// get a reference to out content-id field
var cid = cdoatt.Fields["urn:schemas:mailheader:content-id"];
// notice the special layout of SRC on the image tag, 
//it will be somethong like CID:123456789abcdef
cdo.HTMLBody = @"<HTML><BODY><B>CDO</B><BR /> <IMG SRC=""cid:" + cid.Value + @"""/></BODY></HTML>";

cdo.Send();

请注意如何在附件中设置 Content-ID 标头。这是必需的,因此您可以在图像的 SRC 属性中使用 Content-ID。

一封典型的邮件看起来像

MIME-Version: 1.0
Content-Type: multipart/mixed;
    boundary="----=_NextPart_000_0001_01D1AF72.C8AE8C70"
Content-Transfer-Encoding: 7bit
X-Mailer: Microsoft CDO for Windows 2000
Content-Class: urn:content-classes:message
Importance: normal
Priority: normal
X-MimeOLE: Produced By Microsoft MimeOLE V10.0.10011.16384

This is a multi-part message in MIME format.

------=_NextPart_000_0001_01D1AF72.C8AE8C70
Content-Type: multipart/alternative;
    boundary="----=_NextPart_001_0002_01D1AF72.C8AE8C70"


------=_NextPart_001_0002_01D1AF72.C8AE8C70
Content-Type: text/plain;
    charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

plain cdo 

------=_NextPart_001_0002_01D1AF72.C8AE8C70
Content-Type: text/html;
    charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<HTML><BODY><B>plain cdo</B> <IMG SRC="cid:42"/></BODY></HTML>
------=_NextPart_001_0002_01D1AF72.C8AE8C70--

------=_NextPart_000_0001_01D1AF72.C8AE8C70
Content-Type: image/png;
    name="file.png"
Content-Transfer-Encoding: base64
Content-ID: <42>
Content-Disposition: attachment;
    filename="file.png"

iVBORw0KGgoAAAANSUhEUgAAADQAAABLCAMA

注意 Content-ID(此处为 42)。

当使用普通的 System.Web.Mail 类时,您的原始电子邮件将如下所示:

MIME-Version: 1.0
Content-Type: multipart/mixed;
    boundary="----=_NextPart_000_0000_01D1AF70.59FC25F0"
X-Mailer: Microsoft CDO for Windows 2000
Content-Class: urn:content-classes:message
Importance: normal
Priority: normal
X-MimeOLE: Produced By Microsoft MimeOLE V10.0.10011.16384

This is a multi-part message in MIME format.

------=_NextPart_000_0000_01D1AF70.59FC25F0
Content-Type: multipart/alternative;
    boundary="----=_NextPart_001_0001_01D1AF70.59FC25F0"


------=_NextPart_001_0001_01D1AF70.59FC25F0
Content-Type: text/plain;
    charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Following is an embedded image:

  _____  

test afterit

------=_NextPart_001_0001_01D1AF70.59FC25F0
Content-Type: text/html;
    charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<!DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns = "http://www.w3.org/1999/xhtml" > <head ><meta http - equiv = "content-type" content = "text/html; charset=UTF-8" /></head ><body style = "font-family: Segoe UI; text-align:left;" ><i>Following is an embedded image:</i><br /><img alt = "" src ="<file:///file.png>"/><hr><b>test afterit</b></body ></html >
------=_NextPart_001_0001_01D1AF70.59FC25F0--

------=_NextPart_000_0000_01D1AF70.59FC25F0
Content-Type: image/png;
    name="file.png"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
    filename="file.png"

iVBORw0KGgoAAAANSUhEUgAAADQAAABLCAMAAAAI0l

如您所见,没有添加 Content-ID 标头,也没有来自 .Net 实现的代码路径来添加该标头。

【讨论】:

  • 感谢您如此详细的回复。我已尝试使用您的解决方案,但电子邮件仍未显示图像。 This 是我现在的原始电子邮件的样子。
  • @krw12572 嗯,奇怪,您的内容看起来不错,即使使用您的标记,我也会得到一个嵌入的图像。顺便说一句,我正在使用 gmail 进行测试。
  • 谢谢!代码正在运行。之前可能是与电子邮件服务器有关的问题。它现在与 gmail 合作。
【解决方案2】:

它可能在指定的路径中找不到图像,或者它可能是一个许可的东西,因此损坏的图像是什么。您需要先检查一下,但如果您使用 base64,您将把图像放在一个字符串中。

尝试将图像更改为 base64 link to a free online converter page.

此链接也可能会有所帮助c# embed base64 image for emailing.

【讨论】:

    猜你喜欢
    • 2022-09-25
    • 2011-10-30
    • 2021-09-19
    • 2011-07-12
    • 2012-03-30
    • 2016-11-06
    • 2011-10-06
    相关资源
    最近更新 更多