Java, JSP and SendMail

M

michela rossi

Hi,

Anyone know of a simple yet robust way of sending email using SendMail
in JSP code?

Any free servlets anywhere so that you don't have to drop down to the
Runtime.exec(..) level?

Thanks, Michela.
 
G

GaryM


Just remember that Javamail does not send mail; rather it
communicates with a SMTP relay, which in turn sends the mail. So I
sense it might not do what he's looking for, though he was not that
specific.

The command "sendmail -oi <toaddress> < pipe" will send a message
from a byte stream. I am sure someone (or google) can formulate a
Runtime.exec snippet to do what you need . I am not that familiar
with it myself, else I would.

Gary
 
G

Gorazd Bozic

Just remember that Javamail does not send mail; rather it
communicates with a SMTP relay, which in turn sends the mail. So I
sense it might not do what he's looking for, though he was not that
specific.

You can however send the mail directly to the recipient's SMTP server:

1. Check for DNS MX records for the recipients address, it should
point to the SMTP server for that domain (there can be more than
one MX address)
2. If there is none, check for DNS A record and if found, assume
that there's SMTP running on it
3. Send mail to the SMTP server

Gorazd
 
G

Gorazd Bozic

How can i get dns mx records on my win xp box
not a server?

By querying your provider's DNS server. Your provider will tell you
what is its address.

The code below uses injektilo.net.dns package (see Google) for DNS
queries:

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Vector;
import injektilo.net.dns.*;

public class MXChecker {

public static String dnsServer = "1.2.3.4"; // Address of your DNS server

private static InetAddress[] getMXAddress(String domain) {
Vector records;
InetAddress[] result;

// Query your DNS server for MX records

try {
DnsConnection dns = new DnsConnection(dnsServer);
records = dns.lookup(domain, DnsMessage.MX);
} catch (DnsException e) {
return null;
}

if (records.size() == 0) {

// Get all MX records as InetAddresses

result = new InetAddress[records.size()];
for (int i = 0; i < records.size(); i++) {
MailExchange mx = (MailExchange) records.elementAt(i);
try {
result = InetAddress.getByName(mx.getExchange());
} catch (UnknownHostException e) {
}
}

} else {

// No MX records for this domain; we'll assume that the supplied
// domain is actually a host running a SMTP server.

try {
InetAddress ip = InetAddress.getByName(domain);
result = new InetAddress[1];
result[0] = ip;
return result;
} catch (UnknownHostException e) {
return null;
}

return result;

}
}

public static void main(String[] args) {
for (int i = 0; i < args.length; i++) {
InetAddress[] servers = getMXAddress(args);
if (ip == null)
System.out.println("Unable to find MX/host records for "+
args+" failed");
else {
System.out.println("Mail servers for domain "+args+" are:");
for (int j = 0; j < servers.length; j++) {
System.out.println(" "+servers[j]);
}
}
}
}

}
 
R

Roedy Green

public static String dnsServer = "1.2.3.4"; // Address of your DNS server

how do you get the default DNS server? It is a major hassle getting
customers to configure their DNS servers, which may change.
 
G

GaryM

how do you get the default DNS server? It is a major hassle getting
customers to configure their DNS servers, which may change.

I am used to doing it this way:

env.put("java.naming.factory.initial",
"com.sun.jndi.dns.DnsContextFactory");
env.put("java.naming.provider.url", "dns://" + dns + "/");

<etc>

which, as you mention, requires the DNS to be known.

However, the following code was posted here recently and requires no
DNS hardcoded. I awalys thought the naming provider was required.
Apologies to the author for not finding the attribution:


import java.util.Hashtable;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;

public class MXLookup {

public static void main( String args[] ) {
if( args.length == 0 ) {
System.err.println( "Usage: MXLookup host [...]" );
System.exit( 12 );
}
for( int i = 0; i < args.length; i++ ) {
try {
System.out.println( args + " has " +
doLookup( args ) + " mail servers" );
}
catch( Exception e ) {
e.printStackTrace();
}
}
}

static int doLookup( String hostName ) throws NamingException {
Hashtable env = new Hashtable();
env.put("java.naming.factory.initial",
"com.sun.jndi.dns.DnsContextFactory");
DirContext ictx = new InitialDirContext( env );
Attributes attrs = ictx.getAttributes( hostName,
new String[] { "MX" });
Attribute attr = attrs.get( "MX" );
if( attr == null )
return( 0 );
return( attr.size() );
}
}
 
S

Sudsy

GaryM said:
I am used to doing it this way:

env.put("java.naming.factory.initial",
"com.sun.jndi.dns.DnsContextFactory");
env.put("java.naming.provider.url", "dns://" + dns + "/");

<etc>

which, as you mention, requires the DNS to be known.

However, the following code was posted here recently and requires no
DNS hardcoded. I awalys thought the naming provider was required.
Apologies to the author for not finding the attribution:

'Twas me. I was going to post it again to address Roedy's question
but I see you've saved me the trouble. Thanks kindly.
 

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
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top