none
vb.net 收gmail信件,附加檔案中文名稱變成亂碼 RRS feed

  • 問題

  • 收件匣的網頁編碼是UTF-8

    gmail的郵件可以點選"顯示原始郵件",編碼為Big5

    原始郵件內容會跟用vb.net取得郵件的內容一模一樣

    若郵件附檔名是中文就會變成亂碼,如下:

    filename="=?Big5?B?qtGyvL96v+8ueGxz?="

    很疑惑,vb.net的編碼預設不是Unicode嗎??

    程式該如何做更改呢??

    以下是我的收件程式

    Dim mailclient As TcpClient 
    Dim response As String 
    Dim NetStrm As SslStream 
    Dim sr As StreamReader 
    Dim sw As StreamWriter 
    Dim ns As NetworkStream 
    Dim totmessages As Integer 
    Dim i,j As Integer 
    Dim attachment As String 
    Dim data As String() 
    	 
    mailclient = New TcpClient("pop.gmail.com", 995) 
    NetStrm = New SslStream(mailclient.GetStream()) 
     
    NetStrm.AuthenticateAsClient("pop.gmail.com") 
     
    sr = New StreamReader(NetStrm) 
    sw = New StreamWriter(NetStrm) 
    response = sr.ReadLine() 
     
    sw.WriteLine("user " + "帳號") 
    sw.Flush() 
    	 
    response = sr.ReadLine() 
    	 
    If response.Substring(0, 4) = "-ERR" Then 
      Return 
    end if  
     
    sw.WriteLine("pass " + "密碼") 
    sw.Flush() 
     
    If response.Substring(0, 3) = "-ERR" Then 
      Return 
    end if  
    	 
    sw.WriteLine("stat") 
    sw.Flush() 
    response = sr.ReadLine() 
     
    Dim nummess As String() = response.Split(" "c) 
    totmessages = Convert.ToInt16(nummess(1)) 
    	 
    For i = 1 To totmessages 
        msg = Nothing 
        sw.WriteLine("top " & i & " 0") 
        sw.Flush() 
        response = sr.ReadLine() 
     
        While True 
            response = sr.ReadLine() 
            If response = "." Then 
                Exit While 
            End If 
            msg = msg + response + vbCr & vbLf 
        End While 
     
        attachment = Nothing 
        If Regex.Match(msg, "multipart/mixed").Success Then 
            msg = Nothing 
            sw.WriteLine("retr" & i & " 0") 
            sw.Flush() 
            response = sr.ReadLine() 
    	 
            While True 
                response = sr.ReadLine() 
                If response = "." Then 
                    Exit While 
                End If 
                msg = msg + response + vbCr & vbLf 
            End While 
     
            data = msg.Split(ControlChars.Cr) 
            startindex = 0 
            index = 0 
            lastindex = 0 
     
            While (Not Regex.Match(data(index).Trim(), "filename").Success) 'And (index) 
                index = index + 1 
            End While 
     
            If index = data.Length - 1 Then 
                Return 
            End If 
     
            filename =   data(index).Trim().Substring(42).Replace("""", "") 
     
            'find start of attachment data 
            index = index + 1 
            While (data(index).Length <> 1) And (index) 
                index = index + 1 
            End While 
    	 
            If index = data.Length - 1 Then 
                Return 
            End If 
    	 
            startindex = index + 1 
    	 
            'find end of data 
            index = startindex + 1 
    	 
            While (Not Regex.Match(data(index).Trim(), "--0").Success) And (data(index).Length <> 1) And (index < data.Length - 1) 
                index =index + 1 
            End While 
            lastindex = index - 1 
     
            For j As Integer = startindex To lastindex 
                attachment = attachment + data(j) 
            Next 
            attachment = attachment & vbCr & vbLf 
        end if  
    next i  

    2013年1月3日 上午 07:52

解答

所有回覆