从 Outlook 获取附件
Posted
技术标签:
【中文标题】从 Outlook 获取附件【英文标题】:Getting attachments from Outlook 【发布时间】:2012-02-18 06:18:17 【问题描述】:我是新的 2 C# 并且我被分配了一项任务...我必须编写一个 C# 代码来将已发送的电子邮件附件和电子邮件主题从 Outlook 2007 下载到本地驱动器或任何指定位置。我怎么做?我能够获取收件箱中的附件。谁能帮我获取通过 Outlook 发送的邮件?
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.olFolderSentMail);
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");
提前致谢
【问题讨论】:
【参考方案1】:我测试了你的代码,Attachment
类实例的SaveAsFile()
方法抛出:
System.IO.DirectoryNotFoundException:无法保存附件。 路径不存在。验证路径是否正确。在 Microsoft.Office.Interop.Outlook.Attachment.SaveAsFile(字符串路径) ...
所以,需要确保保存附件的目录存在。
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++)
string filePath = Path.Combine(destinationDirectory, newEmail.Attachments[i].FileName);
newEmail.Attachments[i].SaveAsFile(filePath);
catch (Exception ex)
Console.WriteLine(ex);
希望这会有所帮助。
【讨论】:
如果我想要收件箱中所选电子邮件的附件?【参考方案2】:维护一个列表,该列表将存储发件人的姓名,然后检查最近邮件的发件人在列表中包含的条件,如果是,则不是下载。
private void ThisApplication_NewMail()
const string destinationDirectory = @"C:\TestFileSave";
List<string> senderList = new List<string>();
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;
senderEmailAdd = newEmail.SenderEmailAddress;
if (newEmail == null) continue;
if (newEmail.Attachments.Count > 0 && senderList.Contains(senderEmailAdd))
for (int i = 1; i <= newEmail.Attachments.Count; i++)
string filePath = Path.Combine(destinationDirectory, newEmail.Attachments[i].FileName);
newEmail.Attachments[i].SaveAsFile(filePath);
catch (Exception ex)
Console.WriteLine(ex);
【讨论】:
以上是关于从 Outlook 获取附件的主要内容,如果未能解决你的问题,请参考以下文章
无法从Outlook REST API获取SourceUrl的附件