2013-10-02 34 views
3

我期待從我的程序中生成一條Outlook消息,我能夠從程序內部構建和發送或構建並保存,我想要構建的然後顯示以允許用戶可以從AD列表中手動選擇收件人...下面的代碼是這裏和其他教程網站的示例混合,但是我無法找到構建,然後「顯示」電子郵件,而不保存草稿或從程序中發送它。 ..從C打開新的Outlook消息#

也是我希望找到一種方法,我可以創建一個電子郵件IE內部的UNC鏈接:寫出來的路徑,用戶文件夾\\ UNC \路徑\%USERNAME%或喜歡

private void sendEmailOutlook(string savedLocation, string packageName) 
    { 
     try 
     { 
      Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application(); 
      Microsoft.Office.Interop.Outlook.MailItem oMsg = (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem); 

      oMsg.HTMLBody = "Attached is the required setup files for your <i><b>soemthing</i></b> deployment package."; 
      oMsg.HTMLBody += "\nPlease save this file to your network user folder located.<br /><br/>\\\\UNC\\data\\users\\%USER%\\"; 
      oMsg.HTMLBody += "\nOnce saved please boot your Virtual machine, locate and execute the file at <br /> <br />\\\\UNC\\users\\%USER%\\"; 

      int pos = (int)oMsg.Body.Length +1; 
      int attachType = (int)Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue; 

      Microsoft.Office.Interop.Outlook.Attachment oAttach = oMsg.Attachments.Add(savedLocation, attachType, pos, packageName); 

      oMsg.Subject = "something deployment package instructions"; 
      oMsg.Save(); 

     } 
     catch(Exception ex) 
     { 
      Console.WriteLine("Email Failed", ex.Message); 
     } 
+1

[This](http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook._mailitem.display.aspx)不起作用? – Herdo

+0

@ Herdo它給我一個錯誤,但我可以通過它,它已成爲我期待的第一部分,你是否有能力回答第二部分:終端用戶的UNC路徑... – asuppa

+1

由於您使用的是HTML格式:您是否嘗試使用簡單的html-link元素(「')? – Herdo

回答

3
Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application(); 
Microsoft.Office.Interop.Outlook.MailItem oMsg = (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem); 

oMsg.Subject = "something deployment package instructions"; 
oMsg.BodyFormat = OlBodyFormat.olFormatHTML; 
oMsg.HTMLBody = //Here comes your body; 
oMsg.Display(false); //In order to display it in modal inspector change the argument to true 

關於(你知道用戶名的情況下)鏈接到該文件夾​​,你應該能夠使用:

<a href="C:\Users\*UserName*">Link</a> 

很多公司都附着其員工的用戶名,以解決項目(看起來是如「John Doe(Jdoe)」,其中Jdoe是用戶名)。 當用戶選擇收件人或嘗試發送你能趕上這些事件的電子郵件,並完成類似

foreach (Outlook.Recipient r in oMsg.Recipients) 
{ 
    string username = getUserName(r.Name);// or r.AddressEntry.Name instead of r.Name 
    oMsg.HTMLBody += "<a href='C:\\Users\\" + username + "'>Link</a>" 
} 
oMsg.Save(); 
oMsg.Send(); 

其中getUserName()是提取僅用戶名的方法(可能使用字符串或正則表達式)。

  • 確保郵件的身體是一個有效的HTML
  • /N不會給你,你應該使用<br> insted的新線。
+0

感謝我用@Herdo先前的所有解決方案解決了這個問題,但以前的所有鏈接都是,但是,在運行時不會知道用戶名,它將在通過outlook AD後添加, – asuppa