I have a problem where my app is hanging on exit when I try to use saafeMail. If I run the code in the debugger the program exists normally, but when I run outside the debugger it hangs at exit.
RDOSession session = RedemptionLoader.new_RDOSession();
session.Logon(null, null, false, null, null, null);
Outlook.Application outlookApp = new Outlook.Application();
Outlook.NameSpace outlookNamespace = outlookApp.GetNamespace("MAPI");
Outlook.MailItem newMail = outlookApp.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem;
SafeMailItem safeMail = new SafeMailItem();
safeMail.Item = newMail;
safeMail.Recipients.Add("[email protected]");
safeMail.Recipients.ResolveAll();
newMail.Subject = "Subject";
safeMail.Body = "TextBody";
safeMail.Send();
Thread.Sleep(1000); // tried to add this to allow any pending COM operations to finish
Marshal.ReleaseComObject(safeMail);
Marshal.ReleaseComObject(newMail);
Marshal.ReleaseComObject(outlookNamespace);
Marshal.ReleaseComObject(outlookApp);
session.Logoff();
Marshal.ReleaseComObject(session);
I guess there is a problem releasing COM objects? Any ideas here?
You are still leaking a COM object reference since you are using multiple dot notation. Replace the following lines
safeMail.Recipients.Add("[email protected]");
safeMail.Recipients.ResolveAll();
with
var recipients = safeMail.Recipients;
var recip = recipients.Add("[email protected]");
recip.Resolve();
Marshal.ReleaseComObject(recipients);
Marshal.ReleaseComObject(recip);
You can also create a recipient in a resolved state using
var recipients = safeMail.Recipients;
var recip = recipients.AddEx("jon", "[email protected]", "SMTP", OlMailRecipientType.olTo);
Marshal.ReleaseComObject(recipients);
Marshal.ReleaseComObject(recip);