asp.net實現HotMail免費郵箱擷取連絡人

來源:互聯網
上載者:User
 代碼如下 複製代碼

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
{
 /// <summary>
 /// AddressHotMail 的摘要說明。
 /// </summary>
    public class AddressHotMail
    {
        Messenger messenger = new Messenger();

        AutoResetEvent are;//多線程同步訊號

        #region 讀取hotmail郵箱通訊錄
        private int tmpCount = 1;

        /// <summary>
        /// 讀取郵箱通訊錄
        /// </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>
        /// 登陸郵箱
        /// </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);           //線程休眠2秒

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

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

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

        }

        /// <summary>
        /// 讀取郵箱通訊錄列表
        /// </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 = "暫無名稱";
                }
                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;

        /// <summary>
        /// 讀取郵箱通訊錄
        /// </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>
        /// 登陸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線程等待

            }
            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

    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.