Γεια σας,
κάνω retrieve κάποια gmails με χρήση pop.
Ακολουθεί ο κώδικας:
Private Function Retrieve_Mail()
Dim oFile As System.IO.File
Dim oWrite As System.IO.StreamWriter
Dim tcpClient As New TcpClient
tcpClient.Connect(server_inc, port_inc)
Dim sslstream As SslStream = New SslStream(tcpClient.GetStream())
sslstream.AuthenticateAsClient(server_inc)
Dim sw As System.IO.StreamWriter = New StreamWriter(sslstream)
Dim reader As System.IO.StreamReader = New StreamReader(sslstream)
sw.WriteLine("USER " & username)
sw.Flush()
sw.WriteLine("PASS " & password)
sw.Flush()
sw.WriteLine("RETR 1")
sw.Flush()
sw.WriteLine("Quit ")
sw.Flush()
Dim str As String = ""
Dim strTemp As String = ""
While strTemp = reader.ReadLine() <> Nothing
If (strTemp = ".") Then
Exit While
End If
If strTemp.IndexOf("-ERR") <> -1 Then
Exit While
End If
str += strTemp
End While
Dim message As String = reader.ReadToEnd
Dim lines2() As String = Split(message, vbCrLf)
Dim full As String = ""
Dim i As Integer = 0
Dim mfrom, msub, mdate As String
Dim dateTimeInfo As DateTime = DateTime.Now
Dim mssn As String = dateTimeInfo.ToString("ddMMyyyyhhmmss") & "_" & noumero & ".txt"
oWrite = oFile.CreateText(Application.StartupPath & "/accounts/" & yourmail & "/" & mssn)
Dim inbody As Boolean = False
Dim context As Boolean = False
For Each line As String In lines2
If i > 1 And line <> "+OK Farewell" Then
If line = "" Then
inbody = True
End If
If line.StartsWith("From:") Then
mfrom = line.Replace("From:", "")
ElseIf line.StartsWith("Date: ") Then
mdate = line.Replace("Date: ", "")
ElseIf line.StartsWith("Subject: ") Then
msub = line.Replace("Subject: ", "")
ElseIf inbody Then
oWrite.WriteLine(line)
End If
End If
i += 1
Next
SQLConn = New SqlConnection(SQLString)
SQLCmnd = New SqlCommand("INSERT Mails (MSSN, MFROM, MSUB, MDATE) VALUES ('" & mssn &"', '" & mfrom & "', '" & msub & "', '" & mdate & "')", SQLConn)
SQLConn.Open()
SQLCmnd.ExecuteNonQuery()
SQLConn.Close()
oWrite.Close()
Return 0
End Function
Χρησιμοποιώ τον παραπάνω κώδικα και για να ξεχωρίζω το body απτο header. Το θέμα είναι όταν το mail μου είναι διαφορετικής κωδικοποίησης από Unicode, στο παραγόμενο .txt παιρνιούνται αλαμπουρνέζικα. Διάβασα το RFC του POP, αλλά δεν βρήκα κάτι να με βοηθήσει.
Σκέφτηκα αφού γεμίζω με τα δεδομένα τον StremReader, να βρω το charsettype και μετά να τρέξω όλο το string και να το κωδικοποιώ γραμμή-γραμμή. Μήπως θα ήταν ορθότερο να αποθηκεύω το body σε xml?
Οποιαδήποτε βοήθεια καλοδεχούμενη..
No knowledge can be certain, if it is not based
upon mathematics or upon some other knowledge
which is itself based upon the mathematical
sciences.