2011-12-12 140 views
1

我有一個簡單的小電子郵件應用程序,它允許用戶選擇某些選項來生成字符串併發送電子郵件。我想看看是否有可能將圖像添加到電子郵件中,即標題徽標或簽名等。我一直在研究的研究非常繁重,而且我對HTML知之甚少。誰能幫忙?我的代碼如下...使用Outlook Interop在電子郵件中嵌入圖像

using System; 
using Outlook = Microsoft.Office.Interop.Outlook; 
using System.Configuration; 

namespace My_EmailSender 
{ 
    public class EmailSender:Notification 
    { 
     string emailRecipient = ConfigurationManager.AppSettings["emailRecipient"]; 

     public void SendMail(string message) 
     { 
      try 
      { 
       var oApp = new Outlook.Application(); 
       var oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem); 
       var oRecip = (Outlook.Recipient)oMsg.Recipients.Add(emailRecipient); 
       oRecip.Resolve(); 
       oMsg.Subject = "Email Notification"; 
       oMsg.Body = message; 

       // Display the message before sending could save() also but no need 
       oMsg.Send(); 
       oMsg.Display(true); 
       oRecip = null; 
       oMsg = null; 
       oApp = null; 
      } 
      catch (Exception e) 
      { 
       Console.WriteLine("Problem with email execution. Exception caught: ", e); 
      } 

      return; 
     } 
    } 
} 

回答

0

我總是會用System.Net.Mail發送電子郵件,但也許這是你的要求嗎?

Read up on system.net.mail here.

+0

很容易使用的net.mail發送圖像直通前景樣本代碼,但是,如果您想發送大量電子郵件,我會推薦一個程序SendBlaster。 –

2

下面是在c#

 Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(HttpContext.Request.ApplicationPath); 
     System.Net.Configuration.MailSettingsSectionGroup settings = (System.Net.Configuration.MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings"); 
     System.Net.Configuration.SmtpSection smtp = settings.Smtp; 
     System.Net.Configuration.SmtpNetworkElement network = smtp.Network; 
     Microsoft.Office.Interop.Outlook.Application outlookApp = new Microsoft.Office.Interop.Outlook.Application(); 
     MailItem mailItem = (MailItem)outlookApp.CreateItem(OlItemType.olMailItem); 
     mailItem.To = network.TargetName; 


     Attachment attachment = mailItem.Attachments.Add(
     "C://test.bmp" 
     , OlAttachmentType.olEmbeddeditem 
     , null 
     , "test image" 
     ); 
     string imageCid = "[email protected]"; 

     attachment.PropertyAccessor.SetProperty(
      "http://schemas.microsoft.com/mapi/proptag/0x3712001E" 
      , imageCid 
      ); 
     mailItem.BodyFormat = OlBodyFormat.olFormatRichText; 
     mailItem.HTMLBody = String.Format(
       "<body><img src=\"cid:{0}\"></body>" 
      , imageCid 
      ); 

     mailItem.Importance = OlImportance.olImportanceNormal; 
     mailItem.Display(false); 

相關問題