Tuesday 5 March 2013

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


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 :
 

No comments:

Post a Comment