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.
Implementation:
using Outlook = Microsoft.Office.Interop.Outlook;

public void SendTestMail()
{
    try
    {
        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("abc@xyz.com");
        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.Save();
        otMsg.Send(); // Send Mail
        otRecip = null;
        otAttach = null;
        otMsg = null;
        otApp = null;
    }
    catch (Exception e)
    {
        Console.WriteLine("{0} Exception caught: ", e);
    }
    return 0;
}

13 comments:

  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.

    rajmca.g@gmail.com

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

      Jack

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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  5. Get solution for all error of Hotmail in one place by clicking on hotmail.com 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 onoutlook.com login and fill a callback form.

    ReplyDelete
  6. Hi my friend! I want to say that this post is awesome, great written and include almost all important infos. I would like to see more posts like this. Special info: The Ninja Guide To How To Password Protect Folder Better.

    ReplyDelete
  7. All you have to do is key in your questions and multiple possible solutions will be thrown up by Microsoft Help. Microsoft Help also provides inbuilt trainings that walk you through various packages.reset windows 10 password

    ReplyDelete
  8. Sebetulnya banyak penjelasan mengenai tentang cara menggugurkan kehamilan . Namun , belum tentu semua ulasan yang beredar di media internet tentang cara ampuh menggugurkan kandungan benar-benar terbukti manjur dalam mengatasi persoalan anda . Kenapa bisa begitu ? Memang banyak sekali kita jumpai topik-topik yang berbeda di berbagai situs dalam membahas content ini , ada cara alami dan medis . Cara tradisional belum jelas tentang kebenarannya . Oleh sebab itu , sebaiknya gunakan obat menggugurkan kandungan yang sudah jelas terbukti melalui riset . Apa saja ciri ciri pubertas pada perempuan yang terjadi pada wanita ? . Ternyata gejala menopause dialami oleh wanita yang berusia 45 sampai 55 tahun – usia rata-rata yang diketahui saat ini adalah 55 tahun . Dan satu hal lagi perempuan sangat penting untuk merawat daerah kewanitaannya , cara menjaga daerah kewanitaan agar tetap rapat harus dimulai sejak dini

    ReplyDelete