Wednesday, November 3, 2010

Reading an email using gmail pop3 server from telnet (using stunnel) in windows.


Last time, I showed how to send an email using a gmail account and gmail's smtp server,

Now it's time to login to our inbox and read the email using gmail's pop3 server and stunnel.

First let's configure stunnel, I will listen on port 2302, and gmail's pop3 server is pop.gmail.com port: 995

so in stunnel.conf we write the following:
[pop3s]
accept  = 2302
connect = pop.gmail.com:995

for more info about configuring stunnel, please refer to my other post Sending an email using gmail smtp server from telnet (using stunnel) in windows,

After starting or reloading stunnel, let's start the command line cmd:

in cmd, write the following:
telnet localhost 2302


+OK Gpop ready for requests from xxx.xxx.xxx.xxx eq6pf732841wbb.2
USER etienne.rached@gmail.com
+OK send PASS
PASS YourPassword
+OK Welcome.
STAT
+OK 107 2614891
LIST
+OK 107 messages (2614891 bytes)
1 2989
2 3352
3 3029
4 3240
5 844
.
.
.
103 13741
104 31400
105 170674
106 123019
107 13170
RETR 91
Content of the message will be displayed here
QUIT
Connection to host lost.

USER the gmail address... (can be without @gmail.com)
PASS the password
STAT will list the number of messages you have and its size in bytes.
LIST will show the list of all the messages, their number and their size.
RETR "#Number" will retrieve the message number.
QUIT will disconnect you from the server.

You can also use DELE  "#Number" to delete a message, and RSET to restore all the message that was deleted, but make sure you use RSET before using QUIT, or the messages can't be restored later.

Friday, October 29, 2010

Sending an email using gmail smtp server from telnet (using stunnel) in windows.



This article explains how to send an email from gmail smtp server using telnet and stunnel on a windows operating system.

Sometimes you may want to test how an email is sent in telnet, maybe in order to create a program that will send and receive emails.

For example, developers who use J2ME to write mobile applications know that there is no existent library or API that allow you to send or receive an email through smtp servers that requires authentication and SSL, and in order to implement such an application, the developer must use SecureSocket and Inputsteam and Outputstream and must know how the smtp server (or pop3 in case of receiving an email) works and how is it gonna respond to commands.

By default, Telnet doesn't support secure connections or SSL, for that we're gonna have to redirect (tunnel) the telnet commands to the stunnel to encrypt them and send them to the gmail smtp server.

First, we're gonna need stunnel which can be downloaded from http://www.stunnel.org

After downloading and installing stunnel, we have to configure the stunnel.conf file located in stunnel directory.

in the stunnel.conf, every line that start with a semicolon is a comment,
Make sure to uncomment  client = yes
and edit(or create) the smtps part as following:
[smtps]
accept  = 2301
connect = smtp.gmail.com:465

- smtps is for secure smtp servers, (Note: If [smtps] doesn't work, try to use [ssmtp] instead)
- Accept indicates the port that you want to use for tunneling,
- Connect indicates the server we're connecting to, in our case, its the gmail smtp server, with port 465.
- Port 465 is the port for smtps, while port 587 is for TLS/STARTTLS.


the stunnel.conf file should now look like the following:
socket = l:TCP_NODELAY=1
socket = r:TCP_NODELAY=1

client = yes

[smtps]
accept  = 2301
connect = smtp.gmail.com:465

Save the stunnel.conf file and in stunnel program click on configuration=>Reload.


Now it's time to send the email.

In windows, go to start=>run=>cmd
Once the command line is opened write the following: telnet localhost 2301

you should see the following:
220 mx.google.com ESMTP f14sm1400408wbe.2

we send a hello message:
helo google
250 mx.google.com at your service
ehlo google
250-mx.google.com at your service, [212.28.228.49]
250-SIZE 35651584
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH
250 ENHANCEDSTATUSCODES

Then the authentication request:
 AUTH LOGIN
334 VXNlcm5hbWU6

Now we have to authenticate by using our gmail address and password.
However since this is an encrypted session, we're gonna have to send the email and password encrypted in base64. To encrypt your email and password, you can use a program or an online website to encrypt it (search on google for: base64 online encryption).
For example the sentence "my email address" in base64 will become: bXkgZW1haWwgYWRkcmVzcw==
and the word "password" will become: cGFzc3dvcmQ=

Now you should have your email address and password encrypted in base64, go back to the command line and write your base64 email first, (I will use "my email address" as my email address and "password" as my password for this example):
bXkgZW1haWwgYWRkcmVzcw==
334 UGFzc3dvcmQ6

Enter your password:
cGFzc3dvcmQ=
235 2.7.0 Accepted

Enter your email:
MAIL FROM:<etienne.rached@gmail.com>
250 2.1.0 OK x23sm1104292weq.10

Enter the recipient email (I'll email myself for testing):
RCPT TO:<etienne.rached@gmail.com>
250 2.1.5 OK x23sm1104292weq.10
 
Now comes the message part:
DATA
354  Go ahead x23sm1104292weq.10
From: Etienne <etienne.rached@gmail.com>
To: Meeeeeeee <etienne.rached@gmail.com>
Subject: Testing email from telnet
This is the body

Adding more lines to the body message.
Finish the DATA with a dot:
.
250 2.0.0 OK 1288307376 x23sm1104292weq.10


And quit:

QUIT
221 2.0.0 closing connection x23sm1104292weq.10
Connection to host lost.

The email should now be sent and you can find it in your "sent mail" in your gmail.


Using Telnet with stunnel is very easy, I will try and show how to receive an email with pop3 using telnet, and how to send an email using TLS in my future posts.

Hope you enjoyed this one :)

Hello World!

Hello and welcome to my blog!

This blog is mainly about new Technologies, developing, programing and basically anything computer related.

I hope you enjoy it and learn new stuff while reading :)