2014-10-27 63 views
0

當編程創建新的電子郵件以下代碼用於:展望現有的郵件消息

solutionRoot = rootStoreFolder.Folders.Add("MyInboxFolder", Outlook.OlDefaultFolders.olFolderInbox) as Outlook.Folder; 
folder = solutionRoot.Folders["MyFolderName"] as Outlook.Folder; 
subFolder = folder.Folders["MyFolderSubName"] as Outlook.Folder; 

Outlook.MailItem mailItem = this.Application.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem; 

mailItem.Subject = "TestSubject"; 
mailItem.To = "[email protected]";       
mailItem.Body = "This is the message."; 
mailItem.Importance = Outlook.OlImportance.olImportanceLow; 
mailItem.Display(false); 
mailItem.Move(subFolder); 

結果是具有放置到文件夾的子文件夾發送按鈕一個新的消息的創建。

問題是:我應該使用什麼代碼才能創建一個新的郵件項目到名爲subFolder的文件夾中,而不是準備發送的新電子郵件,已收到並已閱讀的電子郵件,一個現有的電子郵件。

+0

LucianC你好,請嘗試一些背景添加到您的問題,也許本指南有助於:http://stackoverflow.com/help/how-to-ask – reto 2014-10-27 14:57:37

回答

0

Outlook不會讓您創建已發送的郵件(MailItem.Sent屬性爲只讀)。 發送狀態中創建的唯一項目是一個發佈項目 - 您可以創建一個olPostItem對象(PostItem),然後將其MessageClass屬性更改回IPM.Note。您還需要刪除PR_ICON_INDEX屬性以確保其正確顯示。請注意,無法使用OOM設置發送/接收日期。

如果使用Redemption是一個選項,下面的腳本(VB)會創建一個假接收到的消息給當前用戶:

set Session = CreateObject("Redemption.RDOSession") 
    Session.MAPIOBJECT = Application.Session.MAPIOBJECT 
    set Inbox = Session.GetDefaultFolder(olFolderInbox) 
    set Msg = Inbox.Items.Add 
    Msg.Sent = true 
    set CU = Session.CurrentUser 
    set recip = Msg.Recipients.AddEx(CU.Name, CU.SmtpAddress, "SMTP", olTo) 
    Msg.Subject = "fake received message" 
    Msg.Body = "just a test" 
    vSenderEntryId = Session.AddressBook.CreateOneOffEntryID("Joe The Sender", "SMTP", "[email protected]", false, true) 
    set vSender = Session.AddressBook.GetAddressEntryFromID(vSenderEntryId) 
    Msg.Sender = vSender 
    Msg.SentOnBehalfOf = vSender 
    Msg.SentOn = Now 
    Msg.ReceivedTime = Now 
    Msg.Save 
+0

我需要的是創建一個已收到但未發送的電子郵件。這可能嗎? – LucianC 2014-10-28 07:24:42

+0

如果MailItem.Sent屬性是隻讀的,可如何在上面的代碼中設置Msg.Set = true? – LucianC 2014-10-28 08:22:17

+0

上面的腳本使用了Redemption,而不是Outlook對象模型。在第一次保存該項目之前,使用Redemption可以設置Sent屬性(MAPI限制)。 – 2014-10-28 20:23:18