我的應用程序應該發送一些電子郵件到某個目的地。該操作後,我想自動將已發送的郵件移動到特定的文件夾(基於郵件附件中的文檔類型)。如果文件夾不存在,那麼程序必須創建它,然後將郵件移動到新創建的文件夾中。問題是,在我創建一個新文件夾併成功將郵件成功移動到第一次之後,然後當我發送應該移動到所述文件夾的郵件時,程序沒有看到該文件夾。事實上,文件夾方法根本不返回任何文件夾。坦白說,我不知道什麼是錯的。C#Outlook;創建文件夾後無法移動電子郵件
在它說的調試檢查parentFolder.Folders「枚舉沒有結果」的時候
我不知道我是否應該在方法createFolder創建文件夾(後做什麼更多的即是這樣,更新文件夾列表...)
這裏是我的代碼:
public void moveEmails(string itemType, Boolean itemSent, Outlook.MailItem objMail)
{
Outlook.MAPIFolder folderParent = objMail.Parent as Outlook.MAPIFolder;
Outlook.Folders folders;
Boolean notMoved = true;
objMail.UserProperties.Add("TransferredBy", Outlook.OlUserPropertyType.olText, true, Outlook.OlUserPropertyType.olText);
objMail.UserProperties["TransferredBy"].Value = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
objMail.Save();
if (folderParent.Name != "Inbox")
folderParent = digForInbox(folderParent);
folders = folderParent.Folders;
if (!itemSent)
itemType = "NOT DELIVERED";
foreach (Outlook.MAPIFolder folder in folders)
{
if (folder.Name == itemType)
{
objMail.Move(folder);
notMoved = false;
}
}
if (notMoved)
createFolder(itemType,objMail, folderParent);
}
public void createFolder(string itemType, Outlook.MailItem objMail, Outlook.MAPIFolder folderParent)
{
Outlook.MAPIFolder folderNew;
folderNew = folderParent.Folders.Add(itemType, Outlook.OlDefaultFolders.olFolderInbox) as Outlook.MAPIFolder;
objMail.Move(folderNew);
}
private Outlook.MAPIFolder digForInbox(Outlook.MAPIFolder folder)
{
Boolean isNotInbox = true;
while(isNotInbox)
{
if(folder.Name != "Inbox")
{
folder = folder.Parent as Outlook.MAPIFolder;
}
else
{
isNotInbox = false;
}
}
return folder;
}
你可以清理你的代碼有點用'使用Microsoft.Office.Interop;' – 2014-12-04 21:45:19
[熱到移動項目中OutLook](http://msdn.microsoft.com/en-us/library/ms268996.aspx) – MethodMan 2014-12-04 21:52:53
在哪一行是拋出的異常? – Quackmatic 2014-12-04 21:57:57