URL-mailto approach

M

Manish Hatwalne

This is the code I am trying. It should send mail with the required message.
But it throws an exception instead on my machine.

try{
URL mail = new URL("mailto:"+MAIL_TO);
URLConnection conn = mail.openConnection();
conn.setDoInput(false);
conn.setDoOutput(true);
conn.connect();
PrintWriter mout = new PrintWriter(new
OutputStreamWriter(conn.getOutputStream()));
mout.println("From: "+MAIL_FROM);
mout.println("To: "+MAIL_TO);
mout.println("Subject: "+MAIL_SUBJECT);
mout.println();
mout.println(message);
mout.close();
}
catch (Exception e){
e.printStackTrace();
}

The exception thrown is -

java.net.UnknownHostException: mailhost
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:143)
at java.net.Socket.connect(Socket.java:425)
at java.net.Socket.connect(Socket.java:375)
at sun.net.NetworkClient.doConnect(NetworkClient.java:139)
at sun.net.NetworkClient.openServer(NetworkClient.java:118)
at sun.net.smtp.SmtpClient.openServer(SmtpClient.java:111)
at sun.net.smtp.SmtpClient.<init>(SmtpClient.java:156)
at sun.net.smtp.SmtpClient.<init>(SmtpClient.java:162)
at
sun.net.www.protocol.mailto.MailToURLConnection.connect(MailToURLConnection.
java:70)

What could be the problem?? The same code works on another machine with same
JDK configuration. So I think it might be a configuration problem.

TIA,
- Manish
 
A

Alexey Dmitriev

Manish said:
This is the code I am trying. It should send mail with the required message.
But it throws an exception instead on my machine.

try{
URL mail = new URL("mailto:"+MAIL_TO);
URLConnection conn = mail.openConnection();
conn.setDoInput(false);
conn.setDoOutput(true);
conn.connect();
PrintWriter mout = new PrintWriter(new
OutputStreamWriter(conn.getOutputStream()));
mout.println("From: "+MAIL_FROM);
mout.println("To: "+MAIL_TO);
mout.println("Subject: "+MAIL_SUBJECT);
mout.println();
mout.println(message);
mout.close();
}
catch (Exception e){
e.printStackTrace();
}

The exception thrown is -

java.net.UnknownHostException: mailhost
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:143)
at java.net.Socket.connect(Socket.java:425)
at java.net.Socket.connect(Socket.java:375)
at sun.net.NetworkClient.doConnect(NetworkClient.java:139)
at sun.net.NetworkClient.openServer(NetworkClient.java:118)
at sun.net.smtp.SmtpClient.openServer(SmtpClient.java:111)
at sun.net.smtp.SmtpClient.<init>(SmtpClient.java:156)
at sun.net.smtp.SmtpClient.<init>(SmtpClient.java:162)
at
sun.net.www.protocol.mailto.MailToURLConnection.connect(MailToURLConnection.
java:70)

What could be the problem?? The same code works on another machine with same
JDK configuration. So I think it might be a configuration problem.

TIA,
- Manish
What's problem? The exception (java.net.UnknownHostException) is
informative. It's probable that host are not accessed from your machine.
 
R

Roedy Green

This is the code I am trying. It should send mail with the required message.
But it throws an exception instead on my machine.

Try something like this in a browser:

URL url = new URL( "mailto:[email protected]" );
getAppletContext().showDocument( url );

You want the browser to intercept the url and act on it as if the
user typed it, not send it to the server.
 
M

Manish Hatwalne

What's problem? The exception (java.net.UnknownHostException) is
informative. It's probable that host are not accessed from your machine.

--

Maybe I am missing something..
Which host is this -"mailhost"? I am not specifying it anywhere.
My MAIL_TO field is normal (e-mail address removed) type...
So guess this is sth expected from the implementation. Don't have access to
source now, so can't check.

- Manish
 
M

Manish Hatwalne

I am not using it from an Applet.
Besides this will only pop up a new mail from default email client. I want
it o be sent, and same code does this on another machine!

- Manish
 
M

Manish Hatwalne

The problem is -
The system property "mail.host" needs to be properly configured, which I
didn't have.
The solution is to simply add following code before sending this mail -
System.setProperty("mail.host","smtp.myserver.tld");

- Manish
 
G

GaryM

The problem is -
The system property "mail.host" needs to be properly configured,
which I didn't have.
The solution is to simply add following code before sending this
mail -
System.setProperty("mail.host","smtp.myserver.tld");

Cool. I learned something new today. I did not know that worked so
cleanly without Javamail too.

Thanks,

Gary
 

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,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top