Hey, thanx to all the guys in here, i have done some code for retrieving email messages from a pop3 server. this is just a part of my new project. i just put it here so people can see what to do and not to fall into stupid traps i fell in. so here it goes.
1. in a module, create these enums and types.
VB Code:
Public Enum POP_STATUS 'this is for the status of the pop getter
pop_Connect
pop_User
pop_Password
pop_Stat
pop_List
pop_Retr
pop_CRetr
pop_Delete
pop_Quit
End Enum
Public Type MSG_STATS 'this is for the messages in the server
msg_Len As Long
msg_ThisLen As Long
msg_Body As String
msg_File As String
End Type
Public Type POP_SERVER 'this is for the pop server settings
srv_Name As String
srv_Address As String
srv_Port As Long
srv_User As String
srv_Password As String
srv_Messages As Long
End Type
then there are some variable declarations, name it whatever u want but here is what i have done.
VB Code:
Public pStat As POP_STATUS
Public myMsg() As MSG_STATS
Public myServer As POP_SERVER
Public mError As Long
Public curMsg As Long
Public myMsgList As String
Public myMsgListNum As Long
Public fso As New FileSystemObject' use this if u want to save data using the file system objects, or omit the 2 lines here.
Public ***ile As TextStream
now for the settings part, as we already have some settings put up as a datatype, we will access it here.
VB Code:
With myServer
.srv_Address = "<your pop3 server>"
.srv_Name = "<Name of your ISP, not needed explicitly>"
.srv_User = "<your pop3 username>"
.srv_Password = "<your pop3 password>"
.srv_Port = 110 'this is the default pop3 port.
End With
and then the connection part. u know the drill
VB Code:
pStat = pop_Connect 'we set the status enum to connecting stage
if you have an idea of how pop3 works, u will know these simple facts.
1. connect -> returns +OK <some message>
2. user <username> -> returns +OK <some message>
3. pass <password> -> returns +OK <some message> 'we are now ready for data arrival
4. stat -> returns +OK <number of messages> <sum of size of messages>
5. list <message number> -> returns +OK <message number> <size of message in octets>
6. retr <message number> -> returns +OK followed by the contents of the message *NOTE1
7. dele <message number> -> returns +OK and queues the message for delete at user logoff
8. quit -> returns +OK <some message> and closes connection
NOTE1: all messages are ended by a ".", this, I found eventually, was undocumented.
Last edited by mebhas; Dec 6th, 2005 at 05:12 AM.
Reason: small changes for better readability
oh yeah, u need the reference to microsoft scripting object, that is if u want to use the file system object to write down the recieved data. or u can omit the two lines. i used the classic vb way to write down the data, as is stated in my post #4.
thanks but i get error # 11004 May be i don't have MSWINSCK.OCX it says 'Valid Name, no data record of requested type' and source is 'C;\windows\system32\MSWINSCK.OCX'
hello.. all fixed by myself... and u know i m making a mod for this ... user download all messages as .txt and a form read message, From , Subject , To, Date & Time, i m almost done with it
now can u tell me is it not possible to save message with its Subject Name ?
Last edited by mfurqan; Dec 11th, 2005 at 08:53 AM.
sure, just scan the message line by line until u get the left(text,9)="<subject>" u can get the subject there. set the filename as the subject text....
sure, just scan the message line by line until u get the left(text,9)="<subject>" u can get the subject there. set the filename as the subject text....
can u please post an example i really can't understand it
yes it deletes the messages after downloading them. and for the example of the saving by subject. u'll have to wait a couple of days, i am a bit busy right now.
The zip you posted should be enough for anyone to get started ..
you did all the hard work already .. i know this thread is kind of old but just wanted to say thanks ..
hello
I downloaded the attachment ws.zip from this thread abd try to run the project using yahoo Pop server "pop.mail.yahoo.com" and use the username and password but it gives error while checking password. Note that password which i enterd is right. i also use another Username and Password but still it shows the Password Inccorect. can u suggest waht s the problem?
'oops we got a -ERR message means we have an error of some sorts
mError = 6
End If
Case Is = pop_CRetr
Just_Write_To_File myData
showprint myData
With myMsg(curMsg)
.msg_ThisLen = bytesTotal + .msg_ThisLen
End With
If Mid(myData, Len(myData) - 2, 1) = "." Then
Close_And_Save_File
pStat = pop_Delete
sSend "dele " & curMsg
End If
Case Is = pop_Delete
'we just recieved the delete command's reply
showprint myData
If Left(myData, 1) = "+" Then
If CheckForMore(curMsg) = True Then
pStat = pop_List
curMsg = curMsg + 1
sSend "list " & curMsg
Else
pStat = pop_Quit
sSend "quit"
End If
ElseIf Left(myData, 1) = "-" Then
'oops we got a -ERR message means we have an error of some sorts
mError = 7
End If
Case Is = pop_Quit
showprint myData
If Left(myData, 1) = "-" Then
mError = 8
End If
End Select
End Sub
Hello
I gave a look at the code below and at the zip file attached to the thread but I can't understand how this code works yet. I have some questions to ask:
1) what is bytesTotal argument and what I have to pass to it?
2) how has the ws_DataArrival function to be called to retreive all the incoming mails and dowload them?
I really don't understand how to use this code help me please
The ws_DataArrival subroutine is an event handler. Its bytesTotal argument is passed to your event handler subroutine by the Winsock control. However most serious code ignores it since by the time your handler gets run its value might be out of date anyway, and relying on it could mean losing or corrupting data. It is pretty obsolete but retained for compatibility with old programs.
If you don't know this you need to work on your VB skills. It is all pretty basic stuff.
Reliance on bytesTotal here (in the code above) is pathological. The myData variable could well have more characters in it than this, and later bytesTotal is used instead of the far more reliable Len(myData). This is a bad indicator of the quality of all of this code, which indeed has a ton of "well it might work if you get lucky" things in it.
All in all, this is sort of flaky more-or-less-working code for the way email worked in the late 1990s. You'd be far better off to pay for a licensed 3rd party POP3 library, which is likely to support SSL/TLS and modern authentication too.
The ws_DataArrival subroutine is an event handler.
You're totally right, dude. I didn't notice this when I opened the project. What kind of stupid I can be sometimes
Originally Posted by dilettante
Its bytesTotal argument is passed to your event handler subroutine by the Winsock control. However most serious code ignores it since by the time your handler gets run its value might be out of date anyway, and relying on it could mean losing or corrupting data. It is pretty obsolete but retained for compatibility with old programs.
If you don't know this you need to work on your VB skills. It is all pretty basic stuff.
I am an autodidact of these stuff. I have never used Winsock control in the past and I don't know well how it works, I'm trying lo learn it. However thank you for this explaination.
Originally Posted by dilettante
Reliance on bytesTotal here (in the code above) is pathological. The myData variable could well have more characters in it than this, and later bytesTotal is used instead of the far more reliable Len(myData). This is a bad indicator of the quality of all of this code, which indeed has a ton of "well it might work if you get lucky" things in it.
All in all, this is sort of flaky more-or-less-working code for the way email worked in the late 1990s. You'd be far better off to pay for a licensed 3rd party POP3 library, which is likely to support SSL/TLS and modern authentication too.
Ok, now that I know that I still don't want to pay for a 3rd party library LOL
I'd like to develope something with my hands (if it's possible) but I need a start point to study this matter.