Gmail क्लाइंट का उपयोग करके Google संपर्क प्रबंधित करें

Aspose.Email Gmail कॉन्टेक्ट्स के साथ काम करने को समर्थन देता है। इसका उपयोग करके IGmailClient इंटरफ़ेस, उपयोगकर्ता Gmail खाते से संपर्क प्राप्त कर सकते हैं, नए संपर्क बना सकते हैं, और मौजूदा संपर्क अपडेट और हटाया जा सकता है। Gmail अपने सार्वजनिक डेवलपर API के माध्यम से इन सभी कार्यों को करने की अनुमति देता है। Gmail संपर्कों के साथ कार्य करने के लिए निम्नलिखित उपयोगकर्ता जानकारी आवश्यक है: उपयोगकर्ता नाम, ईमेल पता, पासवर्ड, क्लाइंट आईडी, क्लाइंट सीक्रेट रीफ़्रेश टोकन। यह लेख दिखाता है कैसे:

Gmail कॉन्टेक्ट्स तक पहुंच

निम्नलिखित एक नमूना अनुप्रयोग है जिसका उपयोग सभी समूहों में संपर्क विवरण तक पहुँचने के लिए किया जा सकता है।

GoogleTestUser User2 = new GoogleTestUser("user", "email address", "password", "clientId", "client secret");
string accessToken;
string refreshToken;
GoogleOAuthHelper.GetAccessToken(User2, out accessToken, out refreshToken);

// Get IGmailclient
using (IGmailClient client = GmailClient.GetInstance(accessToken, User2.EMail))
{
    Contact[] contacts = client.GetAllContacts();
    foreach (Contact contact in contacts)
        Console.WriteLine(contact.DisplayName + ", " + contact.EmailAddresses[0]);

    // Fetch contacts from a specific group
    ContactGroupCollection groups = client.GetAllGroups();
    GoogleContactGroup group = null;
    foreach (GoogleContactGroup g in groups)
        switch (g.Title)
        {
            case "TestGroup": group = g;
                break;
        }

    // Retrieve contacts from the Group
    if (group != null)
    {
        Contact[] contacts2 = client.GetContactsFromGroup(group.Id);
        foreach (Contact con in contacts2)
            Console.WriteLine(con.DisplayName + "," + con.EmailAddresses[0].ToString());
    }
}

Google संपर्क बनाएं

निम्नलिखित कोड स्निपेट दिखाता है कि Gmail में संपर्क कैसे बनाया जाता है।

GoogleTestUser User2 = new GoogleTestUser("user", "email address", "password", "clientId", "client secret");
string accessToken;
string refreshToken;
GoogleOAuthHelper.GetAccessToken(User2, out accessToken, out refreshToken);

// Gmail Client
IGmailClient client = GmailClient.GetInstance(accessToken, User2.EMail);

