C#将邮件移到PST [英] C# to move mail to PST

查看:268
本文介绍了C#将邮件移到PST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用C#访问我的Outlook已发送文件夹,并将其中的消息移动到我的PST中名为存档"的文件夹中.这是我正在使用的代码,但是出现多个编译错误.这里有更多编码经验的人知道如何做到这一点吗?

I want to use C# to access my Outlook Sent Folder and move the messages there to a folder in my PST called Archive. This is the code I was working with, but I am getting multiple compile errors. Does someone here with more coding experience know how to accomplish this?

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. Only assignment, call, increment, decrement, and new object expressions can be used as a statement
  2. The type or namespace name 'Emails' could not be found (are you missing a using directive or an assembly reference?)
  3. The name 'A_Sent' does not exist in the current context
  4. The name 'moveMail' does not exist in the current context
  5. The name 'SentMail' does not exist in the current context
  1. Only assignment, call, increment, decrement, and new object expressions can be used as a statement
  2. The type or namespace name 'Emails' could not be found (are you missing a using directive or an assembly reference?)
  3. The name 'A_Sent' does not exist in the current context
  4. The name 'moveMail' does not exist in the current context
  5. The name 'SentMail' does not exist in the current context

推荐答案

这是如何获取源文件夹(SentItems)并将其移动到PST(Archive)的示例.

here is an example of how you would get the source folder (SentItems) and move them to a PST(Archive).

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