Page 1 of 1

Check for eMail from any/all accounts

Posted: Thu Aug 28, 2003 6:16 am
by oldefoxx
I took one of the contributed programs and modified it so that I can run it and tell if either my wife or I have new email on any of our accounts. I left four pseudo-accounts in the source so that you can play with it a bit. Please read the comments on ways to enhance the program further.

My thanks to the author, in that without the original code, learning how to query email servers would have been a lot harder to figure out.

Note that finding POP3 and SMTP addresses is not normally the same as the provided @ispname.com that you use to receive new email after your account is set up. But the information is either available through your existing email client software, or should be available from your email provider's technical support.

Code: Select all

; English forum: http://jconserv.net/purebasic/viewtopic.php?t=6668&highlight=
; Author: TerryHough
; Date: 23. June 2003

Structure AccountInfo
  Server.s
  EmailID.s
  password.s
EndStructure
Dim accounts.accountinfo(20)
;enter all the email accounts you want checked here.

;Note that you could read the entries from a text
;file you set up for the purpose, even encrypt and
;decrypt the contents as necessary, or add code to
;request the individual account passwords at the
;start of the program.  If the program becomes
;resident, you could requery the mail serviers as
;frequently as necessary until you shut the PC down.

;If set on an alarm start basis, you would want the
;passwords protected from being found and used by
;others.
accounts(1)\Server="mail.earthlink.net"
accounts(1)\EmailID="First.LastName"
accounts(1)\password="primary.password"

accounts(2)\Server="pop.trends.net"
accounts(2)\EmailID="screenname"
accounts(2)\password="okaydokee"

accounts(3)\Server="mail.earthlink.net"
accounts(3)\EmailID="Wife's.emailname"
accounts(3)\password="herpassword"

accounts(4)\Server="pop.sprynet.com"
accounts(4)\EmailID="Her.Othername"
accounts(4)\password="otherpassword"
For a.l=1 To 20
  If accounts(a)\EmailID>""
    number_of.l=a
  EndIf
Next
crlf$ = Chr(13)+Chr(10) 
*Buffer = AllocateMemory(0, 5000)

