RSS 2.0 Feed
RSS 2.0


Atom 1.0 Feed
Atom 1.0

  SMTP Relaying 101 


qt8gt0bxhw|20009F4EEE83|RyanMain|subtext_Content|Text|0xfbff1c0100000000d800000001000c00

I was speaking to a friend the other day about relaying e-mails via a local SMTP server. There was some problem with some code and the e-mails were not going out. In cases like this one of the first things I like to do (especially when you don't get to access the SMTP server directly, but you're relying on some other IT person to set up the relaying permissions correctly) is to manaully connect to the SMTP server and test sending out an e-mail or two.

<sidetrack>The truth is that command-line stuff now days are treated as old-school. Back in the day I grew up in a terminal window. Text and commands are pure love for me. Don't get me wrong, I love the advances made and where we are today as far as the whole computing user-experience goes. I'm not one of those guys that rant about how things were better back in the "good ol' days" or anything. But I am not about to abandon roots and forget how to actually do things via the command line.</sidetrack> Anyway, something that is useful to know how to do for any developer that ever writes code to send out e-mails is to know how to manually relay mail through an SMTP server.

To make the connection, you'll need to fire up our command-line friend CMD (If you don't know how to start a command window then you're reading the wrong blog, move along). We'll do two things to check that we can sucessfully relay from your current machine to your SMTP server (which might also be your current machine or elsewhere - we're just trying to test that the server will accept relaying from your IP address/machine). First is to check for connectivity between your machine and the server to make sure you're not blocked somewhere along the line. Second is to actually relay an e-mail via the server.


Checking Connectivity with the SMTP Server

An easy way to check connectivity with the server (remember we're staying command-line here) is to use the PORTQRY command-line tool (A free download from Microsoft). You can use portqry to check to make sure that TCP traffic can make it from your machine to the SMTP port 25 on the server. To do so run the following command:

portqry -n MYSERVER -p tcp -e 25

This will query the server to check for connectivity on port/endpoint 25 using the TCP protocol. What you want to hear is "LISTENING". You'll get back 1 of 3 results; listening (A process is listening on the port on the server), not listening (No process is listening on the target port on the server), or filtered (portqry did not receive a response from the port. A process may or may not be listening on the port).


Relaying an E-mail via SMTP

Now on to the fun stuff. Sending the e-mail. On the command-line, open a telnet session.

telnet MYSERVER 25

Just a tip, if you don't see what you type then you need to turn on the local echo by typing (set local_echo or set localecho on XP machines). Anyway, make sure you specify port 25 (notice the 25 after the server name).

From here, I'm going to skip the detailed explanations and get to the point. After all, if you're really that interested read the spec. Basically, there are 4 commands you'll need, EHLO or HELO (used by the sending host to identify itself), MAIL FROM (to specify who the mail is from), RCPT TO (to indicate the recipient of the e-mail - this can be repeated multiple times for multiple recipients), and DATA (for the actual text body of the e-mail - you can also specify subject here). One thing to note, whether to use EHLO or HELO for the handshake depends on the capabilities of the SMTP server. All SMTP servers will respond to HELO, older servers might not understand EHLO. The difference between the two is that basically the server's response to EHLO will return additional info about the server's abilities. Also, on the subject of handshakes, to be RFC 2821 compliant, the system name your computer transmits in the EHLO handshake must be identical to the DNS name of the IP address that your computer is currently attached to.

Here's a sample (lines typed by you are in blue, the gray lines are server responses):

220 deathstar.SUPERFREAK.local Microsoft ESMTP MAIL Service, Version: 6.0.2600.2
180 ready at Fri, 6 Jan 2006 16:59:58 -0700
HELO localhost
250 deathstar.SUPERFREAK.local Hello [127.0.0.1]
MAIL FROM: bgates@microsoft.com
250 2.1.0 bgates@microsoft.com....Sender OK
RCPT TO: someone@mail.com
250 2.1.5 Administrator@deathstar.SUPERFREAK.local
DATA
354 Start mail input; end with <CRLF>.<CRLF>
SUBJECT: This is a test.
Hello, this is just a test.
How are things?
Bye, -Ryan . 250 2.6.0 <DEATHSTARoqwWADykQv00000001@deathstar.SUPERFREAK.local> Queued mail for delivery

The important thing to note here is the single "." (period) on the last line, followed by a CRLF. This is what tells the server that you're done with your text and to queue for delivery. If the server gives you the following message after typing the RCPT TO line:

RCPT TO: someone@mail.com
550 5.7.1 Unable to relay for someone@mail.com

Then that means that you're not allowed to relay from the machine you are on via the connected SMTP server. Now you can go back to the IT person to let them know that he has not opened up relaying from the machine (IP Address) you're using.

Tip: never open relaying on an SMTP server from everyone. If you must relay on a server then only open relaying from the specific IP address only.

Also, I tend to only use anonymous relaying if it is a specific requirement or need. When possible, always use proper authentication to connect as a user on the server (See how do I authenticate to send an e-mail for details).




                   



Leave a comment below.

Comments

  1. sanyogita 1/17/2006 9:14 PM
    Gravatar
    HELLO,
    while searching for the following error in google, I came to your link.

    "Warning: mail() [function.mail]: SMTP server response: 550 5.7.1 Unable to relay for"

    I tried the above code and found it working fine through telnet
    but same error occuring while using the "mail()" function in PHP.

    please help.
    thank you.
  2. Ryan Farley 1/18/2006 7:36 AM
    Gravatar
    sanyogita,

    Sorry I can't help more. I never use PHP so I won't be of much help with that function. If you can relay via the server then there maybe there is something specific with that function that might be causing the problem?

    Good luck, Ryan
  3. rajneesh narain 2/7/2006 12:19 AM
    Gravatar
    nice stuff thanks for the details..
  4. tester 4/12/2006 9:55 AM
    Gravatar
    Hi,
    All the steps are working fine for me, but still, the mails are all stuck in the Queue folder and they are not being sent out, any ideas?
  5. Tripti narain 4/16/2006 6:19 AM
    Gravatar
    Hi,

    My brother Rajneesh found this, its working. Great going!

  6. Kenneth Larsen 5/23/2006 12:46 AM
    Gravatar
    All the steps are working fine for me, but still, the mails are all stuck in the Queue folder and they are not being sent out, any ideas?

    PLEASE HELP!

    Kenneth
  7. Franklin Khu 9/10/2006 6:57 AM
    Gravatar
    Hello, I have tried your code above and it says that it cannot relay to ...., and you said "Now you can go back to the IT person to let them know that he has not opened up relaying from the machine (IP Address) you're using." unfortunately there is not IT person. Could you tell me how to open up relaying? And what are the dangers in it being opened?

    Thanks in advance!


  8. Aravind 3/20/2007 1:46 AM
    Gravatar
    Hi,
    Thanks a lot, U saved my time as I was not able to relay as permission is been restricted.
    One impt thing Can I use Windows Xp for the Same thing on a stand alone michine.

    Again Thanks a lot,
    Aravind
  9. host 6/13/2007 11:30 AM
    Gravatar
    Hi,
    All the steps are working fine for me, but still, the mails are all stuck in the Queue folder and they are not being sent out, any ideas?
  10. r4 nintendo ds 9/24/2009 4:47 AM
    Gravatar
    Very interesting topic. Wish I could help. Would be quite interesting if anyone could give answer.
  11. Udi 2/25/2010 1:00 PM
    Gravatar
    A windows open relay scanner
    http://sites.google.com/site/testersmtprelay/
  12. Dan Moore 3/3/2010 11:20 AM
    Gravatar
    I have the same issues on www.website-cambridge.co.uk/riviera/conf1/rm.php

    I have NO idea what relaying is or how to resolve it. can anyone help at all??
Comments have been closed on this topic.



 

News


Also see my CRM Developer blog

Connect:   @ryanfarley@mastodon.social

         

Sponsor

Sections