Friday 9 December 2011

Opening Outlook with new e-mail from Asp.net

add Outlook reference through com
then include  " using Outlook = Microsoft.Office.Interop.Outlook;  " in header
use the following code for opening outlook with new email

code:

try
        {
            // Create the Outlook application.
            Outlook.Application oApp = new Outlook.Application();

            // Get the NameSpace and Logon information.
            Outlook.NameSpace oNS = oApp.GetNamespace("mapi");

            // Log on by using a dialog box to choose the profile.
            oNS.Logon("Outlook", "", true, true);

            // Alternate logon method that uses a specific profile.
            // TODO: If you use this logon method,
            //  change the profile name to an appropriate value.
            //oNS.Logon("YourValidProfile", Missing.Value, false, true);

            // Create a new mail item.
            Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);

            // Set the subject.
            oMsg.Subject = "Testing";

            // Set HTMLBody.
            String sHtml;
            sHtml = "<HTML>\n" +
               "<HEAD>\n" +
               "<TITLE>Sample GIF</TITLE>\n" +
               "</HEAD>\n" +
               "<BODY><P>\n" +
               "<h1><Font Color=Green>Testing</Font></h1></P>\n" +
               "<P>Testing Testing Testing Testing</P>\n" +
               "<P>Testing Testing Testing Testing</P>\n" +
               "</BODY>\n" +
               "</HTML>";
            oMsg.HTMLBody = sHtml;
          //  oMsg.Attachments.Add(OpenFile(@"C:\MyAttachmentFi.txt"), Outlook.OlAttachmentType.olByValue, Type.Missing, Type.Missing);
            //try for attachments sorry the above code is not working..
           

            // Add a recipient.
            Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
            // TODO: Change the recipient in the next line if necessary.
            Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add("emailid@mail.com");
           
         oRecip.Resolve();
           oMsg.Display(oMsg);  //code for display the outlook
            // Send.
      //    oMsg.Send();    //code for sending

            // Log off.
          oNS.Logoff();

            // Clean up.
            oRecip = null;
            oRecips = null;
            oMsg = null;
            oNS = null;
            oApp = null; 
        }

         // Simple error handling.
        catch (Exception ess)
        {
            Console.WriteLine("{0} Exception caught.", ess);
        } 


screen shot:


Thankyou..:)

6 comments:

  1. will this work for web app? if not, it is of no use.

    ReplyDelete
  2. Hi,

    Yes, It works.

    ReplyDelete
  3. Hi, I'm trying to do the same thing and have a ASP.NET form (VB.NET) that adds an appointment to a shared calendar on our Exchange Server 2007 using Outlook 2007 interop. It works fine from my develpment workstation but when I try and run it from the web server I get a compile error: Compiler Error Message: BC30002: Type 'Outlook.Application' is not defined.

    on the following line:

    Dim olApp As Outlook.Application = New Outlook.Application()

    I would appreciate any ideas on getting this to work.
    Thanks, Gary

    ReplyDelete
  4. Can you please check the Dlls which are used in Your workstation are those are avialable in web server as well..
    Jeevan.

    ReplyDelete
  5. That code only works when I run the web application on my local machine in Debug mode. Why is this so dang difficult? What I need is when the user clicks a button it simply opens a New Email message in outlook so the user can modify the info if needed then they can send. It needs to open this on the client's machine.

    ReplyDelete
  6. I am not sure whether only referring outlook dll to the application will do?
    I add the DLL reference and executed the application on my local PC where outlook is installed..It worked..But as soon as I deployed the same to hosted web server, it failed since the Outlook interop is looking for outlook instance on the hosted server..
    Any alternative??

    ReplyDelete