Java Mail API

N

Nishi Bhonsle

Hi:

I am calling the following code to validate whether the hostname entered on a page in a J2EE application is a valid SMTP server

Session _session = null;

Properties props = System.getProperties();
props.put("mail.smtp.host", hostname); //hostname passed from the page to the backend code

_session = Session.getDefaultInstance(props,null);

if ( session = = null )
{
//invalid hostname provided for a SMTP server
}
else
{
//valid hostaname for a SMTP server
}

This is not stateless and hence i am assuming that i need to do something more since if i enter "yahoo.com" for SMTP server, the code jumps into the else part of the above conditional.

Please suggest.

Thanks, Nishi.
 
G

Gary M

Nishi said:
Hi:

I am calling the following code to validate whether the hostname entered on a page in a J2EE application is a valid SMTP server

Session _session = null;

Properties props = System.getProperties();
props.put("mail.smtp.host", hostname); //hostname passed from the page to the backend code

_session = Session.getDefaultInstance(props,null);

if ( session = = null )
{
//invalid hostname provided for a SMTP server
}
else
{
//valid hostaname for a SMTP server
}

This is not stateless and hence i am assuming that i need to do something more since if i enter "yahoo.com" for SMTP server, the code jumps into the else part of the above conditional.

Please suggest.

Thanks, Nishi.

Nishi, the act of getting a session instance does not make a connection
of any kind to the smtp host. Thus, the else clause will always execute.

To accomplish what you want to do, I suggest opening a socket to the
SMTP server and port and performing 'ehlo yourhostname.whatever'
followed by a 'quit' and parsing the results from the ehlo especially.
This should at least tell you the host is valid and is a SMTP server.

I hope I have understood you correctly and that this is of some help to you.

Gary
 
N

Nishi Bhonsle

Gary said:
Nishi, the act of getting a session instance does not make a connection
of any kind to the smtp host. Thus, the else clause will always execute.

Yes, and I added the following lines to the code:
Transport tr = session.getTransport("smtp");
tr.connect;

This works fine although I do see a delay in the response(when the hostname is invalid) in the browser when this code executes on an app server deployed on a unix box behind a firewall.
To accomplish what you want to do, I suggest opening a socket to the
SMTP server and port and performing 'ehlo yourhostname.whatever'
followed by a 'quit' and parsing the results from the ehlo especially.
This should at least tell you the host is valid and is a SMTP server.

Yes, I was doing this before, ie opening a socket connection on port 25 with "myhostname", but i noticed that there was huge delay in getting the response back in the browser client when the backend code executed on a unix server.
So, I was hoping that using the Java Mail API would be better, I do notice a very slight improvement.

Can someone please tell me whether transport.connect() also opens a socket connection at port 25 under the covers?

Thanks, Nishi.
 
G

GaryM

Can someone please tell me whether transport.connect() also opens
a socket connection at port 25 under the covers?

Just put a javamail in debug mode and it will echo all the smtp
commands it sends.
 
N

Nishi Bhonsle

GaryM said:
Just put a javamail in debug mode and it will echo all the smtp
commands it sends.

yes, seems that it does the same thing....

04/10/05 16:13:32 DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
04/10/05 16:13:32 DEBUG SMTP: useEhlo true, useAuth false
04/10/05 16:13:32 DEBUG: SMTPTransport trying to connect to host "yahoo.com", port 25
 
G

Gary M

Nishi said:
yes, seems that it does the same thing....

04/10/05 16:13:32 DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
04/10/05 16:13:32 DEBUG SMTP: useEhlo true, useAuth false
04/10/05 16:13:32 DEBUG: SMTPTransport trying to connect to host "yahoo.com", port 25

Cool.
 
N

Nigel Wade

Nishi said:
Hi:

I am calling the following code to validate whether the hostname entered
on a page in a J2EE application is a valid SMTP server
Session _session = null;

Properties props = System.getProperties();
props.put("mail.smtp.host", hostname); //hostname passed from the page to the backend code

_session = Session.getDefaultInstance(props,null);

if ( session = = null )
{
//invalid hostname provided for a SMTP server
}
else
{
//valid hostaname for a SMTP server
}

This is not stateless and hence i am assuming that i need to do something
more since if i enter "yahoo.com" for SMTP server, the code jumps into the
else part of the above conditional.
Please suggest.

