Click to See Complete Forum and Search --> : VB6-pop3 email
mebhas
Dec 6th, 2005, 03:54 AM
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.
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.
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.
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
pStat = pop_Connect 'we set the status enum to connecting stage
With myServer
ws.Connect .srv_Address, .srv_Port
End With
mebhas
Dec 6th, 2005, 04:00 AM
2. Now comes the real part of retrieving mail. the code
Private Sub ws_DataArrival(ByVal bytesTotal As Long)
Dim myData As String
ws.GetData myData
Select Case pStat
Case Is = pop_Connect
'this is still in connection state
showprint myData
If Left(myData, 1) = "+" Then
'we got a +OK message, now for user authentication
pStat = pop_User
sSend "user " & myServer.srv_User
ElseIf Left(myData, 1) = "-" Then
'oops we got a -ERR message means we have an error of some sorts
mError = 1
End If
Case Is = pop_User
showprint myData
'we are now in user authentication stage
If Left(myData, 1) = "+" Then
'we got a +OK message, now for password authentication
pStat = pop_Password
sSend "pass " & myServer.srv_Password
ElseIf Left(myData, 1) = "-" Then
'oops we got a -ERR message means we have an error of some sorts
mError = 2
End If
Case Is = pop_Password
'we are now in password authentication stage
showprint myData
If Left(myData, 1) = "+" Then
'we got a +OK message, now for checking number of emails in the server
pStat = pop_Stat
sSend "stat"
ElseIf Left(myData, 1) = "-" Then
'oops we got a -ERR message means we have an error of some sorts
mError = 3
End If
Case Is = pop_Stat
'we are now checking for number of messages
showprint myData
If Left(myData, 1) = "+" Then
'we got a +OK message, now for checking individual messages
myServer.srv_Messages = getMsgNum(myData) 'we got the number of messages in the server
If myServer.srv_Messages > 0 Then
'we have messages
curMsg = 1
pStat = pop_List
sSend "list " & curMsg
Else
'we have no messages just quit
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 = 4
End If
Case Is = pop_List
'we are checking individual messages
showprint myData
If Left(myData, 1) = "+" Then
If curMsg > 0 And curMsg <= myServer.srv_Messages Then
'we are ready to recieve messages
ReDim Preserve myMsg(curMsg) As MSG_STATS
With myMsg(curMsg)
.msg_Body = ""
.msg_File = getMsgFile(curMsg)
.msg_Len = thisMsgLen(myData)
.msg_ThisLen = 0
End With
'send command to recieve the particular message
pStat = pop_Retr
sSend "retr " & curMsg
ElseIf curMsg > myServer.srv_Messages Then
'all messages have been downloaded
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 = 5
End If
Case Is = pop_Retr
'we recieve the actual message here
showprint myData
If Left(myData, 1) = "+" Then
'straight write to file
Open_And_Write_File myMsg(curMsg).msg_File, myData
'calculate the size recieved
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
Else
pStat = pop_CRetr
End If
ElseIf Left(myData, 1) = "-" Then
'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
mebhas
Dec 6th, 2005, 04:07 AM
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.
mebhas
Dec 6th, 2005, 04:11 AM
there are some parts missing in the code in post#2.
->the open_and_write_to_file procedure does nothing except do a myMsgList = wData 'wData is the string recieved
->the just_write_to_file does this myMsgList = myMsgList & wData
->the save_and _close_file does this mF = FreeFile
Open mFile for Output as #nF
Print #mF, myMsgList
Close #mF
mebhas
Dec 6th, 2005, 04:40 AM
forgot to mention. when saving the files, save it with .eml extension so it can be opened by outlook/outlook express even by double clicking the file.
mfurqan
Dec 6th, 2005, 07:37 AM
it says invalid outside procedure
mfurqan
Dec 6th, 2005, 07:40 AM
do i need to add a componene or refrence ?
mebhas
Dec 7th, 2005, 04:11 AM
exactly where does it say that?
u have to add a component, winsock, of course.
mfurqan
Dec 7th, 2005, 07:15 AM
it syas user-defined type not found i added MS Winsock 6.0 component
mfurqan
Dec 7th, 2005, 07:27 AM
may be there is a problem :::
Public fso As New FileSystemObject
Public ***ile As TextStream
mebhas
Dec 7th, 2005, 09:57 PM
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.
mfurqan
Dec 9th, 2005, 07:11 AM
and yeh do i need to make text boxes and other things ?
can't u please provide a zip package it really difficult this way :P
mfurqan
Dec 9th, 2005, 07:18 AM
okay i did it all... no errors now but now i see blank form nothin else no messages.. nothing
mebhas
Dec 10th, 2005, 09:33 PM
on demand...
mfurqan
Dec 11th, 2005, 02:32 AM
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'
mfurqan
Dec 11th, 2005, 03:05 AM
self resolved problem.
its working great :D
but it only downloads Unread Messages ?
and really don't know what that Text4 box realy is for :)
mfurqan
Dec 11th, 2005, 07:45 AM
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 :D
now can u tell me is it not possible to save message with its Subject Name ?
mebhas
Dec 12th, 2005, 03:54 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....
mfurqan
Dec 12th, 2005, 06:16 AM
well does it delete all messages from pop3 account after downloading them. ?
mfurqan
Dec 12th, 2005, 07:20 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....
can u please post an example i really can't understand it :(
mebhas
Dec 12th, 2005, 10:03 PM
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.
mfurqan
Dec 13th, 2005, 07:00 AM
okay sir no problem :)
mfurqan
Dec 15th, 2005, 05:55 AM
i wish if u can do it earlier because i have made .TXT email reader :D
mebhas
Dec 17th, 2005, 11:42 PM
ok, here's some quick code, see if you can make it any better....
'make reference to microsoft scripting runtime, we need it for file system object
'make these variables, fso as filesystemobjects, readFile as textstream, readString as string
set readfile=fso.opentextfile "your file name", forreading
do while not readfile.eof
readfile.readline readstring
processString readstring 'processString is a sub, need some thinking for that
loop
now create a sub processString(inString as string)
some part of the code.
'i am using if statements here coz i know there are more conditions than a select case can handle.
if ucase$(left$(instring,5))="FROM:" then
'this is whom u recieved the message from. the remaining part of the line is the sender.
elseif ucase$(left$(instring,5))="DATE:" then
'this is the date u recieved the message.
elseif ucase$(left$(instring,3))="TO:" then
'this is the reciever of the message
elseif ucase$(left$(instring,8))="SUBJECT:" then
'this is the subject of the message.
elseif len(instring)=0 then
'we have reached the beginning of the message, we can now write all the remining part of the email as the body.
endif
mebhas
Dec 17th, 2005, 11:44 PM
as u can see, i have just typed in the code and it should be debugged thoroughly and proper declarations should be made.
mfurqan
Jan 6th, 2006, 06:11 AM
really can't understand it :( sorry...
rory
Jun 10th, 2006, 10:26 PM
Very well done mebhas ... :thumb:
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 ..
try.test.abc
Apr 28th, 2007, 01:41 AM
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?
teguh123
Sep 7th, 2009, 07:45 AM
I tried that with a throw away hotmail and it doesn't work
daveyhoin0@hotmail.com|nzdr9p
POP server: pop3.live.com (Port 995)
POP SSL required? Yes
martind1
Nov 5th, 2011, 01:10 PM
Hmm, why can't i find the winsock control?
mebhas
Nov 5th, 2011, 01:16 PM
Hmm, why can't i find the winsock control?
you have to add the control to the project. hit ctrl-t and look for winsock, or try browsing for <windows folder>\system32\winsock.ocx
martind1
Nov 5th, 2011, 01:18 PM
Shoot. Does this not do SSL pop?
mebhas
Nov 5th, 2011, 01:20 PM
nope, just plain pop
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.