用C#如何获得msn联系人

简介:

最近接受一项新的有挑战性的工作,那就是获取msn的联系人,并实现msn的操作。

 

 

 private XihSolutions.DotMSN.Messenger messenger = new Messenger();

        protected void Page_Load(object sender, EventArgs e)
        {

            // by default this example will emulate the official microsoft windows messenger client
            messenger.Credentials.ClientID = "msmsgs@msnmsgr.com ";
            messenger.Credentials.ClientCode = "Q1P7W2E4J9R8U3S5";

            // uncomment this to enable verbose output for debugging
            Settings.TraceSwitch.Level = System.Diagnostics.TraceLevel.Verbose;

            // set the events that we will handle
            // remember that the nameserver is the server that sends contact lists, notifies you of contact status changes, etc.
            // a switchboard server handles the individual conversation sessions.
            messenger.NameserverProcessor.ConnectionEstablished += new EventHandler(NameserverProcessor_ConnectionEstablished);
            messenger.Nameserver.SignedIn += new EventHandler(Nameserver_SignedIn);
            messenger.Nameserver.SignedOff += new SignedOffEventHandler(Nameserver_SignedOff);
            messenger.NameserverProcessor.ConnectingException += new ProcessorExceptionEventHandler(NameserverProcessor_ConnectingException);
            messenger.Nameserver.ExceptionOccurred += new HandlerExceptionEventHandler(Nameserver_ExceptionOccurred);
            messenger.Nameserver.AuthenticationError += new HandlerExceptionEventHandler(Nameserver_AuthenticationError);
            messenger.Nameserver.ServerErrorReceived += new ErrorReceivedEventHandler(Nameserver_ServerErrorReceived);
            messenger.ConversationCreated += new ConversationCreatedEventHandler(messenger_ConversationCreated);
            messenger.TransferInvitationReceived += new MSNSLPInvitationReceivedEventHandler(messenger_TransferInvitationReceived);
        }

        /// <summary>
        /// Asks the user to accept or deny the incoming filetransfer invitation.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void messenger_TransferInvitationReceived(object sender, MSNSLPInvitationEventArgs e)
        {
            //if (MessageBox.Show(
            //    messenger.ContactList[e.TransferProperties.RemoteContact].Name +
            //    " wants to send you a file./r/nFilename: " +
            //    e.Filename + "/r/nLength (bytes): " + e.FileSize,
            //    "Filetransfer invitation",
            //    MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            //{
            //    // by setting the Accept property in the EventArgs to true we give the transfer a green light    
            //    saveFileDialog.FileName = e.Filename;
            //    if ((DialogResult)Invoke(new ShowFileDialogDelegate(ShowFileDialog), new object[] { saveFileDialog }) == DialogResult.OK)
            //    {
            //        e.TransferSession.DataStream = new FileStream(saveFileDialog.FileName, FileMode.Create, FileAccess.Write);
            //        e.Accept = true;
            //    }
            //}
        }
        private void messenger_ConversationCreated(object sender, ConversationCreatedEventArgs e)
        {
            // check if the request is initiated remote or by this object
            // if it is initiated remote then we have to create a conversation form. Otherwise the
            // form is already created and we don't need to create another one.
            if (e.Initiator == null)
            {
                // use the invoke method to create the form in the main thread
             //   Control.Invoke(new CreateConversationDelegate(CreateConversationForm), new object[] { e.Conversation });
            }
        }
        /// <summary>
        /// Notifies the user of errors which are send by the MSN server.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Nameserver_ServerErrorReceived(object sender, MSNErrorEventArgs e)
        {
            // when the MSN server sends an error code we want to be notified.
          //  Response.Write(e.MSNError.ToString());
            SetStatus("Server error received");
        }
    
        private void Nameserver_AuthenticationError(object sender, ExceptionEventArgs e)
        {
           // Response.Write("Authentication failed, check your account or password.");
            SetStatus("Authentication failed");
        }
        private void Nameserver_ExceptionOccurred(object sender, ExceptionEventArgs e)
        {
            // ignore the unauthorized exception, since we're handling that error in another method.
            if (e.Exception is UnauthorizedException)
                return;

            //Response.Write(e.Exception.ToString());
        }

        private void NameserverProcessor_ConnectingException(object sender, ExceptionEventArgs e)
        {
           // string message=e.Exception.ToString();
            SetStatus("Connecting failed");
        }

        private void Nameserver_SignedOff(object sender, SignedOffEventArgs e)
        {
            SetStatus("Signed off from the messenger network");
        }
        private void Nameserver_SignedIn(object sender, EventArgs e)
        {
            SetStatus("Signed into the messenger network as " + messenger.Owner.Name);

            // set our presence status
            messenger.Owner.Status = PresenceStatus.Online;

            UpdateContactlist();
        }
        private void NameserverProcessor_ConnectionEstablished(object sender, EventArgs e)
        {
            SetStatus("Connected to server");
        }

        private delegate void UpdateContactlistDelegate();
        /// <summary>
        /// Updates the listview.
        /// </summary>
        private void UpdateContactlist()
        {
            if (messenger.Connected == false)
                return;
         
            //  ListView1.Items.Clear();
            ListBox1.Items.Clear();
            //messenger.ContactList.All.Current
            if (messenger.ContactList.All != null)
            {
                string costring = "";
                foreach (Contact contact in messenger.ContactList.All)
                {

                    ListItem item = new ListItem();
                    item.Text = contact.Name;
                 //   item.Value = contact.ToString();
                    ListBox1.Items.Add(item);
                    costring = costring + contact.Name.Trim();
                }
             Response.Write("<script>alert('"+costring+"');</script>");
                ListBox1.DataBind();

            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            //messenger.Credentials.ClientID = "msmsgs@msnmsgr.com ";
           // messenger.Credentials.ClientCode = "Q1P7W2E4J9R8U3S5";
            if (messenger.Connected)
            {
                messenger.Disconnect();
            }
            messenger.Credentials.Account = txtUserID.Text.Trim();
            messenger.Credentials.Password = txtPassWd.Text.Trim();
          
            // messenger.ConnectivitySettings.ProxyHost = "10.1.180.11";
             //messenger.ConnectivitySettings.ProxyPort =8080;
             //messenger.ConnectivitySettings.ProxyType = ProxyType.Socks5;
           //   messenger.ConnectivitySettings.ProxyUsername = 用户名
               // messenger.ConnectivitySettings.ProxyPassword =密码
             try
             {
                 messenger.Connect();
              //   Response.Write("<script>alert('成功');</script>");
             }
             catch (Exception message)
             {
                 //Response.Write(message);
             }
          
                // ListBox1.AppendDataBoundItems();
            
        }
        private delegate void SetStatusDelegate(string status);

        private void SetStatusSynchronized(string status)
        {
            //Response.Write(status);
        }

        private void SetStatus(string status)
        {
            SetStatusSynchronized(status);
        }

}

 

