Important alert: (current site time 7/16/2013 1:21:57 AM EDT)
 

article

[ Parse POP3 Message Header ]

Email
Submitted on: 12/18/2004 12:16:45 PM
By: Eric Wolcott 
Level: Beginner
User Rating: Unrated
Compatibility: VB 5.0, VB 6.0, VB Script
Views: 47768
author picture
(About the author)
 
     This code shows you how to recieve a message header through POP3 and parse it to get the information you want from it. It took me quite a bit of searching to figure out how so I decieded I would make it a bit easier for others looking to do the same. This is not meant to be a full POP3 Tutorial but will show you how to connect to a server and get the messages header and parse it. The formatting of the tut looked a little message up so I included a project ready to go. Dont forget to change the mailserver, username, and pass to what you want.

This article has accompanying files
 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				
'Includes: 
' Form: Name = form1
' Winsock Control: Name = Winsock1
'----------------------------------
Public prevCmd As String
Public mailServer As String
Public username As String
Public password As String
Private Sub Form_Load()
prevCmd = ""
'Set your mailsever, username, and password
mailServer = "mail.yourmailserver.net"
username = "yourusername"
password = "yourpassword"
'Connect to mail server
winsock1.Connect mailServer, 110
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim data As String
'Get winsock data and find next command to send
Select Case prevCmd
Case "USER"
'Send password
winsock1.senddata "PASS " & password & vbCrLf
prevCmd = "PASS"
Case "PASS"
'Send TOP 1 to get message header
winsock1.senddata "TOP 1" & vbCrLf
prevCmd = "TOP"
Case "TOP"
'Parse message
winsock1.getdata data
parseMSG (data)
Case ""
'Send username.
winsock1.senddata "USER " & username & vbCrLf
prevCmd = "USER"
End Select
end sub
Private Sub parseMSG(data As String)
'Get To, From, and subject
Dim dataHold As Variant 'Data Holder for Split function. Creates an array of lines
Dim epos As Long 'Ending Position for Mid function
Dim spos As Long 'Starting Position for Mid function
'Split data where there are vbCrLf characters. Which is at the end of every line
dataHold = Split(data, vbCrLf)

'Examine Each line of data
For i = LBound(dataHold) To UBound(dataHold)
If dataHold(i) <> "" Then 'If the line is not blank then
'Selects the beginning of each line to see what info it contains
Select Case UCase(Left(dataHold(i), InStr(1, dataHold(i), ":")))
Case "FROM:" 'If its the sender
spos = InStr(1, dataHold(i), ":") + 2 'Starting Position for Mid function
epos = Len(dataHold(i)) - 1 'Ending Position for Mid function
MsgBox Mid$(dataHold(i), spos, epos - spos + 2) 'Select the middle of the data, not including the beginning tag or the vbCrLf's
Case "TO:" 'If its your email
spos = InStr(1, dataHold(i), ":") + 2 'Starting Position for Mid function
epos = Len(dataHold(i)) - 1 'Ending Position for Mid function
MsgBox Mid$(dataHold(i), spos, epos - spos + 2) 'Select the middle of the data, not including the beginning tag or the vbCrLf's
Case "SUBJECT:" 'If its the subject
spos = InStr(1, dataHold(i), ":") + 2 'Starting Position for Mid function
epos = Len(dataHold(i)) - 1 'Ending Position for Mid function
MsgBox Mid$(dataHold(i), spos, epos - spos + 2) 'Select the middle of the data, not including the beginning tag or the vbCrLf's
End Select
End If
Next i
End Sub

winzip iconDownload article

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
  3. Scan the source code with Minnow's Project Scanner

If you don't have a virus scanner, you can get one at many places on the net including:McAfee.com

 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.


Other 57 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
7/18/2005 4:32:46 AMCrack Head again

Coul dyou look at my code and tell how to get it to parse the entire file instead of just one instance? The program works fine so long as there is only one instance of data to parse, but if there are more than one, it will get the first instance, ignore the rest and move to the next file.

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=61750&l ngWId=1

(If this comment was disrespectful, please report it.)

 
7/18/2005 4:34:39 AMCrack Head again

Could you take a look at my code and tell me what I am missing? It parses the files ok for each file you dar and drop on it, but if a file has more than one set to look for, it only grabs the first one and moves to the next file.

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=61750&l ngWId=1

I
see your parse code for the POP3 files and I want to implement something similar to my program.
(If this comment was disrespectful, please report it.)

 
8/26/2005 10:30:29 PMvictor

Can help me to get source for parse IMAP message Header?
(If this comment was disrespectful, please report it.)

 
5/3/2007 8:22:59 AMsutopo

this program can't access gmail why
thanks
(If this comment was disrespectful, please report it.)

 
5/11/2007 9:41:30 AMJoaquin Marcher

sutopo, maybe you don't put the real post of gmail, and remember, gmail works with SSL
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.