Asp.net implements HotMail free email to get contacts

Source: Internet
Author: User
Tags sleep nameserver
The code is as follows: Copy code

Using System;
Using System. Collections;
Using System. Collections. Specialized;
Using System. Net;
Using System. Text;
Using System. Text. RegularExpressions;
Using System. IO;
Using System. Security. Cryptography. X509Certificates;
Using System. Xml;
Using System. Web;
Using System. Data;
Using System. Collections. Generic;
Using System. Diagnostics;
Using System. Security. Permissions;
Using MSNPSharp;
Using MSNPSharp. Core;
Using MSNPSharp. DataTransfer;
Using MSNPSharp. MSNWS. MSNABSharingService;
Using System. Threading;
Using System. ComponentModel;
Using System. Web. UI;

/*
* AddressHotMail
* HotMail free email to get contacts
 *
* Change? Performance? S
* Ver 1.00.00 @ [20100721] Xingyuan Mengyu Xin?
* Ver 2.00.00 @ [20110807] Xingyuan Mengyu upgrade
*/
Namespace GetEmailAddress
{
/// <Summary>
/// AddressHotMail abstract description.
/// </Summary>
Public class AddressHotMail
    {
Messenger messenger = new Messenger ();

AutoResetEvent are; // multithread synchronous signal

# Region read hotmail address book
Private int tmpCount = 1;

/// <Summary>
/// Read the email address book
/// </Summary>
/// <Param name = "username"> </param>
/// <Param name = "password"> </param>
/// <Param name = "IsPageAsync"> </param>
/// <Returns> </returns>
Public GetAddressStatus GetAddressList (string username, string password, bool IsPageAsync)
        {

Login (username, password, IsPageAsync );

Return messenger. Nameserver. IsSignedIn = true? GetAddressStatus. Success: GetAddressStatus. NetError;

        }

/// <Summary>
/// Login email
/// </Summary>
/// <Param name = "username"> </param>
/// <Param name = "password"> </param>
/// <Param name = "IsPageAsync"> </param>
/// <Returns> </returns>
Private void Login (string username, string password, bool IsPageAsync)
        {
If (IsPageAsync)
            {
System. Threading. SynchronizationContext context = System. ComponentModel. AsyncOperationManager. SynchronizationContext;

Try
                {
System. ComponentModel. AsyncOperationManager. SynchronizationContext = new System. Threading. SynchronizationContext ();

Messenger. Credentials = new Credentials (username, password, (MsnProtocol) Enum. Parse (typeof (MsnProtocol), MsnProtocol. MSNP18.ToString ()));

Messenger. Connect ();

Thread. Sleep (3000); // Thread Sleep for 2 seconds

While (tmpCount <6) // reconnect 5 times
                    {
If (! Messenger. Connected | messenger. Nameserver. IsSignedIn)
                        {
TmpCount ++;

This. Login (username, password, IsPageAsync );
                        }
                    }

                }
Finally
                {
System. ComponentModel. AsyncOperationManager. SynchronizationContext = context;
                }
            }

        }

/// <Summary>
/// Read the email address book list
/// </Summary>
/// <Returns> </returns>
Public List <Person> GetContactList ()
        {
List <Person> tmpList = new List <Person> ();

Dictionary <int, Contact> dic = messenger. Nameserver. ContactList;

Foreach (KeyValuePair <int, Contact> tmp in dic)
            {
String tmpName = string. IsNullOrEmpty (tmp. Value. NickName) = true? Tmp. Value. Name: tmp. Value. NickName;

Person personeEntity = new Person ();
If (! String. IsNullOrEmpty (tmpName ))
                {
PersoneEntity. Name = tmpName;
                }
Else
                {
PersoneEntity. Name = "no Name ";
                }
PersoneEntity. Email = tmp. Value. Mail;

TmpList. Add (personeEntity );

            }

Return tmpList;
        }
# Endregion

# Region asynchronous reading

Private Page _ workingPage;

Public AddressHotMail (Page workingPage)
        {
_ WorkingPage = workingPage;
        }

Private GetAddressStatus _ addressStatus = GetAddressStatus. UidOrPwdError;

/// <Summary>
/// Read the email address book
/// </Summary>
/// <Param name = "username"> </param>
/// <Param name = "password"> </param>
/// <Param name = "IsPageAsync"> </param>
/// <Returns> </returns>
Public GetAddressStatus GetAddressList (string username, string password)
        {

Login (username, password );

Return _ addressStatus;

        }

/// <Summary>
/// Log on to MSN
/// </Summary>
/// <Param name = "username"> </param>
/// <Param name = "password"> </param>
Private void Login (string username, string password)
        {
Are = new AutoResetEvent (false );

SynchronizationContext context = AsyncOperationManager. SynchronizationContext;

Try
            {
AsyncOperationManager. SynchronizationContext = new SynchronizationContext ();

Messenger. Nameserver. SignedIn + = new EventHandler <EventArgs> (Nameserver_SignedIn );

Messenger. NameserverProcessor. ConnectingException + = new EventHandler <ExceptionEventArgs> (NameserverProcessor_ConnectingException );

Messenger. Nameserver. ExceptionOccurred + = new EventHandler <ExceptionEventArgs> (Nameserver_ExceptionOccurred );

Messenger. Nameserver. AuthenticationError + = new EventHandler <ExceptionEventArgs> (Nameserver_AuthenticationError );

If (messenger. Connected)
                {
Messenger. Disconnect ();
                }

Messenger. Credentials = new Credentials (username, password, (MsnProtocol) Enum. Parse (typeof (MsnProtocol), MsnProtocol. MSNP18.ToString ()));

Messenger. Connect ();

Are. WaitOne (); // UI thread waiting

            }
Finally
            {
AsyncOperationManager. SynchronizationContext = context;
            }
        }

Private void Nameserver_SignedIn (object sender, EventArgs e)
        {
Are. Set (); // notifies the UI thread to continue working ..
// Post-login operations
_ AddressStatus = GetAddressStatus. Success;
        }

Private void Nameserver_ExceptionOccurred (object sender, ExceptionEventArgs e)
        {
Are. Set ();
// Ignore the unauthorized exception, since we're handling that error in another method.
If (e. Exception is UnauthorizedException)
Return;
_ AddressStatus = GetAddressStatus. NotAuspice;
        }

Private void NameserverProcessor_ConnectingException (object sender, ExceptionEventArgs e)
        {
Are. Set ();

_ AddressStatus = GetAddressStatus. MailError;

        }

Private void Nameserver_AuthenticationError (object sender, ExceptionEventArgs e)
        {
Are. Set ();

_ AddressStatus = GetAddressStatus. UidOrPwdError;

        }
# Endregion

    }
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.