Introduction
Our clients often need to use their email contacts from their application(s), they believe in the good application that reduces their effort to go to other websites, they know that not everything can be implemented but not impossible, so here the requirement occurs to implement a section to import contacts from Gmail and send email to the selected email id from the ASP.Net application.
A good application is one that can complete all your work or most of your work from one browser window or from one application.
I tried here to describe "Import Gmail contacts in ASP.Net". I hope I explained it here better so that everyone encountering this issue can do this like I did.
Requirement
We all know that, to use a third party tool in ASP.Net, we need to add references for the relevant DLLs to the application.
To import Gmail contacts in the ASP.Net application we need to download "Google API setup" from the following link:
Download Google API setup
This link will download the following DLLs to your PC:
- Google.GData.Apps.dll
- Google.GData.Client.dll
- Google.GData.Contacts.dll
- Google.GData.Extensions.dll
When you have these DLLs on your PC we need to add these to our application.
In the Solution Explorer, right-click on the "References" node then select "Add Reference...". In the "Add Reference" window select the "Browse" tab. Browse to the location where the DLLS were put and select one of them then click "OK". Do the same for the other three.
Source Code
Add the following source code to create a design like I created:
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Import gmail contacts in to ASP.Net | Neha Sharma</title>
<style type="text/css">
.auto-style1 {
height: 26px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table align="center" style="height: 104px; width: 301px; background-color: #F0F0F0">
<tr>
<td>
Gmail ID</td>
<td>
<asp:TextBox ID="txtgmailusername" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style1">
Password</td>
<td class="auto-style1">
<asp:TextBox ID="txtpassword" runat="server" TextMode="Password"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="Button1" runat="server" Text="Get" OnClick="Button1_Click" />
</td>
<td>
</td>
</tr>
<tr>
<td colspan="2">
You have following contacts in Gmail ID</td>
</tr>
<tr>
<td colspan="2">
<asp:GridView ID="gridemails" runat="server">
<EmptyDataTemplate>
You dont have any contacts yet.
</EmptyDataTemplate>
</asp:GridView>
</td>
</tr>
</table>
</div>
<div>
<%--<asp:DropDownList ID="ddlemails" runat="server"></asp:DropDownList>--%>
</div>
</form>
</body>
</html>
Code behind
Add the following namespaces first:
using Google.GData.Contacts;
using Google.GData.Client;
using Google.GData.Extensions;
using Google.Contacts;
using System.Data;
using System.Data.SqlClient;
Now :
public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public DataSet GetGmailContacts(string p_name, string e_id, string psw)
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
DataColumn dc = new DataColumn();
dc.DataType = Type.GetType("System.String");
dc.ColumnName = "emailid";
dt.Columns.Add(dc);
RequestSettings rs = new RequestSettings(p_name, e_id, psw);
rs.AutoPaging = true;
ContactsRequest cr = new ContactsRequest(rs);
Feed<Contact> f = cr.GetContacts();
foreach (Contact t in f.Entries)
{
foreach (EMail email in t.Emails)
{
DataRow dr1 = dt.NewRow();
dr1["emailid"] = email.Address.ToString();
dt.Rows.Add(dr1);
}
}
ds.Tables.Add(dt);
return ds;
}
public void getcontacts()
{
DataSet ds = GetGmailContacts("MyNetwork Web Application!", txtgmailusername.Text, txtpassword.Text);
gridemails.DataSource = ds;
gridemails.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
getcontacts();
}
}
Save all the work and run the page.