1

我有一个问题要发送带有 html 和附件作为嵌入图像的邮件。我不知道如何将图像文件夹中的图片用作 div 的背景。

这是我的代码:

SmtpMail oMail = new SmtpMail("TryIt");
            SmtpClient oSmtp = new SmtpClient();

            oMail.From = "test@emailarchitect.net";

            oMail.To = "<my email>";

            oMail.Subject = "test";

            SmtpServer oServer = new SmtpServer("<smtp server>");

            try
            {
               // Attachment header = oMail.AddAttachment("d:\\mail_header.jpg");
                Attachment header = oMail.AddAttachment("images/mail_header.jpg"); // this don't work
                Attachment oAttachment = oMail.AddAttachment("d:\\bg_content.jpg");
                Attachment Footer = oMail.AddAttachment("d:\\mail_footer.jpg");

                string contentID_header = "header";
                header.ContentID = contentID_header; 

                string contentID = "test001@host";
                oAttachment.ContentID = contentID;

                string contentID_footer = "footer";
                Footer.ContentID = contentID_footer; 

//how I can use a pic as background

                oMail.HtmlBody = "<html><body>"+
                                    "<div style='background-image:url(" + contentID_header + ");width: 800px;height: 50px'></div>" +
                                    "<div><img src=\"cid:" + contentID + "\"></div>" +
                                    "<div><img src=\"cid:" + contentID_footer + "\"></div>" +
                                    "</body></html>";

                oSmtp.SendMail(oServer, oMail); 

            }
            catch (Exception ep)
            {
                txtSimulate.Text = ep.Message; 
            } 
4

4 回答 4

3

我使用此代码,使用LinkedResourcesAlternateviews效果很好。比硬编码 CIDS 好多了。

核实:

System.Net.Mail.MailMessage Mensaje = new System.Net.Mail.MailMessage("mail@host.com",DireccionCorreo);

System.Net.Mail.AlternateView htmlView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(Body, null, "text/html");


System.Net.Mail.LinkedResource logo = new System.Net.Mail.LinkedResource("logoimage.jpg");
logo.ContentId = "logoimage";
htmlView.LinkedResources.Add(logo);

System.Net.Mail.LinkedResource logoExchange = new System.Net.Mail.LinkedResource("logoexchange.png");
logoExchange.ContentId = "logoexchange";
htmlView.LinkedResources.Add(logoExchange);

System.Net.Mail.LinkedResource tut1 = new System.Net.Mail.LinkedResource(Application.StartupPath + "/OutlookGuide/tut1.jpg");
tut1 .ContentId = "tut1";
htmlView.LinkedResources.Add(tut1 );

System.Net.Mail.LinkedResource tut2 = new   System.Net.Mail.LinkedResource(Application.StartupPath + "/OutlookGuide/tut2.jpg");
tut2.ContentId = "tut2";
htmlView.LinkedResources.Add(tut2);


Mensaje.AlternateViews.Add(htmlView);
于 2012-12-05T12:17:39.670 回答
1

我不知道 asp.net 但使用 PHP 我们通过将图像转换为 Base64 编码然后使用

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIA..." />
于 2012-12-05T12:14:27.653 回答
1

您必须提供图像的完整路径。

于 2012-12-05T12:16:52.477 回答
1

首先,我找到了这个项目

但基本上,将图像“嵌入”到电子邮件本身中,您需要将其添加为链接资源并在电子邮件的 HTML 中引用附加资源。

于 2012-12-05T12:18:27.883 回答