VB6-pop3 email-VBForums
Results 1 to 33 of 33

Thread: VB6-pop3 email

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2003
    Location
    Sydney
    Posts
    1,123

    VB6-pop3 email

    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:
    1. Public Enum POP_STATUS 'this is for the status of the pop getter
    2.     pop_Connect
    3.     pop_User
    4.     pop_Password
    5.     pop_Stat
    6.     pop_List
    7.     pop_Retr
    8.     pop_CRetr
    9.     pop_Delete
    10.     pop_Quit
    11. End Enum
    12.  
    13. Public Type MSG_STATS 'this is for the messages in the server
    14.     msg_Len As Long
    15.     msg_ThisLen As Long
    16.     msg_Body As String
    17.     msg_File As String
    18. End Type
    19.  
    20. Public Type POP_SERVER 'this is for the pop server settings
    21.     srv_Name As String
    22.     srv_Address As String
    23.     srv_Port As Long
    24.     srv_User As String
    25.     srv_Password As String
    26.     srv_Messages As Long
    27. End Type

    then there are some variable declarations, name it whatever u want but here is what i have done.
    VB Code:
    1. Public pStat As POP_STATUS
    2. Public myMsg() As MSG_STATS
    3. Public myServer As POP_SERVER
    4.  
    5.  
    6. Public mError As Long
    7. Public curMsg As Long
    8.  
    9. Public myMsgList As String
    10. Public myMsgListNum As Long
    11.  
    12. Public fso As New FileSystemObject' use this if u want to save data using the file system objects, or omit the 2 lines here.
    13. 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:
    1. With myServer
    2.         .srv_Address = "<your pop3 server>"
    3.         .srv_Name = "<Name of your ISP, not needed explicitly>"
    4.         .srv_User = "<your pop3 username>"
    5.         .srv_Password = "<your pop3 password>"
    6.         .srv_Port = 110 'this is the default pop3 port.
    7.     End With

    and then the connection part. u know the drill
    VB Code:
    1. pStat = pop_Connect 'we set the status enum to connecting stage
    2.     With myServer
    3.         ws.Connect .srv_Address, .srv_Port
    4.     End With
    Last edited by mebhas; Dec 7th, 2005 at 10:59 PM.

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    May 2003
    Location
    Sydney
    Posts
    1,123

    Re: pop3 email

    2. Now comes the real part of retrieving mail. the code
    VB Code:
    1. Private Sub ws_DataArrival(ByVal bytesTotal As Long)
    2.     Dim myData As String
    3.    
    4.     ws.GetData myData
    5.    
    6.     Select Case pStat
    7.         Case Is = pop_Connect
    8.             'this is still in connection state
    9.             showprint myData
    10.             If Left(myData, 1) = "+" Then
    11.                 'we got a +OK message, now for user authentication
    12.                 pStat = pop_User
    13.                 sSend "user " & myServer.srv_User
    14.             ElseIf Left(myData, 1) = "-" Then
    15.                 'oops we got a -ERR message means we have an error of some sorts
    16.                 mError = 1
    17.             End If
    18.            
    19.         Case Is = pop_User
    20.             showprint myData
    21.             'we are now in user authentication stage
    22.             If Left(myData, 1) = "+" Then
    23.                 'we got a +OK message, now for password authentication
    24.                 pStat = pop_Password
    25.                 sSend "pass " & myServer.srv_Password
    26.             ElseIf Left(myData, 1) = "-" Then
    27.                 'oops we got a -ERR message means we have an error of some sorts
    28.                 mError = 2
    29.             End If
    30.            
    31.         Case Is = pop_Password
    32.             'we are now in password authentication stage
    33.             showprint myData
    34.             If Left(myData, 1) = "+" Then
    35.                 'we got a +OK message, now for checking number of emails in the server
    36.                 pStat = pop_Stat
    37.                 sSend "stat"
    38.             ElseIf Left(myData, 1) = "-" Then
    39.                 'oops we got a -ERR message means we have an error of some sorts
    40.                 mError = 3
    41.             End If
    42.            
    43.         Case Is = pop_Stat
    44.             'we are now checking for number of messages
    45.             showprint myData
    46.             If Left(myData, 1) = "+" Then
    47.                 'we got a +OK message, now for checking individual messages
    48.                
    49.                 myServer.srv_Messages = getMsgNum(myData) 'we got the number of messages in the server
    50.                
    51.                 If myServer.srv_Messages > 0 Then
    52.                     'we have messages
    53.                     curMsg = 1
    54.                     pStat = pop_List
    55.                     sSend "list " & curMsg
    56.                 Else
    57.                     'we have no messages just quit
    58.                     pStat = pop_Quit
    59.                     sSend "quit"
    60.                 End If
    61.             ElseIf Left(myData, 1) = "-" Then
    62.                 'oops we got a -ERR message means we have an error of some sorts
    63.                 mError = 4
    64.             End If
    65.            
    66.         Case Is = pop_List
    67.             'we are checking individual messages
    68.             showprint myData
    69.             If Left(myData, 1) = "+" Then
    70.                 If curMsg > 0 And curMsg <= myServer.srv_Messages Then
    71.                     'we are ready to recieve messages
    72.                     ReDim Preserve myMsg(curMsg) As MSG_STATS
    73.                     With myMsg(curMsg)
    74.                         .msg_Body = ""
    75.                         .msg_File = getMsgFile(curMsg)
    76.                         .msg_Len = thisMsgLen(myData)
    77.                         .msg_ThisLen = 0
    78.                     End With
    79.                
    80.                     'send command to recieve the particular message
    81.                     pStat = pop_Retr
    82.                     sSend "retr " & curMsg
    83.                 ElseIf curMsg > myServer.srv_Messages Then
    84.                     'all messages have been downloaded
    85.                     pStat = pop_Quit
    86.                     sSend "quit"
    87.                 End If
    88.             ElseIf Left(myData, 1) = "-" Then
    89.                 'oops we got a -ERR message means we have an error of some sorts
    90.                 mError = 5
    91.             End If
    92.            
    93.         Case Is = pop_Retr
    94.             'we recieve the actual message here
    95.             showprint myData
    96.             If Left(myData, 1) = "+" Then
    97.                 'straight write to file
    98.                 Open_And_Write_File myMsg(curMsg).msg_File, myData
    99.                
    100.                 'calculate the size recieved
    101.                 With myMsg(curMsg)
    102.                     .msg_ThisLen = bytesTotal + .msg_ThisLen
    103.                 End With
    104.                
    105.                 If Mid(myData, Len(myData) - 2, 1) = "." Then
    106.                     Close_And_Save_File
    107.                     pStat = pop_Delete
    108.                     sSend "dele " & curMsg
    109.                 Else
    110.                     pStat = pop_CRetr
    111.                 End If
    112.                
    113.             ElseIf Left(myData, 1) = "-" Then
    114.                 'oops we got a -ERR message means we have an error of some sorts
    115.                 mError = 6
    116.             End If
    117.            
    118.         Case Is = pop_CRetr
    119.             Just_Write_To_File myData
    120.             showprint myData
    121.            
    122.             With myMsg(curMsg)
    123.                 .msg_ThisLen = bytesTotal + .msg_ThisLen
    124.             End With
    125.                
    126.             If Mid(myData, Len(myData) - 2, 1) = "." Then
    127.                 Close_And_Save_File
    128.                 pStat = pop_Delete
    129.                 sSend "dele " & curMsg
    130.             End If
    131.            
    132.                        
    133.         Case Is = pop_Delete
    134.             'we just recieved the delete command's reply
    135.             showprint myData
    136.             If Left(myData, 1) = "+" Then
    137.                 If CheckForMore(curMsg) = True Then
    138.                     pStat = pop_List
    139.                     curMsg = curMsg + 1
    140.                     sSend "list " & curMsg
    141.                 Else
    142.                     pStat = pop_Quit
    143.                     sSend "quit"
    144.                 End If
    145.             ElseIf Left(myData, 1) = "-" Then
    146.                 'oops we got a -ERR message means we have an error of some sorts
    147.                 mError = 7
    148.             End If
    149.            
    150.         Case Is = pop_Quit
    151.             showprint myData
    152.             If Left(myData, 1) = "-" Then
    153.                 mError = 8
    154.             End If
    155.            
    156.     End Select
    157. End Sub

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    May 2003
    Location
    Sydney
    Posts
    1,123

    Re: pop3 email

    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

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    May 2003
    Location
    Sydney
    Posts
    1,123

    Re: pop3 email

    there are some parts missing in the code in post#2.

    ->the open_and_write_to_file procedure does nothing except do a
    VB Code:
    1. myMsgList = wData 'wData is the string recieved

    ->the just_write_to_file does this
    VB Code:
    1. myMsgList = myMsgList & wData

    ->the save_and _close_file does this
    VB Code:
    1. mF = FreeFile
    2. Open mFile for Output as #nF
    3. Print #mF, myMsgList
    4. Close #mF

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    May 2003
    Location
    Sydney
    Posts
    1,123

    Re: pop3 email

    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.

  6. #6
    Addicted Member mfurqan's Avatar
    Join Date
    Oct 2005
    Location
    Pakistan
    Posts
    176

    Re: VB6-pop3 email

    it says invalid outside procedure
    Muhammad Furqan Attari.

  7. #7
    Addicted Member mfurqan's Avatar
    Join Date
    Oct 2005
    Location
    Pakistan
    Posts
    176

    Re: VB6-pop3 email

    do i need to add a componene or refrence ?
    Muhammad Furqan Attari.

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    May 2003
    Location
    Sydney
    Posts
    1,123

    Re: VB6-pop3 email

    exactly where does it say that?
    u have to add a component, winsock, of course.

  9. #9
    Addicted Member mfurqan's Avatar
    Join Date
    Oct 2005
    Location
    Pakistan
    Posts
    176

    Re: VB6-pop3 email

    it syas user-defined type not found i added MS Winsock 6.0 component
    Muhammad Furqan Attari.

  10. #10
    Addicted Member mfurqan's Avatar
    Join Date
    Oct 2005
    Location
    Pakistan
    Posts
    176

    Re: VB6-pop3 email

    may be there is a problem :::

    Public fso As New FileSystemObject
    Public ***ile As TextStream
    Muhammad Furqan Attari.

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    May 2003
    Location
    Sydney
    Posts
    1,123

    Re: VB6-pop3 email

    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.

  12. #12
    Addicted Member mfurqan's Avatar
    Join Date
    Oct 2005
    Location
    Pakistan
    Posts
    176

    Re: VB6-pop3 email

    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
    Muhammad Furqan Attari.

  13. #13
    Addicted Member mfurqan's Avatar
    Join Date
    Oct 2005
    Location
    Pakistan
    Posts
    176

    Re: VB6-pop3 email

    okay i did it all... no errors now but now i see blank form nothin else no messages.. nothing
    Muhammad Furqan Attari.

  14. #14

    Thread Starter
    Frenzied Member
    Join Date
    May 2003
    Location
    Sydney
    Posts
    1,123

    Re: VB6-pop3 email

    on demand...
    Attached Files Attached Files

  15. #15
    Addicted Member mfurqan's Avatar
    Join Date
    Oct 2005
    Location
    Pakistan
    Posts
    176

    Re: VB6-pop3 email

    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'
    Muhammad Furqan Attari.

  16. #16
    Addicted Member mfurqan's Avatar
    Join Date
    Oct 2005
    Location
    Pakistan
    Posts
    176

    Re: VB6-pop3 email

    self resolved problem.

    its working great
    but it only downloads Unread Messages ?

    and really don't know what that Text4 box realy is for
    Muhammad Furqan Attari.

  17. #17
    Addicted Member mfurqan's Avatar
    Join Date
    Oct 2005
    Location
    Pakistan
    Posts
    176

    Re: VB6-pop3 email

    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.
    Muhammad Furqan Attari.

  18. #18

    Thread Starter
    Frenzied Member
    Join Date
    May 2003
    Location
    Sydney
    Posts
    1,123

    Re: VB6-pop3 email

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

  19. #19
    Addicted Member mfurqan's Avatar
    Join Date
    Oct 2005
    Location
    Pakistan
    Posts
    176

    Re: VB6-pop3 email

    well does it delete all messages from pop3 account after downloading them. ?
    Muhammad Furqan Attari.

  20. #20
    Addicted Member mfurqan's Avatar
    Join Date
    Oct 2005
    Location
    Pakistan
    Posts
    176

    Re: VB6-pop3 email

    Quote Originally Posted by mebhas
    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
    Muhammad Furqan Attari.

  21. #21

    Thread Starter
    Frenzied Member
    Join Date
    May 2003
    Location
    Sydney
    Posts
    1,123

    Re: VB6-pop3 email

    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.

  22. #22
    Addicted Member mfurqan's Avatar
    Join Date
    Oct 2005
    Location
    Pakistan
    Posts
    176

    Re: VB6-pop3 email

    okay sir no problem
    Muhammad Furqan Attari.

  23. #23
    Addicted Member mfurqan's Avatar
    Join Date
    Oct 2005
    Location
    Pakistan
    Posts
    176

    Re: VB6-pop3 email

    i wish if u can do it earlier because i have made .TXT email reader
    Muhammad Furqan Attari.

  24. #24

    Thread Starter
    Frenzied Member
    Join Date
    May 2003
    Location
    Sydney
    Posts
    1,123

    Re: VB6-pop3 email

    ok, here's some quick code, see if you can make it any better....
    VB Code:
    1. 'make reference to microsoft scripting runtime, we need it for file system object
    2. 'make these variables, fso as filesystemobjects, readFile as textstream, readString as string
    3. set readfile=fso.opentextfile "your file name", forreading
    4. do while not readfile.eof
    5.     readfile.readline readstring
    6.     processString readstring 'processString is a sub, need some thinking for that
    7. loop

    now create a sub processString(inString as string)

    some part of the code.
    VB Code:
    1. 'i am using if statements here coz i know there are more conditions than a select case can handle.
    2.  
    3. if ucase$(left$(instring,5))="FROM:" then
    4.     'this is whom u recieved the message from. the remaining part of the line is the sender.
    5. elseif ucase$(left$(instring,5))="DATE:" then
    6.     'this is the date u recieved the message.
    7. elseif ucase$(left$(instring,3))="TO:" then
    8.     'this is the reciever of the message
    9. elseif ucase$(left$(instring,8))="SUBJECT:" then
    10.     'this is the subject of the message.
    11. elseif len(instring)=0 then
    12.     'we have reached the beginning of the message, we can now write all the remining part of the email as the body.
    13. endif

  25. #25

    Thread Starter
    Frenzied Member
    Join Date
    May 2003
    Location
    Sydney
    Posts
    1,123

    Re: VB6-pop3 email

    as u can see, i have just typed in the code and it should be debugged thoroughly and proper declarations should be made.

  26. #26
    Addicted Member mfurqan's Avatar
    Join Date
    Oct 2005
    Location
    Pakistan
    Posts
    176

    Re: VB6-pop3 email

    really can't understand it sorry...
    Muhammad Furqan Attari.

  27. #27
    Bahamas Mon
    Join Date
    May 2006
    Location
    Nassau, Bahamas
    Posts
    2,989

    Re: VB6-pop3 email

    Very well done mebhas ...

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

  28. #28
    New Member
    Join Date
    Nov 2006
    Posts
    10

    Re: VB6-pop3 email

    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?

  29. #29
    Fanatic Member
    Join Date
    Oct 2007
    Posts
    544

    Re: VB6-pop3 email

    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

  30. #30
    Member
    Join Date
    Aug 2002
    Posts
    58

    Re: VB6-pop3 email

    Hmm, why can't i find the winsock control?
    -------------------------------
    StupidMonkee

  31. #31

    Thread Starter
    Frenzied Member
    Join Date
    May 2003
    Location
    Sydney
    Posts
    1,123

    Re: VB6-pop3 email

    Quote Originally Posted by martind1 View Post
    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

  32. #32
    Member
    Join Date
    Aug 2002
    Posts
    58

    Re: VB6-pop3 email

    Shoot. Does this not do SSL pop?
    -------------------------------
    StupidMonkee

  33. #33

    Thread Starter
    Frenzied Member
    Join Date
    May 2003
    Location
    Sydney
    Posts
    1,123

    Re: VB6-pop3 email

    nope, just plain pop

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.