Wednesday, May 16, 2012

Send mail using Microsoft Outlook

Already posted how to mail using SMTP; but now in this implemenation will show how to send mail using Microsoft Outlook object.
But there a basic rule of these implementation:
  1. Create the required objects.
  2. Add recepient, main subject, mail body, attachment.
  3. Send mail and validate.
  4. Release resources.
using Outlook = Microsoft.Office.Interop.Outlook;

public void SendTestMail()
        Outlook.Application otApp = new Outlook.Application();// create outlook object
        Outlook.NameSpace ns = otApp.Session;
        Outlook.MailItem otMsg = (Outlook.MailItem)otApp.CreateItem(Outlook.OlItemType.olMailItem); // Create mail object
        Outlook.Inspector oInspector = otMsg.GetInspector;
        Outlook.Recipient otRecip = (Outlook.Recipient)otMsg.Recipients.Add("");
        otRecip.Resolve();// validate recipient address
        otMsg.Subject = "test subject";
        otMsg.Body = "text message";
        String sSource = "C:\\Test.txt";
        String sDisplayName = "TestAttachment";
        int iPos = (int)otMsg.Body.Length + 1;
        int iAttType = (int)Outlook.OlAttachmentType.olByValue;  
        Outlook.Attachment oAttach = otMsg.Attachments.Add(sSource,iAttType,iPos,sDisplayName); // add attachment
        otMsg.Send(); // Send Mail
        otRecip = null;
        otAttach = null;
        otMsg = null;
        otApp = null;
    catch (Exception e)
        Console.WriteLine("{0} Exception caught: ", e);
    return 0;


  1. Hi Kuntal Saha,
    Same coding is working fine in my system. But when i published in IIS it is not working :(. Could you please help me in this.

    1. Hi Raj,
      I would rather suggest you creating a service seperately to handle mail communication. Consume the service whenever you wanna send mail.


  2. just tried this code, doesnt do anything for me :(

  3. Thanks for posting this info. I just want to let you know that I just check out your site and I find it very interesting and informative. I can't wait to read lots of your posts. microsoft powerpoint courses malaysia

  4. It is because some antivirus programs often consider auto-emails that are trying to be sent or downloaded as phishing, spam, or virus carriers and thus block them. microsoft support number

  5. Get solution for all error of Hotmail in one place by clicking on login we are third-part hotmail support provider on nominal cost. We have expert to fix your error on call quickly.
    Our services also for outlook, Microsoft has upgraded it’s email services from hotmail to outlook so those are using upgraded version of Microsoft and having any issues they can dial our too-free number 1-844-797-8692 or click login and fill a callback form.