asp.net实现HotMail免费邮箱获取联系人

作者:袖梨 2022-06-25
 代码如下 复制代码

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免费邮箱获取联系人
 *
 *  改?履?s
 * ver 1.00.00 @[20100721]  星缘梦雨 新?
 * ver 2.00.00 @[20110807]  星缘梦雨 升级 
 */
namespace GetEmailAddress
{
 ///


 /// AddressHotMail 的摘要说明。
 ///

    public class AddressHotMail
    {
        Messenger messenger = new Messenger();

        AutoResetEvent are;//多线程同步信号

        #region 读取hotmail邮箱通讯录
        private int tmpCount = 1;

        ///


        /// 读取邮箱通讯录
        ///

        ///
        ///
        ///
        ///
        public GetAddressStatus GetAddressList(string username, string password, bool IsPageAsync)
        {

            Login(username, password, IsPageAsync);

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

        }

        ///


        /// 登陆邮箱
        ///

        ///
        ///
        ///
        ///
        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);           //线程休眠2秒

                    while (tmpCount < 6)          //重连5次
                    {
                        if (!messenger.Connected||messenger.Nameserver.IsSignedIn)
                        {
                            tmpCount++;

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

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

        }

        ///


        /// 读取邮箱通讯录列表
        ///

        ///
        public List GetContactList()
        {
            List tmpList = new List();

            Dictionary dic = messenger.Nameserver.ContactList;

            foreach (KeyValuePair 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 = "暂无名称";
                }
                personeEntity.Email = tmp.Value.Mail;

                tmpList.Add(personeEntity);

            }

            return tmpList;
        }
        #endregion

        #region 异步读取

         private Page _workingPage;

         public AddressHotMail(Page workingPage)
        {
            _workingPage = workingPage;
        }

         private GetAddressStatus _addressStatus = GetAddressStatus.UidOrPwdError;

        ///


        /// 读取邮箱通讯录
        ///

        ///
        ///
        ///
        ///
        public GetAddressStatus GetAddressList(string username, string password)
        {

            Login(username, password);

            return _addressStatus;

        }

        ///


        /// 登陆MSN
        ///

        ///
        ///
        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(Nameserver_SignedIn);

                messenger.NameserverProcessor.ConnectingException += new EventHandler(NameserverProcessor_ConnectingException);

                messenger.Nameserver.ExceptionOccurred += new EventHandler(Nameserver_ExceptionOccurred);

                messenger.Nameserver.AuthenticationError += new EventHandler(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线程等待

            }
            finally
            {
                AsyncOperationManager.SynchronizationContext = context;
            }
        }

        private void Nameserver_SignedIn(object sender, EventArgs e)
        {
            are.Set();//通知UI线程可以继续干活了。。
            //登陆后的操作
            _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

    }
}

相关文章

精彩推荐