![](/img/trans.png)
[英]How can I fix “Unable to cast COM object of type 'Microsoft.Office.Interop.Outlook.ApplicationClass' / The RPC server is unavailable”?
[英]InvalidCastException - Unable to cast COM object of type 'Microsoft.Office.Interop.Outlook.ApplicationClass'
我已經編寫了這段代碼,以便從我的.net應用程序使用Outlook發送附件文件,這是代碼:
Microsoft.Office.Interop.Outlook.Application outlook = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.Application session = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.NameSpace ns = outlook.Session;
Outlook.MailItem mail = outlook.Application.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem;
mail.Subject = txtSubject.Text;
mail.To = txtTo.Text;
mail.Subject = txtSubject.Text;
mail.Body = txtBody.Text;
mail.Attachments.Add(@"c:\Users\admin\Desktop\Excel.txt",
Outlook.OlAttachmentType.olByValue, Type.Missing,
Type.Missing);
Outlook.Accounts accounts = outlook.Session.Accounts;
foreach (Outlook.Account account in accounts)
{
// When the e-mail address matches, send the mail.
if (string.Equals(account.SmtpAddress, txtFrom.Text, StringComparison.OrdinalIgnoreCase))
{
mail.SendUsingAccount = account;
mail.Save();
((Outlook._MailItem)outlook).Send();
lblStatus.Text = "Report Sent";
break;
}
}
但是當到達Send()方法調用時,我得到了這個錯誤:
Unable to cast COM object of type 'Microsoft.Office.Interop.Outlook.ApplicationClass' to interface type 'Microsoft.Office.Interop.Outlook._MailItem'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063034-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
您投放了錯誤的對象
outlook
定義為Microsoft.Office.Interop.Outlook.Application
你需要改變
((Outlook._MailItem)outlook).Send();
至:
((Outlook._MailItem)mail).Send();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.