2011-05-27 111 views
30

我正在尋找一種方式來在Outlook窗口中打開新郵件。如何打開Outlook新郵件窗口c#

我需要programically填寫:從,到,主題,正文信息,但離開這個新郵件窗口中打開,以便用戶可以驗證內容/添加一些東西然後把正常的Outlook味精。

發現:

Process.Start(String.Format(
"mailto:{0}?subject={1}&cc={2}&bcc={3}&body={4}", 
    address, subject, cc, bcc, body)) 

但沒有 「從」 選項(我的用戶有多個郵箱的詳細......)

任何意見(S)?

+0

是否有一個原因,你沒有在Outlook本身的應用程序使用VBScript?最終,如果你願意,你可以很容易地從c#啓動Outlook應用程序,在啓動時運行的outlook中有一個規則,可以從vbscript宏爲你填充。正如你期望在gui中編輯它,我的問題依然存在:你需要一個特定於c#的解決方案嗎?或者你只是在問你是否需要使用c#? – tjborromeo 2013-05-01 00:08:14

回答

41

I'cve終於解決了這個問題 下面是一段代碼,解決我的問題(與使用的Outlook互操作性展示)

Outlook.Application oApp = new Outlook.Application(); 
Outlook._MailItem oMailItem = (Outlook._MailItem)oApp.CreateItem (Outlook.OlItemType.olMailItem); 
oMailItem.To = address; 
// body, bcc etc... 
oMailItem.Display (true); 
+0

使用此代碼我得到一個例外。 – papaiatis 2013-03-27 13:30:05

+0

謝謝,效果很好。 – mack 2013-07-05 18:39:50

+0

當我的Outlook沒有打開時,當我運行類似的東西時,Outlook會打開,我會看到模態電子郵件對話框,但只要用戶點擊發送並且電子郵件停留在發件箱中,Outlook就立即關閉。有沒有人有這個問題? – user1198049 2013-09-04 22:17:12

0

你不能用mailto做到這一點。您的客戶必須選擇他們發送的帳戶,默認帳戶爲默認帳戶,或者您必須提供郵件表單並在發送電子郵件時設置標題。

+0

我知道我做不到這一點 - 這就是爲什麼我看着不同的選擇 - 看到一些應用程序管理,所以必須以某種方式... – Maciej 2011-05-27 07:39:13

4

這是我都試過了。它按預期工作。

此應用程序添加收件人,添加cc並添加主題並打開一個新的郵件窗口。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Threading; 
using Outlook = Microsoft.Office.Interop.Outlook; 

public partial class _Default : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 
    protected void ButtonSendMail_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      List<string> lstAllRecipients = new List<string>(); 
      //Below is hardcoded - can be replaced with db data 
      lstAllRecipients.Add("[email protected]"); 
      lstAllRecipients.Add("[email protected]"); 

      Outlook.Application outlookApp = new Outlook.Application(); 
      Outlook._MailItem oMailItem = (Outlook._MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem); 
      Outlook.Inspector oInspector = oMailItem.GetInspector; 
      // Thread.Sleep(10000); 

      // Recipient 
      Outlook.Recipients oRecips = (Outlook.Recipients)oMailItem.Recipients; 
      foreach (String recipient in lstAllRecipients) 
      { 
       Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add(recipient); 
       oRecip.Resolve(); 
      } 

      //Add CC 
      Outlook.Recipient oCCRecip = oRecips.Add("[email protected]"); 
      oCCRecip.Type = (int)Outlook.OlMailRecipientType.olCC; 
      oCCRecip.Resolve(); 

      //Add Subject 
      oMailItem.Subject = "Test Mail"; 

      // body, bcc etc... 

      //Display the mailbox 
      oMailItem.Display(true); 
     } 
     catch (Exception objEx) 
     { 
      Response.Write(objEx.ToString()); 
     } 
    } 
} 
+0

如果用戶使用outlook取消按鈕取消發送電子郵件,您將如何取消發送電子郵件? – singhswat 2017-04-13 11:57:38

+0

該代碼只會生成一個新的電子郵件窗口,其中填充了To:和Subject字段並將其顯示。用戶可以發送電子郵件(或不發送),所以如果電子郵件窗口關閉(未發送),則不會發送電子郵件。 – 2017-09-03 17:36:07