Jump to content
Sign in to follow this  
therks

TCP+POP to check/read email

Recommended Posts

therks

I'm using the TCP commands to log into a POP server, but I can't get the "retr" command to work properly. When using Telnet the same command works fine, but AutoIt doesn't seem to receive the email contents, TCPRecv just returns an empty string.

This email account currently has 3 messages in it: 1 read, 2 unread. I haven't figured out how to determine this status via the POP commands (either Telnet or AutoIt's TCP), if someone knows how to do this I'd very much appreciate it. The username and password are provided in the script, please don't abuse the account.

Opt('TrayIconDebug', 1)
GUICreate('CheckMail', 300, 200)
$status = GUICtrlCreateEdit('Initiating...', 5, 5, 290, 190)
GUISetState()

TCPStartup()

$ip = TCPNameToIP('mail.therks.com')
;~ $user = InputBox('Username', 'Username (don''t forget @domain):', '', '', 200, 150)
;~ $pass = InputBox('Password', 'Password for ' & $user & ':', '', '*', 200, 150)
$user = 'autoit@therks.com'
$pass = 'jonbennett'

$socket = TCPConnect($ip, 110)
If $socket = -1 Then
    GUICtrlSetData($status, 'Error: Bad connection' & @CRLF)
Else
    $step = 'login'
    Do
        $recv = TCPRecv($socket, 512)
        If @error Then
            GUICtrlSetData($status, 'Notice: Disconnected!' & @CRLF, 1)
            $socket = -1
        ElseIf StringInStr($recv, '+OK') = 1 Then
            GUICtrlSetData($status, $recv & @CRLF, 1)
            Switch $step
                Case 'login'
                    GUICtrlSetData($status, 'Login: Username...' & @CRLF, 1)
                    _TCPSend($socket, 'user ' & $user & @CRLF)
                    $step = 'pass'
                Case 'pass'
                    GUICtrlSetData($status, 'Login: Password...' & @CRLF, 1)
                    _TCPSend($socket, 'pass ' & $pass & @CRLF)
                    $step = 'stat'
                Case 'stat'
                    GUICtrlSetData($status, 'Logged In: Check for mail...' & @CRLF, 1)
                    _TCPSend($socket, 'stat' & @CRLF)
                    $step = 'check'
                Case 'check'
                    $match = StringRegExp($recv, '\+OK ([0-9]+)', 2)
                    If Not @error Then
                        GUICtrlSetData($status, 'Success: There are ' & $match[1] & ' email(s) in the inbox' & @CRLF, 1)
                        If $match[1] > 0 Then
                            _TCPSend($socket, 'retr 1' & @CRLF)
                            $step = 'read'
                        Else
                            GUICtrlSetData($status, 'Disconnecting', 1)
                            TCPCloseSocket($socket)
                            $socket = -1
                        EndIf
                    Else
                        GUICtrlSetData($status, 'Error: Improper format:' & $recv & @CRLF, 1)
                        GUICtrlSetData($status, 'Disconnecting', 1)
                        TCPCloseSocket($socket)
                        $socket = -1
                    EndIf
                Case 'read'
                    $recv = TCPRecv($socket, 512)
                    GUICtrlSetData($status, 'Email #1 content:' & @CRLF & '----------' & @CRLF & $recv & @CRLF & '----------' & @CRLF, 1)
                    GUICtrlSetData($status, 'Disconnecting', 1)
                    TCPCloseSocket($socket)
                    $socket = -1
            EndSwitch
        ElseIf StringInStr($recv, '-ERR') = 1 Then
            GUICtrlSetData($status, 'Error: ' & $recv & @CRLF, 1)
            GUICtrlSetData($status, 'Disconnecting', 1)
            TCPCloseSocket($socket)
            $socket = -1
        EndIf
    Until $socket = -1
EndIf

Do
Until GUIGetMsg() = -3

Func _TCPSend($socket, $data)
    TCPSend($socket, $data)
    GUICtrlSetData($status, '> ' & $data, 1)
EndFunc

More on the POP commands can be found here: http://rfc.net/rfc1939.html

Share this post


Link to post
Share on other sites
YOUF

expect someone to complete it

Share this post


Link to post
Share on other sites
SmOke_N

expect

There's your first mistake.

Edit:

Case 'read'
                    Local $sBackup = ""
                    $recv = ""
                    ;Header
                    While $recv = ""
                        $recv &= TCPRecv($socket, 512)
                        Sleep(10)
                    WEnd
                    $sBackup = $recv
                    $recv = ""
                    ;Body
                    While $recv = ""
                        $recv &= TCPRecv($socket, 512)
                        Sleep(10)
                    WEnd
                    ;You'll have to work out others
                    $recv = $sBackup & @CRLF & $recv
                    GUICtrlSetData($status, 'Email #1 content:' & @CRLF & '----------' & @CRLF & $recv & @CRLF & '----------' & @CRLF, 1)
                    GUICtrlSetData($status, 'Disconnecting', 1)
                    TCPCloseSocket($socket)
                    $socket = -1
            EndSwitch
Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
MicahC

Hello everybody,

I know this is an old post, but I was wondering if anyone knew how to get gmail working with this. I have tried a few times, but am having no success. I may be off on the tcp ports. If anyone knows how to get it setup with gmail that would be reallly appreciated!

Micah C

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×