I was a little bit curious about the inner workings of SMTP, but googling it reveals a lot of information on the protocol itself. Email client uses SMTP to talk to email server, which talks directly to another email server to deliver the message. Person receiving the email uses POP or IMAP or some other protocol to receive the email.
The details of the SMTP protocol (commands, packet structure, etc) are easy to find, but what no online site seems to mention is how the email server finds another email server to send the email to. For example, if jim@yahoo.com wants to email tom@hotmail.com, how does yahoo’s email server locate the hotmail server?
Is it just done the same way as for web pages? yahoo’s email server uses a DNS server to convert hotmail.com to an IP address, and then makes a TCP connection to that IP address on port 25?
Pretty much. What happens when you want to send an email to, say, joe@example.org, is that the SMTP server looks up example.org in DNS, and requests its “MX” (Mail Exchanger) record. That returns the address of the mail server responsible for example.org. It then contacts that mail server and tells it that it has a message for Joe.
Thanks Crescend. I guess that answers my next question - what if the hotmail.com web server has a different IP address to the hotmail.com email server? So the DNS server has different records for email and web servers. Is there a list of types of records that DNS can have (email and web I’m assuming are only two of many)?
In addition to the above info which is good & valuable. I would just add that you can play with this by opening a telnet session to that IP address on port 25 and become a human SMTP client.
Oooh, that sounds like fun. I have a gmail email address, for example: can I just go “telnet gmail.com 25” and start typing in SMTP commands and an email? Is using telnet in this way identical to opening a regular TCP socket?
Opens command prompt to try it
C:\Documents and Settings\Just>telnet gmail.com 25
Connecting To gmail.com…Could not open connection to the host, on port 25: Connect failed
Damn, that doesn’t work. My firewall prompted me a few times to allow the outgoing connection (I hit ‘yes’ everytime), and then it seemed to attempt to connect for a few seconds, before I get the “connect failed” error.
I’m guessing it’d probably be better for me to set up my own email server to learn how this works, but I’m still curious: why does the connection fail? How does gmail know I’m not an actual email server?
Remember that MX records are separate from regular DNS records. To talk to gmail.com’s SMTP server, you need to first look up the MX record for gmail.com:
$ host -t mx gmail.com
gmail.com mail is handled by 5 gmail-smtp-in.l.google.com.
gmail.com mail is handled by 10 gsmtp163.google.com.
gmail.com mail is handled by 10 gsmtp171.google.com.
gmail.com mail is handled by 10 gsmtp185.google.com.
gmail.com mail is handled by 10 gsmtp171-2.google.com.
gmail.com mail is handled by 10 gsmtp185-2.google.com.
Those are (some of) the gmail SMTP servers - you can telnet to any of them on port 25 and send yourself (or anyone else on gmail) a message.
Cool, I just sent myself an email to my gmail account. Thanks friedo. On Windows, I discovered I could get the same output as you did using the host command by going “nslookup -querytype=mx gmail.com”. Then I telnetted to port 25 on the first of these in the list, and sent myself an email.
The only weird behaviour I noticed was that gmail wouldn’t accept some of my commands the first time, but would on the second. For example, I went “RCPT TO: <myemailaddress@gmail.com>”, and it responded with “550 5.7.1 No such user 12si191267nzn” on the first attempt and “502 5.5.1 Unrecognized command 12si191267nzn” on the second attempt. On the third attempt, it worked fine. I also noticed that my email wasn’t sent quite the same way I typed it (eg I tried to make the subject of the email “Testing SMTP”, but it actually came out as “Testing SMPTY” followed by a few garbage characters).
Any ideas why? I suspect it’s Windows’ telnet client, which I’ve found to be pretty lousy in the past. Or are there any more likely explanations?
I just tried one of the gmail SMTP servers with linux telnet and it worked fine for sending to my gmail account. I’m guessing it’s an issue with Windows telnet. (Probably sending weird carriage returns or something. Windows is like that.)