繁体   English   中英

通过C#WinForm发送邮件中的奇怪错误

[英]Strange error in sending the mail through C# WinForm

我有此代码用于通过C#WinForm发送邮件。

我有这个参考:

Microsoft.Office.Interop.Outlook (version 11.0)

我的代码:

string sampleSource = System.Windows.Forms.Application.StartupPath + @"\TEST.txt"; 
string sampleDisplayName = "Test";
Microsoft.Office.Interop.Outlook.Application sampleApp = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.MailItem sampleMessage = (Microsoft.Office.Interop.Outlook.MailItem)sampleApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
Microsoft.Office.Interop.Outlook.Recipient sampleRecipient = (Microsoft.Office.Interop.Outlook.Recipient)sampleMessage.Recipients.Add(sampleSource);
sampleRecipient.Resolve();
sampleMessage.Subject = "Test sub"
sampleMessage.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML;
FinalMSG = "Test msg";
sampleMessage.HTMLBody = FinalMSG;
sampleMessage.To = "MyMail@gmail.com";
int samplePosition = (int)sampleMessage.Body.Length + 1;
int sampleType = (int)Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue;
Microsoft.Office.Interop.Outlook.Attachment sampleFile = sampleMessage.Attachments.Add(sampleSource, sampleType, samplePosition, sampleDisplayName);
sampleMessage.Save();
sampleMessage.Send();
sampleRecipient = null;
sampleFile = null;
sampleMessage = null;
sampleApp = null;

有时候效果很好,有时候我得到这个错误:

由于以下错误,检索具有CLSID {0006F03A-0000-0000-C000-000000000046}的组件的COM类工厂失败:80080005。

我在具有Outlook 2010 ... 2013..2016和相同问题的计算机上尝试使用它。

找不到为什么有时有效但有时无效的原因

谢谢

COM互操作失败的主要原因有几个:

  1. 未安装该应用程序。 这有点明显,但是经常发生,值得一提。 仅在安装了Outlook的情况下,Outlook互操作才有效。
  2. 客户端在与主机不同的会话中或在不同的用户下运行。 如果这两个都是简单的桌面应用程序,那应该不成问题,但是您仍然可以使用任务管理器进行检查。
  3. COM服务器是32位,而您的应用程序是64位。 同样,易于与任务管理器进行检查。 将您的应用程序构建为32位,以允许正确的COM互操作。 .NET应用程序默认情况下以AnyCPU模式运行-在32位操作系统上为32位,在64位操作系统上为64位。 因此,一切都可以在32位计算机上正常运行,但是在64位Windows上您会遇到位不匹配的情况。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM