0
votes

I want to get emails from an Outlook Inbox. I'm using the newest Version of Redemption 5.21.0.5378 on Windows 10 with Outlook 2016 installed.

Unfortunately the Program which does that, I haven't written I just got the task to get it working. I also haven't used Redemption before.

Here is the code (I removed some lines, which I think aren't important, if you need the full method, just say):

        AMS.Profile.Xml mailboxesxml;
        Redemption.RDOSession rdo;
        Redemption.RDOFolder inbox;
        Redemption.RDOMail msg;
        Redemption.RDOAttachment att;

        string[] mailboxes;

        try
        {
            mailboxesxml = new AMS.Profile.Xml(appPath + "mailboxes.xml");

            rdo = new Redemption.RDOSession();
            mailboxes = mailboxesxml.GetSectionNames();
            if (mailboxes != null) 
            {
                foreach (string me in mailboxes) {

                    sserver = mailboxesxml.GetValue(me, "server", "");
                    suser = mailboxesxml.GetValue(me, "user", ""); //Environment.UserName);
                    spassword = mailboxesxml.GetValue(me, "password", ""); 
                    sfolder = mailboxesxml.GetValue(me, "folder", "");
                    stargetdir = mailboxesxml.GetValue(me, "targetdirectory", appPath);
                    stargetdir = IncludeBackslash(stargetdir);
                    sfilename = mailboxesxml.GetValue(me, "filename", "$$EntryID$$");

                    //LOGON
                    rdo.LogonHostedExchangeMailbox(sserver, suser, spassword);
                    inbox = rdo.GetFolderFromPath(sfolder);


                    Redemption.RDOItems mails;
                    mails = inbox.Items;

                    while (mails.Count > 0) {

                        msg = mails.Item(1);
                        fn = "Test"
                        msg.SaveAs(fn, exportFormat); 


                        msg.MarkRead(true);
                        msg.Delete(Redemption.redDeleteFlags.dfHardDelete);
                        Marshal.ReleaseComObject(msg);

                    }
                    writeLog(@"cleanup...1", 2);
                    Marshal.ReleaseComObject(inbox);
                    writeLog(@"cleanup...2", 2);
                    Marshal.ReleaseComObject(mails);
                    writeLog(@"cleanup...3", 2);
                    GC.Collect();
                    writeLog(@"cleanup...4", 2);
                    rdo.Logoff();
                    writeLog(@"cleanup...5", 2);

                }
            }
            Marshal.ReleaseComObject(rdo);
            writeLog(@"cleanup...6", 2);

        }
        catch (Exception ex)
        {
            writeLog(@"error retrieving mails: " + ex, 0);
            GC.Collect();
        }
        finally
        {
            GC.Collect();
            writeLog(@"cleanup...7", 2);
        }

The Program successfully logon to outlook, gets the mail,mark the mail as read and deletes the mail. But when its calling the rdo.Logoff(); Method, it just gets stuck and not even throwing a exception.

So I never getting the "cleanup...5" log.

Maybe it something similar to this question? Process gets stuck in oSession.Logoff()

Some more relevant info: The Application is single threaded and its happening on the first run.

Thank you

1

1 Answers

0
votes

Try to call GC.Collect() before calling Logoff - you still have live Redemption objects (that internally reference various MAPI objects) when you call Logoff.

You might also want to scope variables other than RDOSession to make sure GC.Collect() can release them:

    Redemption.RDOSession rdo;
    try
    {
         MS.Profile.Xml mailboxesxml;
         Redemption.RDOFolder inbox;
         Redemption.RDOMail msg;
         Redemption.RDOAttachment att;
         string[] mailboxes;
         ...
    }
    catch (Exception ex)
    {
        writeLog(@"error retrieving mails: " + ex, 0);
        Marshal.ReleaseComObject(rdo);
        GC.Collect();
    }
    finally
    {
        GC.Collect();
        rdo.Logoff();
        Marshal.ReleaseComObject(rdo);
        writeLog(@"cleanup...7", 2);
    }