2012-12-05 37 views
1

我有一個問題發送HTML和附件作爲嵌入式圖像的郵件。 我不知道如何使用我的圖片文件夾中的圖片作爲div的背景。我如何在ASP.NET中使用圖像作爲我的html郵件的背景

這裏我的代碼:

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

      oMail.From = "[email protected]"; 

      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 = "[email protected]"; 
       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; 
      } 

回答

2

我使用此代碼,它是工作很好用LinkedResourcesAlternateviews。 比硬編碼的CIDS好得多。

檢查:

所有的
System.Net.Mail.MailMessage Mensaje = new System.Net.Mail.MailMessage("[email protected]",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); 
+1

是的,但我的代碼也工作。只有「

」不工作,我希望該div有背景 – Tarasov

+0

@塔拉索夫即使面臨完全相同的問題你解決了這個問題嗎? –

1

我不知道asp.net但PHP我們通過圖像轉換爲Base64編碼,然後使用

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIA..." /> 
1

你將不得不做給圖像的完整路徑。

1

首先,我發現這個project

但基本上,將圖像「嵌入」到電子郵件本身中,您需要將其添加爲Linked Resource,並在電子郵件的HTML中引用附加資源。

相關問題