Tuesday, April 10, 2012

Open outlook in Asp.net(C#) only in 3 steps


Open outlook in Asp.net(C#) only in 3 steps-:
Step 1 :  At first  add the namespace for outlook  and if its not showing in intellisense than add reference for Microsoft.Outlook
using Microsoft.Office.Interop.Outlook;
Step 2 : Bind Data in Grid Which contains Mail Address.Keep Mail Id in Template field in this format

<asp:TemplateField HeaderText="Email" SortExpression="Email">
<ItemTemplate>
<asp:LinkButton ID="lbSendMail" runat="server"  CommandName="SendMail"
CommandArgument='<%# Bind("Email") %>' Text='<%# Bind("Email") %>' ></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>

Step 3 : Fire the RowCommand Event of GridView and Write down the Code Given Below.

if (e.CommandName == "SendMail")
{
   Microsoft.Office.Interop.Outlook.Application oApp = new                Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.MailItem oMailItem = (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem(OlItemType.olMailItem);
  oMailItem.To = e.CommandArgument.ToString();          
  //Similary U can Add another Detail like body, bcc etc...
  oMailItem.Display(true);
}

Now Run Your Programme and output will be like this :
After Clicking on Any mail Id a new window popup like this one :

Note : To get proper output you have to configure Outlook in your System.


And Finally Here is the Source Code :



3 comments:

  1. i get the fellowing message
    CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80080005 Server execution failed (Exception from HRESULT: 0x80080005 (CO_E_SERVER_EXEC_FAILURE)).

    ReplyDelete
  2. Hi
    I have the same problem as melaouhia mohamed amine.
    Getting this message in IIS when trying to browse may page.
    IIS dont have access rights to Outlook... any ideas...

    ReplyDelete