none
c# code to send mail in outlook RRS feed

  • Question

  • Hi

    we created a form with a button . our requirement is when the user clicks on the button, then a mail has to be sent from his outlook(already configured on the machine) to a mailid

      . I tried the code suggested in similar forums

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using outlook = Microsoft.Office.Interop.Outlook;

    namespace Novartis_utilities
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void button2_Click(object sender, EventArgs e)
            {
                outlook.Application oApp = new outlook.Application();
                outlook.MailItem omsg = (outlook.MailItem) oApp.CreateItem(outlook.OlItemType.olMailItem);
                outlook._NameSpace ons = oApp.GetNamespace("MAPI");
                ons.Logon(null, null, true, true);
                outlook.MAPIFolder ofldr = ons.GetDefaultFolder(outlook.OlDefaultFolders.olFolderOutbox);
                outlook.Recipient orecip = omsg.Recipients.Add(mailid string);
                orecip.Type = (int)outlook.OlMailRecipientType.olTo;
                orecip.Resolve();
                omsg.Subject = "this is test mail";
                omsg.Body = "this is also test mail body";
                omsg.Save();
                omsg.Send();
            }
        }
    }

    but end up getting the below error while debugging the code

    "Operation aborted(Exception from HRESULT:0x8000404(E_ABORT)".

    . Could anybody please suggest a solution.

    Thanks

    Phaneendra

    Thursday, January 17, 2013 1:23 PM

Answers

  • try to move oApp.Createitem after getting hold of Session object (GetNamespace). Second, leave outbox alone, touching mails there is bound to give you troubles, if you want to wait untill your mail is sent to give Ui feedback or close outlook, get hold of sentmails folder and attach there to ItemAdd event - if this event will be raised (assuming no other outbound mails are being sent in the meantime, your responsibility to check that) you will know that mail was indeed sent.
    Thursday, January 17, 2013 2:53 PM
  • Is this code running in a VSTO addin for Outlook? This is a VSTO forum.
     
    If this is not running in a VSTO addin, when you try this code is Outlook already running? You should check for that instead of just trying to instantiate a new Outlook session. You also should use NameSpace.Logon("", "", false, false), and there's no need at all in that code to get an instance of the Outbox.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "PHANEENDRA SVRSS" <=?utf-8?B?UEhBTkVFTkRSQSBTVlJTUw==?=> wrote in message news:2127c7b0-c0af-4664-bba5-f4fbc95df516...

    Hi

    we created a form with a button . our requirement is when the user clicks on the button, then a mail has to be sent from his outlook(already configured on the machine) to a mailid

      . I tried the code suggested in similar forums

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using outlook = Microsoft.Office.Interop.Outlook;

    namespace Novartis_utilities
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void button2_Click(object sender, EventArgs e)
            {
                outlook.Application oApp = new outlook.Application();
                outlook.MailItem omsg = (outlook.MailItem) oApp.CreateItem(outlook.OlItemType.olMailItem);
                outlook._NameSpace ons = oApp.GetNamespace("MAPI");
                ons.Logon(null, null, true, true);
                outlook.MAPIFolder ofldr = ons.GetDefaultFolder(outlook.OlDefaultFolders.olFolderOutbox);
                outlook.Recipient orecip = omsg.Recipients.Add(mailid string);
                orecip.Type = (int)outlook.OlMailRecipientType.olTo;
                orecip.Resolve();
                omsg.Subject = "this is test mail";
                omsg.Body = "this is also test mail body";
                omsg.Save();
                omsg.Send();
            }
        }
    }

    but end up getting the below error while debugging the code

    "Operation aborted(Exception from HRESULT:0x8000404(E_ABORT)".

    . Could anybody please suggest a solution.

    Thanks

    Phaneendra


    Ken Slovak MVP - Outlook
    Thursday, January 17, 2013 3:11 PM

All replies

  • try to move oApp.Createitem after getting hold of Session object (GetNamespace). Second, leave outbox alone, touching mails there is bound to give you troubles, if you want to wait untill your mail is sent to give Ui feedback or close outlook, get hold of sentmails folder and attach there to ItemAdd event - if this event will be raised (assuming no other outbound mails are being sent in the meantime, your responsibility to check that) you will know that mail was indeed sent.
    Thursday, January 17, 2013 2:53 PM
  • Is this code running in a VSTO addin for Outlook? This is a VSTO forum.
     
    If this is not running in a VSTO addin, when you try this code is Outlook already running? You should check for that instead of just trying to instantiate a new Outlook session. You also should use NameSpace.Logon("", "", false, false), and there's no need at all in that code to get an instance of the Outbox.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "PHANEENDRA SVRSS" <=?utf-8?B?UEhBTkVFTkRSQSBTVlJTUw==?=> wrote in message news:2127c7b0-c0af-4664-bba5-f4fbc95df516...

    Hi

    we created a form with a button . our requirement is when the user clicks on the button, then a mail has to be sent from his outlook(already configured on the machine) to a mailid

      . I tried the code suggested in similar forums

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using outlook = Microsoft.Office.Interop.Outlook;

    namespace Novartis_utilities
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void button2_Click(object sender, EventArgs e)
            {
                outlook.Application oApp = new outlook.Application();
                outlook.MailItem omsg = (outlook.MailItem) oApp.CreateItem(outlook.OlItemType.olMailItem);
                outlook._NameSpace ons = oApp.GetNamespace("MAPI");
                ons.Logon(null, null, true, true);
                outlook.MAPIFolder ofldr = ons.GetDefaultFolder(outlook.OlDefaultFolders.olFolderOutbox);
                outlook.Recipient orecip = omsg.Recipients.Add(mailid string);
                orecip.Type = (int)outlook.OlMailRecipientType.olTo;
                orecip.Resolve();
                omsg.Subject = "this is test mail";
                omsg.Body = "this is also test mail body";
                omsg.Save();
                omsg.Send();
            }
        }
    }

    but end up getting the below error while debugging the code

    "Operation aborted(Exception from HRESULT:0x8000404(E_ABORT)".

    . Could anybody please suggest a solution.

    Thanks

    Phaneendra


    Ken Slovak MVP - Outlook
    Thursday, January 17, 2013 3:11 PM