Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# to move mail to PST

Tags:

c#

outlook

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

Error list from the comments:

  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
like image 780
MasterOfStupidQuestions Avatar asked Nov 04 '13 20:11

MasterOfStupidQuestions


People also ask

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

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

    }
like image 79
Sorceri Avatar answered Sep 30 '22 02:09

Sorceri