views:

96

answers:

2

I need to send emails DIRECLTY to the recipient's mail-server, bypassing any MTA's on my end. I know there are some great reasons to use MTAs like sendmail, etc, so I don't need advice in that direction. I would like to write code that directly connects to the recipient's mailservers.

So, am I missing anything here?

  1. Suppose the destination email is [email protected]
  2. Look up the MX records for domain.com and pick a random MX entry. We'll call this "server"
  3. If there is no MX record for domain.com, use "domain.com" as the "server"
  4. Connect to "server" at port 25.
  5. Transmit this:

HELO myserver.com
MAIL FROM: <[email protected]>
RCPT TO: <[email protected]>
DATA
Subject: This is a test

Hello, this is a test message.

QUIT

This has worked in a lot of cases. What I am wondering is -- what am I missing? Are there any cases when this won't work, and I'll have to do something more special?

Can I test it as we go -- if the remote server I connect to says "MESSAGE ACCEPTED FOR DELIVERY" can I assume that it's going to go through?

A: 

Some good information here:

Send Email without Mail Server
http://www.example-code.com/csharp/send_email_without_mail_server.asp

See also here:

http://www.eggheadcafe.com/community/aspnet/2/10062223/mail-server.aspx

Robert Harvey
+2  A: 

You should be checking for the expected response codes at each step, and you will of course need to escape lines in the message beginning with a dot, but yeah, that's pretty much it.

This is known as direct-to-MX mailing, and it has the advantage that you can very often tell straight away when the user has input an address that is wrong, or not accepting mail, and inform them straight away. This is good for website signup processes where you don't want the user sitting around wondering where their signup mail went.

However, the main users of direct-to-MX mail are, unfortunately, spammers. So there are some spam countermeasures that may also trip you up.

Firstly, a domain may have multiple MXes, expecting spammers with their typically-dumb software to try only the first. If delivery fails at one MX you should try the others.

It might also be worth implementing ESMTP (EHLO et al) to distinguish yourself from poorer-quality MTAs like the scripts spammers use. I don't have any evidence that mail servers out there are discriminating their spam scores based on this, but it's something I'd consider doing so probably someone else has thought of it!

A more common problem is greylisting. If you want the mail to arrive in the face of the temporary-failure responses greylisting generates, you will have to wait a little then attempt to connect and send it a second time. This loses some of the potential statelessness and quick-failure benefit of direct-to-MX mail.

Plus of course with any solution where you're sending mail without an upstream ISP mail relay, whether that's directly or through your own local postfix, you run the risk of being blocked by address. This possibility increases when you can't set up your reverse DNS to match your HELO and be something that doesn't look like a dynamically-allocated address.

bobince
If your mail server can check if the user exists for you, you can send it "manually" but to your mail server so you do not have to worry about finding the right MX for delivery.
Goran Jurić