Προς το περιεχόμενο

[VB.NET] POP3 Reading Mails και Attachments...


nkeroulis

Προτεινόμενες αναρτήσεις

Δημοσ.

Μπορείτε να μου πείτε πως γίνεται να διαβάσω ενα mail (POP3) που ξέρω τον τίτλο του?

Πχ. Θέλω να διαβάσω ενα mail που έχει τίτλο "G12".

 

Επίσης τι κώδικας χρειάζεται για να αποθηκεύω και τα attachment?

Πχ αν έχει δύο attachments. File.txt File.dat

 

Μέχρι στιγμής ξέρω να λαμβάνω...

>
Imports System.Net.Sockets

Public Class Form1

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown

       Dim mail As New Pop3Mail

       mail.Connect("pop.gmail.com", "nkeroulis", "********")

       For Each msg As Pop3Mail.Pop3Message In mail.List

           TextBox1.Text = (DirectCast(mail.Retrieve(msg), Pop3Mail.Pop3Message).message)

       Next

   End Sub

End Class

Public Class Pop3Mail

   Inherits System.Net.Sockets.TcpClient

   Public Class Pop3Exception

       Inherits System.ApplicationException


       Public Sub New(ByVal str As String)

           MyBase.New(str)


       End Sub 'New

   End Class 'Pop3Exception

   Public Class Pop3Message

       Public number As Long

       Public bytes As Long

       Public retrieved As Boolean

       Public message As String

   End Class 'Pop3Message

   Public Overloads Sub Connect(ByVal server As String, ByVal username As String, ByVal password As String)

       Dim message As String

       Dim strResponse As String

       Connect(server, 110)

       strResponse = Response()

       If strResponse.Substring(0, 3) <> "+OK" Then

           Throw New Pop3Exception(Response)

       End If

       message = "USER " + username + vbCr + vbLf

       Write(message)

       strResponse = Response()

       If strResponse.Substring(0, 3) <> "+OK" Then

           Throw New Pop3Exception(strResponse)

       End If

       message = "PASS " + password + vbCr + vbLf

       Write(message)

       strResponse = Response()

       If strResponse.Substring(0, 3) <> "+OK" Then

           Throw New Pop3Exception(strResponse)

       End If

   End Sub 'Connect

   Public Sub Disconnect()

       Dim message As String

       Dim strResponse As String

       message = "QUIT" + vbCr + vbLf

       Write(message)

       strResponse = Response()

       If strResponse.Substring(0, 3) <> "+OK" Then

           Throw New Pop3Exception(strResponse)

       End If

   End Sub 'Disconnect

   Public Function List() As ArrayList

       Dim message As String

       Dim strResponse As String

       Dim retval As New ArrayList

       message = "LIST" + vbCr + vbLf

       Write(message)

       strResponse = Response()

       If strResponse.Substring(0, 3) <> "+OK" Then

           Throw New Pop3Exception(strResponse)

       End If

       While True

           strResponse = Response()

           If strResponse = "." & vbCr & vbLf Then

               Return retval

           Else

               Dim msg As New Pop3Message

               Dim seps As Char() = " ".ToCharArray

               Dim values As String() = strResponse.Split(seps)

               msg.number = Int32.Parse(values(0))

               msg.bytes = Int32.Parse(values(1))

               msg.retrieved = False

               retval.Add(msg)

           End If

       End While

   End Function 'List

   Public Function Retrieve(ByVal rhs As Pop3Message) As Pop3Message

       Dim message As String

       Dim strResponse As String

       Dim msg As New Pop3Message

       msg.bytes = rhs.bytes

       msg.number = rhs.number

       message = "RETR " & rhs.number & vbCr & vbLf

       Write(message)

       strResponse = Response()

       If strResponse.Substring(0, 3) <> "+OK" Then

           Throw New Pop3Exception(strResponse)

       End If

       msg.retrieved = True

       While True

           strResponse = Response()

           If strResponse = "." & vbCr & vbLf Then

               Exit While

           Else

               msg.message += strResponse

           End If

       End While

       Return msg

   End Function 'Retrieve

   Private Sub Write(ByVal message As String)

       Dim en As New System.Text.ASCIIEncoding

       Dim WriteBuffer(1023) As Byte

       WriteBuffer = en.GetBytes(message)

       Dim stream As NetworkStream = GetStream()

       stream.Write(WriteBuffer, 0, WriteBuffer.Length)

       Debug.WriteLine("WRITE:" + message)

   End Sub 'Write



   Public Sub Delete(ByVal rhs As Pop3Message)

       Dim message As String

       Dim strResponse As String

       message = "DELE " & rhs.number & vbCr & vbLf

       Write(message)

       strResponse = Response()

       If strResponse.Substring(0, 3) <> "+OK" Then

           Throw New Pop3Exception(strResponse)

       End If

   End Sub 'Delete

   Private Function Response() As String

       Dim enc As New System.Text.ASCIIEncoding

       Dim serverbuff() As Byte = New [byte](1023) {}

       Dim stream As NetworkStream = GetStream()

       Dim count As Integer = 0

       While True

           Dim buff() As Byte = New [byte](1) {}

           Dim bytes As Integer = stream.Read(buff, 0, 1)

           If bytes = 1 Then

               serverbuff(count) = buff(0)

               count += 1

               If buff(0) = Asc(vbLf) Then

                   Exit While

               End If

           Else

               Exit While

           End If

       End While

       Dim retval As String = enc.GetString(serverbuff, 0, count)

       Debug.WriteLine("READ:" + retval)

       Return retval

   End Function 'Response

End Class

Δημοσ.

Δεν το έχω ψάξει πολύ, αλλα δοκίμασε και την Imports System.Net.Mail

 

Για παραδειγμα αν σε βοηθάει έχω την δημιουργία ενός mail.

 

>Dim insMail As New MailMessage(New MailAddress(strFromAddress, strFromName), Νew MailAddress(strToAddress, strToName))
           With insMail
               .Subject = strSubject
               .Body = strBody
               .IsBodyHtml = True
               If Not strAttachments.Equals(String.Empty) Then
                   Dim strFile As String
                   Dim strAttach() As String = strAttachments.Split(";")
                   For Each strFile In strAttach
                       .Attachments.Add(New Attachment(strFile.Trim()))
                   Next
               End If
           End With

           Dim smtp As New System.Net.Mail.SmtpClient
           smtp.UseDefaultCredentials = False

           smtp.Host = "host.com"
           smtp.Port = 25
           smtp.Send(insMail)

 

Ίσως να έχει κάτι για αυτό που θές αλλά μπορεί να αλλάξει πολύ και τον κώδικα σου.

Αρχειοθετημένο

Αυτό το θέμα έχει αρχειοθετηθεί και είναι κλειστό για περαιτέρω απαντήσεις.

  • Δημιουργία νέου...