微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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;
}
}

评论中的错误列表:

>只能将赋值,调用,递增,递减和新对象表达式用作语句
>无法找到类型或命名空间名称“电子邮件”(您是否缺少using指令或程序集引用?)
>当前上下文中不存在名称“A_Sent”
>当前上下文中不存在名称“moveMail”
>当前上下文中不存在名称“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();
        }

    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