将图像嵌入到System.Web.Mail.MailMessage中的电子邮件中 [英] Embed image to email message in System.Web.Mail.MailMessage

查看:189
本文介绍了将图像嵌入到System.Web.Mail.MailMessage中的电子邮件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用System.Web.Mail.MailMessage和System.Web.Mail.SmtpMail类一起发送电子邮件。
我知道这是一个不推荐的类,但使用System.Net.Mail不是我的选择。

I want to send email using System.Web.Mail.MailMessage along with System.Web.Mail.SmtpMail class. I know that this is a deprecated class but using System.Net.Mail is not an option for me.

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

So far I am able to send html formatted mail but I am not able to embed image to my email while sending.

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

This is what I've tried so far;

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嵌入到我的电子邮件中。

This code sends email but instead of showing image, the mail shows small square with a cross in it. How can I embed this "imageToEmbed.jpg" to my email.

推荐答案

您无法使用类 System.Web.Mail 因为该实现将访问许多基本的协作数据对象组件

You can't achieve that with the classes System.Web.Mail because that implementation abstracts the access to a lot of features of the underlying Collaboration Data Objects component.

而不是使用由 System.Web.Mail.Message提供的包装器你最好直接使用CDO COM对象。在参考对话框的COM选项卡中添加对 Microsoft CDO for Windows 2000 Library 的引用。

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

After that you can use the following code to create and send an html email with embedded images:

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。

Notice how you can set the Content-ID header here on the Attachment. That is needed so you can use the Content-ID in the SRC attribute of your image.

典型的邮件消息将显示

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)。

Notice the Content-ID (here 42).

使用简单的 System.Web.Mail 将您的原始邮件将如下所示:

When using the plain System.Web.Mail classes your raw email will look like this:

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

As您可以看到,没有添加Content-ID标头,并且没有.Net实现的代码路径来添加该标题。

As you can see, there is no Content-ID header added and there is no code-path from the .Net implementation to add that header.

这篇关于将图像嵌入到System.Web.Mail.MailMessage中的电子邮件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