Thanks, Nishi.

You're method won't work. What you need to do is lookup the DNS MX record
for the email domain in question. If it doesn't have one it's not a valid
mail domain. You shouldn't need the mail server.

The method you are using is almost bound to fail most of the time. SMTP uses
MX records to identify the actual hosts which receive incoming SMTP
connections for a particular mail domain. For example, the MX record for
yahoo.com lists 4 different MX records for that domain, which in turn point
to 17 mail servers.

However, if you do a simple socket connection to yahoo.com the A record will
be looked up and this resolves to 2 completely different systems. If you
attempt to connect to port 25 at yahoo.com it's not likely to work as those
systems are are very unlikely to be running an SMTP server.
 
G

GaryM

You're method won't work. What you need to do is lookup the DNS MX
record for the email domain in question. If it doesn't have one
it's not a valid mail domain. You shouldn't need the mail server.

The above is not true. An MX record is not required for a domain to
receive mail. The mail server will try domain name itself in the
absence of the MX record.

However, if you do a simple socket connection to yahoo.com the A
record will be looked up and this resolves to 2 completely
different systems. If you attempt to connect to port 25 at
yahoo.com it's not likely to work as those systems are are very
unlikely to be running an SMTP server.

The value he is prompting for _is_ SMTP Host, i.e. he is not trying
to derive it from, nor assume it is the top level domain. So he is
right to assume that the host entered would reply as an SMTP host.
 
S

Sudsy

Nishi Bhonsle wrote:
Please suggest.

Thanks, Nishi.

I suggest that you not rely on TCP/IP sockets to validate a domain.
There are too many indeterminates. You could be dealing with firewalls
which drop packets (stealth mode) or refuse to acknowledge originating
domain (to foil spoofers or miners).
Not to mention that you also have to accomodate DNS time-outs...
Not the right approach, IMHO.
 
B

Babu Kalakrishnan

Sudsy said:
Nishi Bhonsle wrote:


I suggest that you not rely on TCP/IP sockets to validate a domain.
There are too many indeterminates. You could be dealing with firewalls
which drop packets (stealth mode) or refuse to acknowledge originating
domain (to foil spoofers or miners).
Not to mention that you also have to accomodate DNS time-outs...
Not the right approach, IMHO.

I agree that you ought to accommodate DNS timeouts, but I cannot see
another way to check if the hostname entered by the user is a valid SMTP
host (I think that was the intent of the OP - not to validate the domain
as such). To send out mail through that host, the system would have to
open a TCP connection to port 25 of the host, and testing this
beforehand by attempting to make that connection sounds like a
reasonable approach.

BK
 
W

Will Hartung

Babu Kalakrishnan said:
I agree that you ought to accommodate DNS timeouts, but I cannot see
another way to check if the hostname entered by the user is a valid SMTP
host (I think that was the intent of the OP - not to validate the domain
as such). To send out mail through that host, the system would have to
open a TCP connection to port 25 of the host, and testing this
beforehand by attempting to make that connection sounds like a
reasonable approach.

Even if the connection succeeds you need to go farther. You can have a port
open on to an SMTP server, but that doesn't mean that it will actually take
your mail. You need to go a bit deeper into the protocol to verify that when
it comes time to send mail, it will actually accept it.

Regards,

Will Hartung
([email protected])
 
B

Babu Kalakrishnan

Will said:
Even if the connection succeeds you need to go farther. You can have a port
open on to an SMTP server, but that doesn't mean that it will actually take
your mail. You need to go a bit deeper into the protocol to verify that when
it comes time to send mail, it will actually accept it.

I agree. My comment was based on the on the assumption that this was a
simple sanity check on whether the host name entered was reasonable. A
succesful connect to port 25 says "OK - this does seem to be a machine
which has an SMTP server installed". To really check out if the
specified host is an appropriate SMTP server for the application would
be much more difficult and probably impossible to verify 100% (For
instance it may accept mail for a particular domain only, might
accept/deny relaying to / from specific domains etc - I have even seen
really idiotic mail server setups that would allow/deny a relay based on
the the "From:" address in the *body* of the message (not even the
envelope From:") matching a particular set of domains)

BK
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top