C#将邮件移至PST
我想使用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; } }
评论中的错误列表:
-
Only assignment, call, increment, decrement, and new object expressions can be used as a statement
-
The type or namespace name 'Emails' could not be found (are you missing a using directive or an assembly reference?)
-
The name 'A_Sent' does not exist in the current context
-
The name 'moveMail' does not exist in the current context
-
The name 'SentMail' does not exist in the current context
这里是一个如何获取源文件夹(SentItems)并将它们移动到PST(存档)的示例。
上述就是C#学习教程:C#将邮件移至PST分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
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(); } }
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/987916.html