Jabber-net发送消息(VB.NET)

时间:2012-05-14 08:06:59

标签: asp.net xmpp vb.net-2010 sendmessage google-talk

我创建了测试项目,使用Jabber库通过google talk发送消息。由于我已经有了可以使用agsXMPP成功发送消息的测试项目,我想模仿这个项目来改用jabber库。但是,即使代码运行传递发送消息命令没有任何错误,也没有发送消息。似乎没有检查密码,因为它没有输入OnAuthError事件。

我的测试项目是使用VB.NET语言的ASP.NET Web应用程序项目。有4个文本框可供填写:发件人帐户(txt_Sender),发件人密码(txt_Password),要发送的邮件(txt_Message)和收件人帐户(txt_Receiver)以及1个用于发送邮件的按钮(btn_Send)。我使用我的电子邮件帐户(xxx1@gmail.com)测试并发送消息给我的朋友(xxx2@gmail.com)。这是我的VB代码

Imports jabber
Imports jabber.client
Imports Microsoft.Win32
Imports System.Threading
Imports jabber.protocol.client
Imports jabber.connection

Public Class TestSendMsg
    Inherits System.Web.UI.Page
    Public done As ManualResetEvent
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        done = New ManualResetEvent(False)
    End Sub

    Private Sub btn_Send_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_Send.Click
        Dim jcSender As JabberClient = New JabberClient()
        Dim jidSender As New jabber.JID(txt_Sender.Text.Trim)
        With jcSender
            .User = jidSender.User
            .Password = txt_Password.Text.Trim
            .Server = jidSender.Server
            .AutoReconnect = True
            .AutoRoster = True
        End With
        With jcSender
            Try
                AddHandler .OnAuthenticate, New bedrock.ObjectHandler(AddressOf j_OnAuthenticate)
                'AddHandler .OnAuthenticate, AddressOf j_OnAuthenticate
                AddHandler .OnPresence, AddressOf j_OnPresence
                AddHandler .OnBeforePresenceOut, AddressOf j_OnBeforePresenceOut
                AddHandler .OnAuthError, AddressOf j_OnAuthError
                AddHandler .OnAfterPresenceOut, AddressOf j_OnAfterPresenceOut
                .Connect()
                .Login()
                .IsAuthenticated = True
                .Message(txt_Reciever.Text.Trim, txt_Message.Text.Trim)
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
            .Close()
            .Dispose()
        End With
    End Sub

Private Sub j_OnAfterPresenceOut(ByVal sender As Object, ByVal pres As Presence)
    'Dim j As JabberClient = CType(sender, JabberClient)
    'j.Message(TARGET, "Registered: " & iq.BaseURI)
    'done.Set()
End Sub

Private Sub j_OnAuthError(ByVal sender As Object, ByVal pres As Presence)
    'Dim j As JabberClient = CType(sender, JabberClient)
    'j.Message(TARGET, "Registered: " & iq.BaseURI)
    'done.Set()
End Sub

Private Sub j_OnBeforePresenceOut(ByVal sender As Object, ByVal pres As Presence)
    'Dim j As JabberClient = CType(sender, JabberClient)
    'j.Message(TARGET, "Registered: " & iq.BaseURI)
    'done.Set()
End Sub

Private Sub j_OnPresence(ByVal sender As Object, ByVal pres As Presence)
    'Dim j As JabberClient = CType(sender, JabberClient)
    'j.Message(TARGET, "Presence: " & pres.BaseURI)
    'done.Set()
End Sub

Private Sub j_OnAuthenticate(ByVal sender As Object)
    ' Sender is always the JabberClient.
    Dim j As JabberClient = CType(sender, JabberClient)
    j.Message(txt_Reciever.Text.Trim, "Test OnAuthenticate")
    ' Finished sending.  Shut down.
    done.Set()
End Sub

End Class

1 个答案:

答案 0 :(得分:1)

在发送消息之前,您需要等待OnAuthenticate。