0
votes

i have this code for sending mail through C# WinForm.

i have this reference:

Microsoft.Office.Interop.Outlook (version 11.0)

my code:

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;

Sometimes it works Excellent and sometimes i got this error:

Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80080005.

i try it on computer with outlook 2010...2013..2016 and same problem.

Can not find why sometimes it works and sometimes not

thanks

1
Out of curiosity, why are you using the Outlook COM instead of just the System.Net.Mail library?krillgar
If you don't mind using System.Net.Mail, you can use the following thread for reference: stackoverflow.com/questions/35108461/…frostedcoder
ok, i try this and its working. but how to add file to this method ? and how to send mail winthout open outlook ?Gold
@Gold: You can send it via SMTP, you can refer this for code sample: msdn.microsoft.com/en-us/library/…frostedcoder

1 Answers

0
votes

There's a few main reasons for COM interop to fail:

  1. The application isn't installed. This is a bit obvious, but happens often enough that it's worth mentioning. Outlook interop will only work if Outlook is installed.
  2. The client runs in a different session or under a different user than the host. If both are just simple desktop applications, this shouldn't be a problem, but you can still check using task manager.
  3. The COM server is 32-bit, while your application is 64-bit. Again, easy to check with task manager. Build your application as 32-bit to allow proper COM interop to work. .NET applications run in AnyCPU mode by default - 32-bit on 32-bit OSes, and 64-bit on 64-bit. So everything would work fine on a 32-bit machine, but you'll get bitness-mismatch on 64-bit Windows.