我已经编写了outllok插件的代码,以便当电子邮件到达收件箱时它应该下载,但是当前代码是这样的,如果它到达特定文件夹也不会被下载。有人可以指导我吗?
private void ThisApplication_NewMail()
{
const string destinationDirectory = @"C:\TestFileSave";
if (!Directory.Exists(destinationDirectory))
{
Directory.CreateDirectory(destinationDirectory);
}
MAPIFolder sentMail = Application.ActiveExplorer().Session.GetDefaultFolder(OlDefaultFolders.olFolderSentMail);
Items sentMailItems = sentMail.Items;
try
{
foreach (object collectionItem in sentMailItems)
{
MailItem newEmail = collectionItem as MailItem;
if (newEmail == null) continue;
if (newEmail.Attachments.Count > 0)
{
for (int i = 1; i <= newEmail.Attachments.Count; i++)
{
if (newEmail.Attachments[i].FileName.Contains("Logic"))
{
string filePath = Path.Combine(destinationDirectory, newEmail.Attachments[i].FileName);
newEmail.Attachments[i].SaveAsFile(filePath);
}
}
}
}
}
catch (System.Exception ex)
{
Console.WriteLine(ex);
}
答案 0 :(得分:0)
您可以处理NewMailEx事件,以便在收到新邮件时收到通知。然后,您可以使用传递给NewMailEx事件处理程序的EntryID值获取刚到达项目的实例。 Namespace类的GetItemFromID方法返回由指定条目ID标识的Microsoft Outlook项(如果有效)。
您也可以考虑处理Items类的ItemAdd事件。将一个或多个项目添加到指定集合时会触发它。请注意,当一次将大量项目添加到文件夹时,此事件不会运行。
您可以在系列文章中阅读更多相关内容:
Outlook项目的MarkForDownload属性返回一个OlRemoteStatus常量,该常量确定远程用户收到项目后的状态。例如,以下示例在用户的收件箱中搜索尚未完全下载的项目。如果发现任何未完全下载的项目,则会显示一条消息,并标记该项目以供下载。
Sub DownloadItems()
Dim mpfInbox As Outlook.Folder
Dim obj As Object
Dim i As Integer
Set mpfInbox = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
'Loop all items in the Inbox folder
For i = 1 To mpfInbox.Items.Count
Set obj = mpfInbox.Items.Item(i)
'Verify if the state of the item is olHeaderOnly
If obj.DownloadState = olHeaderOnly Then
MsgBox ("This item has not been fully downloaded.")
'Mark the item to be downloaded.
obj.MarkForDownload = olMarkedForDownload
End If
Next
End Sub
最后,您可以使用SyncObject类的Start方法开始使用指定的Send \ Receive组同步用户的文件夹。
Public Sub Sync()
Dim nsp As Outlook.NameSpace
Dim sycs As Outlook.SyncObjects
Dim syc As Outlook.SyncObject
Dim i As Integer
Dim strPrompt As Integer
Set nsp = Application.GetNamespace("MAPI")
Set sycs = nsp.SyncObjects
For i = 1 To sycs.Count
Set syc = sycs.Item(i)
strPrompt = MsgBox( _
"Do you wish to synchronize " & syc.Name &"?", vbYesNo)
If strPrompt = vbYes Then
syc.Start
End If
Next
End Sub