NO IMAGE

最近接受一項新的有挑戰性的工作,那就是獲取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 = “[email protected]
“;
            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 = “[email protected]
“;
           // 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獲取聯絡人的功能。