Outlook 兑换和 safeMail 在退出时挂起

问题描述 投票:0回答:1

当我尝试使用 saafeMail 时,我的应用程序在退出时挂起。如果我在调试器中运行代码,程序会正常存在,但是当我在调试器之外运行时,它会在退出时挂起。

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

我猜释放 COM 对象有问题?这里有什么想法吗?

outlook-redemption
1个回答
0
投票

由于您使用的是多点表示法,因此您仍然泄漏 COM 对象引用。替换以下行

safeMail.Recipients.Add("[email protected]");  
safeMail.Recipients.ResolveAll(); 

var recipients = safeMail.Recipients;
var recip = recipients.Add("[email protected]");  
recip.Resolve();
Marshal.ReleaseComObject(recipients);
Marshal.ReleaseComObject(recip);

您还可以使用

创建处于已解决状态的收件人
var recipients = safeMail.Recipients;
var recip = recipients.AddEx("jon", "[email protected]", "SMTP", OlMailRecipientType.olTo);  
Marshal.ReleaseComObject(recipients);
Marshal.ReleaseComObject(recip);
© www.soinside.com 2019 - 2024. All rights reserved.