0

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

M. Schena
  • 2,039
  • 1
  • 21
  • 29
Gold
  • 60,526
  • 100
  • 215
  • 315
  • 1
    Out of curiosity, why are you using the Outlook COM instead of just the `System.Net.Mail` library? – krillgar Feb 02 '16 at 14:47
  • If you don't mind using System.Net.Mail, you can use the following thread for reference: http://stackoverflow.com/questions/35108461/cannot-send-email-with-smtp-in-windows-forms/35109274#35109274 – frostedcoder Feb 02 '16 at 15:02
  • ok, i try this and its working. but how to add file to this method ? and how to send mail winthout open outlook ? – Gold Feb 02 '16 at 16:46
  • @Gold: You can send it via SMTP, you can refer this for code sample: https://msdn.microsoft.com/en-us/library/system.net.mail.attachment(v=vs.110).aspx – frostedcoder Feb 03 '16 at 19:45

1 Answers1

0

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.
Luaan
  • 62,244
  • 7
  • 97
  • 116