如何在VB.net中使用IMAP命令从Gmail.com服务器获取响应?

时间:2012-08-25 22:25:11

标签: vb.net email imap

我正在使用Visual Basic 2010专业版,我正在开发一个软件,其中一部分是获取[收件箱]电子邮件,但我甚至无法从服务器获得响应。

我做了以下步骤:

  1. Made sure that my Gmail.com account is working with IMAP
  2. 我正在使用两步验证方法,因此我正在使用我的应用程序密码。
  3. 我正在使用此代码:

        Imports System.Net.Sockets
        Imports System.IO
        Imports System.Text        
        Imports System.Net.Security
    
        Public Class ImapClient1 
    
        Dim ServerNm As String 
        Dim UsrNm As String
        Dim PassStr As String
        Dim _IntPort As Integer
        Dim ImapClient As New Net.Sockets.TcpClient
        Dim NetworkS_stream As NetworkStream
        Dim m_sslStream As SslStream
        Dim Read_Stream As StreamReader
        Dim StatResp As String
    
        Function Login(ByVal Sstream As SslStream, ByVal Server_Command As String)
        ImapClient = New TcpClient(ServerNm, _IntPort)
        NetworkS_stream = ImapClient.GetStream 'Read the stream
        Sstream = New SslStream(NetworkS_stream)
        Dim Read_Stream2 = New StreamReader(Sstream)
        Server_Command = Server_Command ' + vbCrLf
        m_buffer = System.Text.Encoding.ASCII.GetBytes(Server_Command.ToCharArray())
        Sstream.Write(m_buffer, 0, m_buffer.Length)
        Dim Server_Reponse As String
        Server_Reponse = Read_Stream2.ReadLine()
        Return Server_Reponse
    
    End Function
    
  4. 当我使用带有[按钮]的登录功能时:

            ListBox1.Items.Clear()
            StatResp = Login(m_sslStream, "LOGIN " + UsrNm + " " + PassStr + " ") & vbCrLf
            ListBox1.Items.Add(StatResp)
    

    我在[ListBox]控件中什么都没有。

1 个答案:

答案 0 :(得分:0)

每个IMAP命令都需要在其前面加上一个“Tag”,这是一个需要生成的随机字母数字字符串。通常,登录过程如下所示:

A001 LOGIN user password
A002 SELECT INBOX
A003 ...
...
A010 LOGOUT