gpt4 book ai didi

C# 将邮件移动到 PST

转载 作者:行者123 更新时间:2023-11-30 13:57:44 25 4
gpt4 key购买 nike

我想使用 C# 访问我的 Outlook 已发送文件夹,并将那里的邮件移动到我的 PST 中名为 Archive 的文件夹中。这是我正在使用的代码,但出现多个编译错误。这里有更多编码经验的人知道如何完成这个吗?

static void MoveMe()
{
try
{
_app = new Microsoft.Office.Interop.Outlook.Application();
_ns = _app.GetNamespace("MAPI");
_ns.Logon(null, null, false, false);
Application.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderOutbox);
Outlook.Items SentMailItems = SentMail.Items;
Outlook.MailItem newEmail = null;
foreach (object collectionItem in SentMailItems)
{
moveMail.Move(Archive);
}
}
catch (System.Runtime.InteropServices.COMException ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
_ns = null;
_app = null;
_inboxFolder = null;
}
}

评论中的错误列表:

  1. 只有assignment、call、increment、decrement、new对象表达式可以作为语句使用
  2. 找不到类型或命名空间名称“Emails”(是否缺少 using 指令或程序集引用?)
  3. 名称“A_Sent”在当前上下文中不存在
  4. 名称“moveMail”在当前上下文中不存在
  5. 名称“SentMail”在当前上下文中不存在

最佳答案

这是一个示例,说明如何获取源文件夹 (SentItems) 并将它们移动到 PST(存档)。

using Outlook = Microsoft.Office.Interop.Outlook; 

public void MoveMyEmails()
{
//set up variables
Outlook.Application oApp = null;
Outlook.MAPIFolder oSource = null;
Outlook.MAPIFolder oTarget = null;
try
{
//instantiate variables
oApp = new Outlook.Application();
oSource = oApp.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail);
oTarget = oApp.Session.Folders["Archive"];
//loop through the folders items
for (int i = oSource.Items.Count; i > 0; i--)
{
move the item
oSource.Items[i].Move(oTarget);
}
}
catch (Exception e)
{
//handle exception
}
//release objects
if (oTarget != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(oTarget);
GC.WaitForPendingFinalizers();
GC.Collect();
}
if (oSource != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(oSource);
GC.WaitForPendingFinalizers();
GC.Collect();
}
if (oApp != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(oApp);
GC.WaitForPendingFinalizers();
GC.Collect();
}

}

关于C# 将邮件移动到 PST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19776935/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com