473,398 Members | 2,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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
Nov 21 '05 #1
4 1850
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("mail.xxxx.net", "user", "password")

For Each msg As Pop3Mail.Pop3Message In mail.List

Trace.WriteLine(DirectCast(mail.Retrieve(msg),
Pop3Mail.Pop3Message).message)

Next

The class

Imports System.Net.Sockets

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

Ken

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

"Brett" <no@spam.com> wrote in message
news:eR**************@TK2MSFTNGP14.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

Nov 21 '05 #2
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:

NullReferenceException 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\Application Data\Temporary
Projects\ClassLibrary1\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]" <vb***@bellsouth.net> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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("mail.xxxx.net", "user", "password")

For Each msg As Pop3Mail.Pop3Message In mail.List

Trace.WriteLine(DirectCast(mail.Retrieve(msg),
Pop3Mail.Pop3Message).message)

Next

Nov 21 '05 #3
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:uC**************@TK2MSFTNGP12.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:

NullReferenceException 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\Application Data\Temporary
Projects\ClassLibrary1\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]" <vb***@bellsouth.net> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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("mail.xxxx.net", "user", "password")

For Each msg As Pop3Mail.Pop3Message In mail.List

Trace.WriteLine(DirectCast(mail.Retrieve(msg),
Pop3Mail.Pop3Message).message)

Next


Nov 21 '05 #4
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]" <vb***@bellsouth.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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:uC**************@TK2MSFTNGP12.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:

NullReferenceException 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\Application Data\Temporary
Projects\ClassLibrary1\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]" <vb***@bellsouth.net> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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("mail.xxxx.net", "user", "password")

For Each msg As Pop3Mail.Pop3Message In mail.List

Trace.WriteLine(DirectCast(mail.Retrieve(msg),
Pop3Mail.Pop3Message).message)

Next


Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: S.W. Rasmussen | last post by:
Handling file downloading with Randy Birch very nice code (http://vbnet.mvps.org/code/internet/ftpdofiledownload.htm) raises a second question: I my case, the user should not be allowed to...
2
by: Ian Gordon | last post by:
Hi, I have 2 problems when downloading a file from my ASP.net app to the client PC. At the moment I'm doing it with the following code: Response.ContentType = "application/octet-stream" ...
4
by: Sumaira Ahmad | last post by:
Hi, Is there any way of playing songs and downloading songs from a server onto a client machine. I want to develop an application such as a music portal which allows clients to play songs that...
0
by: kamal | last post by:
Hi, I have one problem . we have implemented overriding of IDownloadManager of Internet explorer. it was working fine. If we give any files which have direct hyperlink i.e from geocities or like...
7
by: Steve | last post by:
Hello, I am a beginner/intermediate c++ coder. I have a program that I am writing and would like to be able to distribute to some of my friends. The problem with this is that the data files...
35
by: keerthyragavendran | last post by:
hi i'm downloading a single file using multiple threads... how can i specify a particular range of bytes alone from a single large file... for example say if i need only bytes ranging from...
1
by: apondu | last post by:
Hi, I am new to web services and i am facing a problem. I am interested in downloading some file from internet and use it for furthur processing For eg. i have a file at a the following URL and...
1
by: apondu | last post by:
Hi, I am new to web services and i am facing a problem. I am interested in downloading some file from internet and use it for furthur processing For eg. i have a file at a the following URL and...
1
by: =?Utf-8?B?ZGVicmFrYXk=?= | last post by:
the last day and a half, I have only been able to dowload a small percent of emails - mail always says that it is downloading 28 of 174 emails, and it never goes any further???? I am running the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.