我是新的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");
}
}
}
先谢谢
答案 0 :(得分:5)
我已经测试了您的代码,SaveAsFile()
类实例的Attachment
方法抛出了:
{System.IO.DirectoryNotFoundException:无法保存附件。 路径不存在。验证路径是否正确。在 Microsoft.Office.Interop.Outlook.Attachment.SaveAsFile(String Path) ......}
因此,有必要确保保存附件的目录存在。
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);
}
}
希望这有帮助。
答案 1 :(得分:0)
维护一个列表,该列表将存储发件人的姓名,然后检查最近邮件的发件人在列表中包含的条件,如果是,则下载。
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);
}
}