[英]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互操作失败的主要原因有几个:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.