Downloading mail through VB.NET?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brett

    Downloading mail through VB.NET?

    I'd like any references to code that demonstrates how to download email
    messages from a mail server strictly through VB.NET. I'd also like to split
    each message into seperate components such as:

    - header
    - return path
    - to
    - from
    - subject
    - folder it was downloaded from
    - body (multi parts)
    - attachment

    Thanks,
    Brett


  • Ken Tucker [MVP]

    #2
    Re: Downloading mail through VB.NET?

    Hi,

    I converted this c# pop3 mail class to vb.net. Maybe this will
    help.



    How to use

    Dim mail As New Pop3Mail

    mail.Connect("m ail.xxxx.net", "user", "password")

    For Each msg As Pop3Mail.Pop3Me ssage In mail.List

    Trace.WriteLine (DirectCast(mai l.Retrieve(msg) ,
    Pop3Mail.Pop3Me ssage).message)

    Next



    The class



    Imports System.Net.Sock ets

    Public Class Pop3Mail

    Inherits System.Net.Sock ets.TcpClient





    Public Class Pop3Exception

    Inherits System.Applicat ionException


    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.Sub string(0, 3) <> "+OK" Then

    Throw New Pop3Exception(R esponse)

    End If

    message = "USER " + username + vbCr + vbLf

    Write(message)

    strResponse = Response()

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

    Throw New Pop3Exception(s trResponse)

    End If

    message = "PASS " + password + vbCr + vbLf

    Write(message)

    strResponse = Response()

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

    Throw New Pop3Exception(s trResponse)

    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.Sub string(0, 3) <> "+OK" Then

    Throw New Pop3Exception(s trResponse)

    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.Sub string(0, 3) <> "+OK" Then

    Throw New Pop3Exception(s trResponse)

    End If

    While True

    strResponse = Response()

    If strResponse = "." & vbCr & vbLf Then

    Return retval

    Else

    Dim msg As New Pop3Message

    Dim seps As Char() = " ".ToCharArr ay

    Dim values As String() = strResponse.Spl it(seps)

    msg.number = Int32.Parse(val ues(0))

    msg.bytes = Int32.Parse(val ues(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.Sub string(0, 3) <> "+OK" Then

    Throw New Pop3Exception(s trResponse)

    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.ASC IIEncoding

    Dim WriteBuffer(102 3) As Byte

    WriteBuffer = en.GetBytes(mes sage)

    Dim stream As NetworkStream = GetStream()

    stream.Write(Wr iteBuffer, 0, WriteBuffer.Len gth)

    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.Sub string(0, 3) <> "+OK" Then

    Throw New Pop3Exception(s trResponse)

    End If

    End Sub 'Delete

    Private Function Response() As String

    Dim enc As New System.Text.ASC IIEncoding

    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(buf f, 0, 1)

    If bytes = 1 Then

    serverbuff(coun t) = 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(s erverbuff, 0, count)

    Debug.WriteLine ("READ:" + retval)

    Return retval

    End Function 'Response

    End Class



    Ken

    ---------------------------------

    "Brett" <no@spam.com> wrote in message
    news:eRUWe8o7EH A.2700@TK2MSFTN GP14.phx.gbl...
    I'd like any references to code that demonstrates how to download email
    messages from a mail server strictly through VB.NET. I'd also like to split
    each message into seperate components such as:

    - header
    - return path
    - to
    - from
    - subject
    - folder it was downloaded from
    - body (multi parts)
    - attachment

    Thanks,
    Brett



    Comment

    • Brett

      #3
      Re: Downloading mail through VB.NET?

      Ken,

      Thanks. I'm new to VB. Please forgive me for being slow.

      I've created the necessary class and have placed the For loop code into a
      form's Load() prcoedure. I built the project than executed the EXE. I keep
      getting this message:

      NullReferenceEx ception was unhandled
      Use the "new" keyword to create an object instance.
      Check to determine if the object is null before calling the method.

      Do you know what the above is referring to?

      If I click the EXE from Windows Explorer (outside of IDE), it seems to work.
      This is after I moved it out of the default Visual Studio folder and
      rebuilt. I see the form with no errors. However, nothing is diplayed,
      just a blank form. There is one message in the account. I probably need to
      use another method of running the app. I have this now:
      - Project
      - Class1.vb (class code is here)
      - Form1.vb (For loop here inside Form1_Load())


      Also, the only warning I get during the build is:
      Warning 1 Function 'List' doesn't return a value on all code paths. A null
      reference exception could occur at run time when the result is used.
      C:\Documents and Settings\Owner\ Local Settings\Applic ation Data\Temporary
      Projects\ClassL ibrary1\Class1. vb 149 4

      Should I put a return 0 or something in the List() function or is it
      affecting anything?

      Thanks,
      Brett


      "Ken Tucker [MVP]" <vb2ae@bellsout h.net> wrote in message
      news:%23$x5m$o7 EHA.3504@TK2MSF TNGP12.phx.gbl. ..[color=blue]
      > Hi,
      >
      > I converted this c# pop3 mail class to vb.net. Maybe this will
      > help.
      >
      > http://www.programmersheaven.com/2/Art_CSharp_3
      >
      > How to use
      >
      > Dim mail As New Pop3Mail
      >
      > mail.Connect("m ail.xxxx.net", "user", "password")
      >
      > For Each msg As Pop3Mail.Pop3Me ssage In mail.List
      >
      > Trace.WriteLine (DirectCast(mai l.Retrieve(msg) ,
      > Pop3Mail.Pop3Me ssage).message)
      >
      > Next
      >
      >
      >[/color]


      Comment

      • Ken Tucker [MVP]

        #4
        Re: Downloading mail through VB.NET?

        Hi,

        The mail.connect method accepts Server, UserName, Password. If
        it is unable to connect you will get an error. Check and make sure you
        supplied the correct information in the mail.connect method.

        Ken
        ---------------------
        "Brett" <no@spam.com> wrote in message
        news:uCDFFWp7EH A.2676@TK2MSFTN GP12.phx.gbl...
        Ken,

        Thanks. I'm new to VB. Please forgive me for being slow.

        I've created the necessary class and have placed the For loop code into a
        form's Load() prcoedure. I built the project than executed the EXE. I keep
        getting this message:

        NullReferenceEx ception was unhandled
        Use the "new" keyword to create an object instance.
        Check to determine if the object is null before calling the method.

        Do you know what the above is referring to?

        If I click the EXE from Windows Explorer (outside of IDE), it seems to work.
        This is after I moved it out of the default Visual Studio folder and
        rebuilt. I see the form with no errors. However, nothing is diplayed,
        just a blank form. There is one message in the account. I probably need to
        use another method of running the app. I have this now:
        - Project
        - Class1.vb (class code is here)
        - Form1.vb (For loop here inside Form1_Load())


        Also, the only warning I get during the build is:
        Warning 1 Function 'List' doesn't return a value on all code paths. A null
        reference exception could occur at run time when the result is used.
        C:\Documents and Settings\Owner\ Local Settings\Applic ation Data\Temporary
        Projects\ClassL ibrary1\Class1. vb 149 4

        Should I put a return 0 or something in the List() function or is it
        affecting anything?

        Thanks,
        Brett


        "Ken Tucker [MVP]" <vb2ae@bellsout h.net> wrote in message
        news:%23$x5m$o7 EHA.3504@TK2MSF TNGP12.phx.gbl. ..[color=blue]
        > Hi,
        >
        > I converted this c# pop3 mail class to vb.net. Maybe this will
        > help.
        >
        > http://www.programmersheaven.com/2/Art_CSharp_3
        >
        > How to use
        >
        > Dim mail As New Pop3Mail
        >
        > mail.Connect("m ail.xxxx.net", "user", "password")
        >
        > For Each msg As Pop3Mail.Pop3Me ssage In mail.List
        >
        > Trace.WriteLine (DirectCast(mai l.Retrieve(msg) ,
        > Pop3Mail.Pop3Me ssage).message)
        >
        > Next
        >
        >
        >[/color]



        Comment

        • Brett

          #5
          Re: Downloading mail through VB.NET?

          Ken,

          I have it working now. Thanks. Does VB.NET offer any type of POP
          functionality besides connecting? For example - downloading messages, file
          attachment boolean value, message size, message count, etc?

          Brett

          "Ken Tucker [MVP]" <vb2ae@bellsout h.net> wrote in message
          news:%23Q6RiWI8 EHA.4004@tk2msf tngp13.phx.gbl. ..[color=blue]
          > Hi,
          >
          > The mail.connect method accepts Server, UserName, Password. If
          > it is unable to connect you will get an error. Check and make sure you
          > supplied the correct information in the mail.connect method.
          >
          > Ken
          > ---------------------
          > "Brett" <no@spam.com> wrote in message
          > news:uCDFFWp7EH A.2676@TK2MSFTN GP12.phx.gbl...
          > Ken,
          >
          > Thanks. I'm new to VB. Please forgive me for being slow.
          >
          > I've created the necessary class and have placed the For loop code into a
          > form's Load() prcoedure. I built the project than executed the EXE. I
          > keep
          > getting this message:
          >
          > NullReferenceEx ception was unhandled
          > Use the "new" keyword to create an object instance.
          > Check to determine if the object is null before calling the method.
          >
          > Do you know what the above is referring to?
          >
          > If I click the EXE from Windows Explorer (outside of IDE), it seems to
          > work.
          > This is after I moved it out of the default Visual Studio folder and
          > rebuilt. I see the form with no errors. However, nothing is diplayed,
          > just a blank form. There is one message in the account. I probably need
          > to
          > use another method of running the app. I have this now:
          > - Project
          > - Class1.vb (class code is here)
          > - Form1.vb (For loop here inside Form1_Load())
          >
          >
          > Also, the only warning I get during the build is:
          > Warning 1 Function 'List' doesn't return a value on all code paths. A
          > null
          > reference exception could occur at run time when the result is used.
          > C:\Documents and Settings\Owner\ Local Settings\Applic ation Data\Temporary
          > Projects\ClassL ibrary1\Class1. vb 149 4
          >
          > Should I put a return 0 or something in the List() function or is it
          > affecting anything?
          >
          > Thanks,
          > Brett
          >
          >
          > "Ken Tucker [MVP]" <vb2ae@bellsout h.net> wrote in message
          > news:%23$x5m$o7 EHA.3504@TK2MSF TNGP12.phx.gbl. ..[color=green]
          >> Hi,
          >>
          >> I converted this c# pop3 mail class to vb.net. Maybe this
          >> will
          >> help.
          >>
          >> http://www.programmersheaven.com/2/Art_CSharp_3
          >>
          >> How to use
          >>
          >> Dim mail As New Pop3Mail
          >>
          >> mail.Connect("m ail.xxxx.net", "user", "password")
          >>
          >> For Each msg As Pop3Mail.Pop3Me ssage In mail.List
          >>
          >> Trace.WriteLine (DirectCast(mai l.Retrieve(msg) ,
          >> Pop3Mail.Pop3Me ssage).message)
          >>
          >> Next
          >>
          >>
          >>[/color]
          >
          >
          >[/color]


          Comment

          Working...