您好我想获得Outlook发送的附件和邮件的收件人主题...我能够通过Outlook的默认文件夹获取它。 如何在发送邮件之前通过VSTO检索发送的邮件。
现在我正在这样做
namespace OutlookAddInAttachment
{
public partial class ThisAddIn
{
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
this.Application.NewMail += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_NewMailEventHandler(ThisApplication_NewMail);
}
private void ThisApplication_NewMail()
{
Outlook.MAPIFolder SentMail = this.Application.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderOutbox);
Outlook.Items SentMailItems = SentMail.Items;
Outlook.MailItem newEmail = null;
//SentMailItems = SentMailItems.Restrict("[Unread] = true");
try
{
foreach (object collectionItem in SentMailItems)
{
newEmail = collectionItem as Outlook.MailItem;
if (newEmail != null)
{
if (newEmail.Attachments.Count > 0)
{
for (int i = 1; i <= newEmail.Attachments.Count; i++)
{
newEmail.Attachments[i].SaveAsFile(@"C:\TestFileSave\" + newEmail.Attachments[i].FileName);
}
}
}
}
}
catch (Exception ex)
{
string errorInfo = (string)ex.Message
.Substring(0, 11);
if (errorInfo == "Cannot save")
{
MessageBox.Show(@"Create Folder C:\TestFileSave");
}
}
}
答案 0 :(得分:1)
我不确定你到底想要做什么。
您正在侦听NewMail事件,该事件侦听INBOX中的新RECEIVED邮件,然后扫描已发送邮件文件夹,根据定义,该文件夹仅包含已发送的邮件。
如果您要做的是截取正在发送的新邮件的附件,那么您需要的是ItemSend事件,这将允许您在发送之前捕获它:
public partial class ThisAddIn
{
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
this.Application.ItemSend += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_ItemSendEventHandler(ThisApplication_ItemSend);
}
private void ThisApplication_ItemSend(object item, bool cancel)
{
Outlook.MailItem newEmail = item as MailItem;
if (newEmail != null)
{
foreach (var attachment in newEmail.Attachments)
{
attachment.SaveAsFile(@"C:\TestFileSave\" + attachment.FileName);
}
}
}
}