How to import Gmail Contacts in asp.net
To import the Gmail contacts using asp.net C#/VB, Below code is complete, you have to download the required dll and then copy paste that dll's into bin folder.
Click here to download dll
ImportGmailContacts.aspx
<div id="ListingPage">
<h1>
Import GMail Contacts</h1>
<table width="95%" border="0">
<tr>
<td colspan="2" align="center" style="padding-left:2px;">
<span class="warning">
<asp:Literal ID="litMessage" runat="server"></asp:Literal></span>
</td>
</tr>
<tr>
<td class="style1" valign="top" align="right" width="20%">
Username :</td>
<td align="left" width="20%">
<asp:TextBox ID="txtgmailusername" CssClass="inputBox" Width="180px" MaxLength="100" runat="server" ></asp:TextBox><br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" CssClass="warning"
ControlToValidate="txtgmailusername" ErrorMessage="*" Text="Please enter Username" ValidationGroup="a"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="style1" valign="top" align="right" width="20%">
Password :</td>
<td align="left" width="20%">
<asp:TextBox ID="txtpassword" CssClass="inputBox" TextMode="Password" Width="180px" MaxLength="40" runat="server" ></asp:TextBox><br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" CssClass="warning"
ControlToValidate="txtpassword" ErrorMessage="Group Name" Text="Please enter Password" ValidationGroup="a"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td width="25%" align="left" colspan="2" style="padding-left:130px;">
<asp:Button ID="btnAddtoFav" runat="server" CssClass="btnSubmitSimple" ValidationGroup="a"
OnClick="Button1_Click" AlternateText=" " Height="28" BorderWidth="0" />
<asp:HiddenField ID="hdnId" runat="server" />
</td>
</tr>
</table>
</div>
</div>
ImportGmailContacts.aspx.cs
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Google.GData.Contacts;
using Google.GData.Client;
using Google.GData.Extensions;
using Google.Contacts;
namespace MBP
{
public partial class GmailContacts : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
DataColumn C2 = new DataColumn();
C2.DataType = Type.GetType("System.String");
C2.ColumnName = "EmailID";
dt.Columns.Add(C2);
DataColumn C3 = new DataColumn();
C3.DataType = Type.GetType("System.String");
C3.ColumnName = "title";
dt.Columns.Add(C3);
RequestSettings rs = new RequestSettings("MyBridalPlanner.com", txtgmailusername.Text, txtpassword.Text);
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();
dr1["title"] = t.Title.ToString();
dt.Rows.Add(dr1);
}
}
ds.Tables.Add(dt);
if (ds != null)
{
if (ds.Tables[0].Rows.Count > 0)
{
DataTable dt1 = ds.Tables[0];
if (dt1 != null)
{
foreach (DataRow dr in dt1.Rows)
{
MBP_GuestList guest = GuestMgmtClass.GetData(0);
bool isNew = guest == null;
if (guest == null)
{
guest = new MBP_GuestList();
guest.added = DateTime.Now;
}
else
{
guest.updated = DateTime.Now;
}
guest.Email = dr["EmailID"].ToString();
if (!GuestMgmtClass.DuplicateGuestByEmail(guest.Email, Convert.ToInt32(User.Identity.Name)))
{
string name = dr["title"].ToString();
string FName = string.Empty;
string LName = string.Empty;
if (name.Length > 0)
{
string[] namestr = name.Split(' ');
if (namestr.Length > 0)
FName = namestr[0].ToString();
if (namestr.Length > 1)
LName = namestr[1].ToString();
}
guest.FirstName = FName;
guest.LastName = LName;
guest.Relation = "";
guest.Phone = "";
guest.address = "";
guest.IsActive = true;
guest.RSVP = false;
guest.Memberid = Convert.ToInt32(User.Identity.Name);
guest = GuestMgmtClass.SetData(guest);
}
}
}
litMessage.Text = "Contacts imported successfully.";
}
}
}
catch (Exception ex)
{
if (ex.Message.ToLower().Contains("invalid"))
{
litMessage.Text = ex.Message;
}
else
{
litMessage.Text = "Invalid Credentials.";
}
}
}
}
}
Related Alrticles