;----------------------------------------------------------------------- 
; POP3 - Check number of messages 
; Written by TerryHough - last updated 06/23/2003 
;----------------------------------------------------------------------- 
For idx.l=1 To number_of
  If InitNetwork()=0 
    MessageRequester("Error", "Can't initialize the network !", #MB_ICONSTOP) 
    Goto pau 
  EndIf
  ConnectionID=OpenNetworkConnection(accounts(idx)\server, 110) 
  If ConnectionID=0
    MessageRequester("Error","Unable to connect to "+accounts(idx)\server,#MB_ICONERROR) 
    Goto pau
  EndIf
; Most POP3 servers send an ID message back upon connection 
; check If OK And respond with USER ID 
  RequestLength.l = ReceiveNetworkData(ConnectionID, *Buffer, 5000)  
  Result$=PeekS(*Buffer,RequestLength) 
  Debug(result$)
  If Left(Result$,3)<>"+OK"
    MessageRequester("error","Did not receive acknowledge from "+accounts(idx)\server,#mb_iconerror)
    Goto pau
  EndIf
; Send the USER ID now.  Some POP3 servers don't prompt for it, 
; but expect it To be sent here. 
  SendNetworkString(ConnectionID,"USER "+accounts(idx)\EmailID+crlf$) 
; Should get back a password request if USER ID is valid 
  RequestLength.l = ReceiveNetworkData(ConnectionID, *Buffer, 5000)  
  result$=PeekS(*Buffer,RequestLength) 
  Debug(result$)
  If Left(Result$,3)<>"+OK" 
    MessageRequester("Error",accounts(idx)\server+" did not accept "+accounts(idx)\EmailID,#MB_ICONERROR) 
    Goto pau
  EndIf
; Send the PASSWORD now 
  SendNetworkString(ConnectionID, "PASS "+accounts(idx)\password+crlf$) 
; Should get back a welcome message if password is accepted 
  RequestLength.l = ReceiveNetworkData(ConnectionID, *Buffer, 5000)  
  Result$=PeekS(*Buffer,RequestLength) 
  Debug(result$)
  If Left(Result$,3)<>"+OK" 
    MessageRequester("Error",accounts(idx)\server+" did not accept the PASSWORD provided.",#MB_ICONERROR) 
    Goto pau
  EndIf
; Send the LIST command to get number of available messages 
  SendNetworkString(ConnectionID, "LIST"+crlf$) 
; Should get back a number of available messages 
; Interpret based on the response receive, they vary and may 
; include info about the message ids 
  RequestLength.l = ReceiveNetworkData(ConnectionID, *Buffer, 5000)  
  Result$ = PeekS(*Buffer,RequestLength) 
  Debug(result$)
  Position.l = FindString(Reply$, "messages", 1) 
  If Position 
    MessageRequester("Info",Result$,#MB_ICONINFORMATION)
;   You could use VAL(Left(Result$,position-2)) to
;   determine how many messages, and place an Icon for
;   each user of sound a different tone for each user
;   mail is detected.  But to do this properly, you
;   would have to ensure that you only redisplay the
;   icon or make the sound again if the amount of
;   mail increases over the amount last reported.    
  EndIf 
  SendNetworkString(ConnectionID,"QUIT"+crlf$)
  RequestLength.l = ReceiveNetworkData(ConnectionID, *Buffer, 5000)  
  Result$ = PeekS(*Buffer,RequestLength) 
  Debug(result$)
pau:
  CloseNetworkConnection(ConnectionID) 
Next
End

;Note:  the following list of POSSIBLE mail servers
;is only To be used For debugging purposes.  It
;first shows that there is no standard way to name
;a mail server.  While you might see text with
;(POP3) or (SMTP) included, this usually is just
;used to indicate incoming mail protocol/server
;(POP3), ;and outgoing mail protocol/server (SMTP).

;Second, these servers will reject your efforts if
;you do not have a valid email account userid and
;password.  After setting up a valid email account
;with any ISP or service provider, you will have
;to check the email client for the POP3 and SMTP
;settings to use with this program, or call their
;customer service for the needed information.  Many
;free email services are available, but some only
;provide http:// services for their clients, meaning
;they will not work with this sample code.  There
;are other email protocols as well, which may use
;a different port number besides 110, and which also
;involve different commands and capabilities.

;POP3 Servers:              SMTP Servers
;mail.trends.net
;pop.protegy.net
;pop.amex.mail.net
;postoffice.worldnet.att.net
;popd.ix.netcom.com
;sprynet.com
;mail.earthlink.net       smtpauth.earthlink.net
;fastmail.fm              fastmail.fm
;pop.sbcglobal.net        smtpauth.prodigy.net
;pop.sprintpcs.com
;mail.mac.com             smtp.mac.com              
;pop.ameritech.yahoo.com  smtp.ameritech.yahoo.com  
;pop.flash.yahoo.com      smtp.flash.yahoo.com 
;pop.nvbell.yahoo.com     smtp.nvbell.yahoo.com 
;pop.pacbell.yahoo.com    smtp.pacbell.yahoo.com 
;pop.prodigy.yahoo.com    smtp.prodigy.yahoo.com 
;pop.sbcglobal.yahoo.com  smtp.sbcglobal.yahoo.com 
;pop.snet.yahoo.com       smtp.snet.yahoo.com 
;pop.swbell.yahoo.com     smtp.swbell.yahoo.com 
;pop.wans.yahoo.com       smtp.wans.yahoo.com 

Correction to code

Posted: Thu Aug 28, 2003 6:46 am
by oldefoxx
not the line where position.l=FindString(reply$,"messages",1)
Change the Reply$ to Receive$, and it should work correctly.

Re: Check for eMail from any/all accounts

Posted: Thu Aug 28, 2003 2:34 pm
by TerryHough
oldefoxx wrote:My thanks to the author, in that without the original code, learning how to query email servers would have been a lot harder to figure out.
You are welcome. Glad you have gotten some use of it.

My full version is a bit more sophisticated, has a better developed user
interface and works from a .INI file. The initial run of the program asks
for the server address and password to create a default .INI file. This
can then be modified in the Config menu. The program runs minimized
in the SysTray and has optional visual and sound alarms and even allows
the user to "read" the messages by starting their email program from
the user interface. It also allows a user defined interval for rechecking
the server.

The SysTray icon is yellow if there are no message waiting, green if there
are messages not yet downloaded. There is even a way to see a verbose
conversation between the POP3 server and the program.

So, to check multiple servers I just run instances of the program from
separate folders with a .INI file for that server.

Drop me a PM with your email address if you would like for me to send
you a copy of the EXE. (73kB)

Regards,
Terry

Edited later

I Appreciate the Offer

Posted: Thu Aug 28, 2003 5:24 pm
by D'Oldefoxx
Thank you for your generous offer. Actually, I am more interested in source code to help me learn some of the capabilities and tricks of the trade. I have already found that just the bit I wrote is sufficient for my purposes - I made s small change so that I put a MessageRequester() if if I have any Val() for the number of messages, so I only see the Email accounts that have messages waiting. Now if I just could figure out how to fire up Incredimail ifor a specific user! I think I have it defaulted to the last user. and now it would be more convenient to specify that at the onset.

Fact is, with my own accounts and my wife's, I can close my email cleint and just manually run the compiled version every once in awhile to see if either of us has mail waiting from any of our accounts now. So I stuck a shortcut up on the destkop, located a .exe that had a nice Icon associated with it, and "borrowed" the Icon for my shortcut. Looks great and works great. I didn't realize how irksome it can get to be interrupted while using the keyboard by a "you have mail" notification - sepecially since it interrupts my typing, my concentration, sometimes causes the window to lose focus, and may even to be responsible for short freezes where the characters won't appear as I type (presumably gone to check the mail).

Wo while I really appreciate the offer, I think I will just keep playing with the code itself. That's the fun of programming.

And for anyone just getting into this subject, you might want to check out RFC (Request For Comments) number 1939, which covers POP3 mail service. Just search for RFC1939 on the internet. It gives the commands and fuinctions that you can use to build your own e-mail downloader. There is another one that covers SMTP (Simple Mail Transfer Protocol), which covers how you can send mail as well.