// Create a Contact
Contact contact = new Contact();
contact.Prefix = "Prefix";
contact.GivenName = "GivenName";
contact.Surname = "Surname";
contact.MiddleName = "MiddleName";
contact.DisplayName = "Test User 1";
contact.Suffix = "Suffix";
contact.JobTitle = "JobTitle";
contact.DepartmentName = "DepartmentName";
contact.CompanyName = "CompanyName";
contact.Profession = "Profession";
contact.Notes = "Notes";
PostalAddress address = new PostalAddress();
address.Category = PostalAddressCategory.Work;
address.Address = "Address";
address.Street = "Street";
address.PostOfficeBox = "PostOfficeBox";
address.City = "City";
address.StateOrProvince = "StateOrProvince";
address.PostalCode = "PostalCode";
address.Country = "Country";
contact.PhysicalAddresses.Add(address);
PhoneNumber pnWork = new PhoneNumber();
pnWork.Number = "323423423423";
pnWork.Category = PhoneNumberCategory.Work;
contact.PhoneNumbers.Add(pnWork);
PhoneNumber pnHome = new PhoneNumber();
pnHome.Number = "323423423423";
pnHome.Category = PhoneNumberCategory.Home;
contact.PhoneNumbers.Add(pnHome);
PhoneNumber pnMobile = new PhoneNumber();
pnMobile.Number = "323423423423";
pnMobile.Category = PhoneNumberCategory.Mobile;
contact.PhoneNumbers.Add(pnMobile);
contact.Urls.Blog = "Blog.ru";
contact.Urls.BusinessHomePage = "BusinessHomePage.ru";
contact.Urls.HomePage = "HomePage.ru";
contact.Urls.Profile = "Profile.ru";
contact.Events.Birthday = DateTime.Now.AddYears(-30);
contact.Events.Anniversary = DateTime.Now.AddYears(-10);
contact.InstantMessengers.AIM = "AIM";
contact.InstantMessengers.GoogleTalk = "GoogleTalk";
contact.InstantMessengers.ICQ = "ICQ";
contact.InstantMessengers.Jabber = "Jabber";
contact.InstantMessengers.MSN = "MSN";
contact.InstantMessengers.QQ = "QQ";
contact.InstantMessengers.Skype = "Skype";
contact.InstantMessengers.Yahoo = "Yahoo";
contact.AssociatedPersons.Spouse = "Spouse";
contact.AssociatedPersons.Sister = "Sister";
contact.AssociatedPersons.Relative = "Relative";
contact.AssociatedPersons.ReferredBy = "ReferredBy";
contact.AssociatedPersons.Partner = "Partner";
contact.AssociatedPersons.Parent = "Parent";
contact.AssociatedPersons.Mother = "Mother";
contact.AssociatedPersons.Manager = "Manager";

// Email Address
EmailAddress eAddress = new EmailAddress();
eAddress.Address = "email@gmail.com";
contact.EmailAddresses.Add(eAddress);
string contactUri = client.CreateContact(contact);

Google संपर्क अपडेट करें

एक बार संपर्क प्राप्त हो जाने पर, उसके गुण अपडेट किए जा सकते हैं और संपर्क को Gmail खाते में वापस सहेजा जा सकता है। निम्नलिखित कोड स्निपेट दिखाता है कि Gmail खाते से संपर्क कैसे प्राप्त किए जाएँ और फिर उनमें से एक को संशोधित कर वापस सहेजा जाए।

GoogleTestUser User2 = new GoogleTestUser("user", "email address", "password", "clientId", "client secret");
string accessToken;
string refreshToken;
GoogleOAuthHelper.GetAccessToken(User2, out accessToken, out refreshToken);

// Get IGmailclient
using (IGmailClient client = GmailClient.GetInstance(accessToken, User2.EMail))
{
    Contact[] contacts = client.GetAllContacts();
    Contact contact = contacts[0];
    contact.JobTitle = "Manager IT";
    contact.DepartmentName = "Customer Support";
    contact.CompanyName = "Aspose";
    contact.Profession = "Software Developer";
    client.UpdateContact(contact);
}

Google संपर्क हटाएँ

Gmail संपर्क हटाने के लिए, Gmail क्लाइंट DeleteContact विधि का उपयोग नीचे दिए गए नमूना स्निपेट में दिखाए अनुसार किया जाता है।

client.DeleteContact(contact.Id.GoogleId);

Google संपर्क सहेजें

Aspose.Email आपको संपर्कों को विभिन्न आउटपुट स्वरूपों जैसे MSG और VCF में सहेजने की अनुमति देता है। सहेजें विधि इस क्षमता को प्रदान करती है। निम्नलिखित कोड स्निपेट दिखाता है कि कैसे एक संपर्क सहेजा जाता है।

contact.Save(dataDir + "contact_out.msg", ContactSaveFormat.Msg);
contact.Save(dataDir + "contact_out.vcf", ContactSaveFormat.VCard);