0

I'm trying to send mail from my c#.net application, I've used this code:

var mailItem = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
    mailItem.Subject = "Error Report from user: " + AuthenticationManager.LoggedInUserName;
    mailItem.HTMLBody = "Test email\n"+ReadSignature();
    mailItem.To = "test@test.com";
    mailItem.Display(true);

The mail doesn't get sent, but added to the outbox folder. I suspect that the mail isn't sent because outlook isn't started. So I googled and came up with this:

        var oApp = new Outlook.Application();

        Microsoft.Office.Interop.Outlook.NameSpace ns = oApp.GetNamespace("MAPI");
        var f = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);

        Thread.Sleep(5000); // a bit of startup grace time.

        var mailItem = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
        mailItem.Subject = "Error Report from user: " + AuthenticationManager.LoggedInUserName;
        mailItem.HTMLBody = "Test email\n"+ReadSignature();
        mailItem.To = "test@test.com";

        mailItem.Display(true);

But once again it end up in the outbox folder. I need to start outlook, because I can't specify the FromMail property in every single client application.config. Any ideas?

Tobias Moe Thorstensen
  • 8,861
  • 16
  • 75
  • 143
  • if you are using web application then you can configure from mail in web.config file – user1102001 Sep 30 '13 at 10:00
  • Do you really need this overkill to simply send simple email? there are classes that allow sending mail directly without outlook. – Rafal Sep 30 '13 at 10:02

3 Answers3

3

You need to call the MailItem.Send method:

mailItem.Send();
Douglas
  • 53,759
  • 13
  • 140
  • 188
0

Try to use Namespace.SyncObjects collection or Namespace.SendAndReceive method to start a sync.

Note that message submission is an asynchronous process, so you will need to keep a reference to the Namespace object and the SyncObject (if you are using it) until the message is actually sent.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
0

Check this answer for similar question - it contains solution where you don't have to use

Thread.Sleep(5000)
Community
  • 1
  • 1
Woodman
  • 1,108
  • 9
  • 11