通过C#WinForm发送邮件时出现奇怪的错误
我有这个通过C#WinForm发送邮件的代码 我有以下参考:通过C#WinForm发送邮件时出现奇怪的错误,c#,winforms,email,C#,Winforms,Email,我有这个通过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
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的组件的COM类工厂
{0006F03A-0000-0000-C000-0000000000 46}由于以下原因失败
错误:80005
我用outlook 2010…2013…2016在电脑上试过了,同样的问题
找不到为什么有时有效,有时无效
谢谢COM互操作失败有几个主要原因:
出于好奇,您为什么要使用Outlook COM而不仅仅是
System.Net.Mail
库?如果您不介意使用System.Net.Mail,您可以使用以下线程作为参考:好的,我尝试一下它的工作原理。但如何将文件添加到此方法中?如何在不打开outlook的情况下发送邮件?@Gold:您可以通过SMTP发送邮件,您可以参考以下代码示例:谢谢您的帮助,我尝试了所有这些-但仍然存在相同的问题