使用DotMSN可以很好地对msn控件进行操作。实现开心,校内通过msn获取联系人的功能。

目录
相关文章
|
数据库
C#&nbsp;对Outlook联系人的增、删、查&nbsp;
原文:C# 对Outlook联系人的增、删、查 [转] 注:定义变量 Outlook.Application myOlApp = new Outlook.ApplicationClass(); Outlook.
980 0
|
7月前
|
开发框架 前端开发 .NET
C#编程与Web开发
【4月更文挑战第21天】本文探讨了C#在Web开发中的应用,包括使用ASP.NET框架、MVC模式、Web API和Entity Framework。C#作为.NET框架的主要语言,结合这些工具,能创建动态、高效的Web应用。实际案例涉及企业级应用、电子商务和社交媒体平台。尽管面临竞争和挑战,但C#在Web开发领域的前景将持续拓展。
215 3
|
7月前
|
SQL 开发框架 安全
C#编程与多线程处理
【4月更文挑战第21天】探索C#多线程处理,提升程序性能与响应性。了解C#中的Thread、Task类及Async/Await关键字,掌握线程同步与安全,实践并发计算、网络服务及UI优化。跟随未来发展趋势,利用C#打造高效应用。
204 3
|
1月前
|
C# 开发者
C# 一分钟浅谈:Code Contracts 与契约编程
【10月更文挑战第26天】本文介绍了 C# 中的 Code Contracts,这是一个强大的工具,用于通过契约编程增强代码的健壮性和可维护性。文章从基本概念入手,详细讲解了前置条件、后置条件和对象不变量的使用方法,并通过具体代码示例进行了说明。同时,文章还探讨了常见的问题和易错点,如忘记启用静态检查、过度依赖契约和性能影响,并提供了相应的解决建议。希望读者能通过本文更好地理解和应用 Code Contracts。
35 3
|
1天前
|
存储 安全 编译器
学懂C#编程:属性(Property)的概念定义及使用详解
通过深入理解和使用C#的属性,可以编写更清晰、简洁和高效的代码,为开发高质量的应用程序奠定基础。
26 12
|
1月前
|
设计模式 C# 图形学
Unity 游戏引擎 C# 编程:一分钟浅谈
本文介绍了在 Unity 游戏开发中使用 C# 的基础知识和常见问题。从 `MonoBehavior` 类的基础用法,到变量和属性的管理,再到空引用异常、资源管理和性能优化等常见问题的解决方法。文章还探讨了单例模式、事件系统和数据持久化等高级话题,旨在帮助开发者避免常见错误,提升游戏开发效率。
51 4
|
3月前
|
API C#
C# 一分钟浅谈:文件系统编程
在软件开发中,文件系统操作至关重要。本文将带你快速掌握C#中文件系统编程的基础知识,涵盖基本概念、常见问题及解决方法。文章详细介绍了`System.IO`命名空间下的关键类库,并通过示例代码展示了路径处理、异常处理、并发访问等技巧,还提供了异步API和流压缩等高级技巧,帮助你写出更健壮的代码。
51 2
|
2月前
|
安全 C# 数据安全/隐私保护
实现C#编程文件夹加锁保护
【10月更文挑战第16天】本文介绍了两种用 C# 实现文件夹保护的方法:一是通过设置文件系统权限,阻止普通用户访问;二是使用加密技术,对文件夹中的文件进行加密,防止未授权访问。提供了示例代码和使用方法,适用于不同安全需求的场景。
138 0
|
3月前
|
安全 程序员 编译器
C#一分钟浅谈:泛型编程基础
在现代软件开发中,泛型编程是一项关键技能,它使开发者能够编写类型安全且可重用的代码。C# 自 2.0 版本起支持泛型编程,本文将从基础概念入手,逐步深入探讨 C# 中的泛型,并通过具体实例帮助理解常见问题及其解决方法。泛型通过类型参数替代具体类型,提高了代码复用性和类型安全性,减少了运行时性能开销。文章详细介绍了如何定义泛型类和方法,并讨论了常见的易错点及解决方案,帮助读者更好地掌握这一技术。
84 11
|
3月前
|
SQL 开发框架 安全
并发集合与任务并行库:C#中的高效编程实践
在现代软件开发中,多核处理器普及使多线程编程成为提升性能的关键。然而,传统同步模型在高并发下易引发死锁等问题。为此,.NET Framework引入了任务并行库(TPL)和并发集合,简化并发编程并增强代码可维护性。并发集合允许多线程安全访问,如`ConcurrentQueue&lt;T&gt;`和`ConcurrentDictionary&lt;TKey, TValue&gt;`,有效避免数据不一致。TPL则通过`Task`类实现异步操作,提高开发效率。正确使用这些工具可显著提升程序性能,但也需注意任务取消和异常处理等常见问题。
56 1