当前位置:编程学习 > C#/ASP.NET >>

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

 代码如下 复制代码

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

    }
}

补充:asp.net教程,.Net开发
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,