Tomcat and email

Does Tomcat need to be configured to allow emailing? I got this source code online. I packaged it and put my smtpserver name in. But the server never receives the message only the attempt. I am using a free edition of PostCast, is that the problem? Is there a better server out there? import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import sun.net.smtp.SmtpClient; public class Servlet06 extends HttpServlet{ public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{

try { //Pass a string containing the name of your smtp // server as a parameter to the following // constructor SmtpClient smtp = new SmtpClient("YourSmtpServer.com");

//Write the text of the message in the output // stream msg.println("This is a test message."); msg.println(new Date());//put date and time in msg //Close the stream and send the messaged smtp.closeServer();

//Notify HTML client that the message has been sent res.setContentType("text/html");PrintWriter out = res.getWriter();

I can only guess that it must be a configuration issue. I have just tested your code with (my own server and email addresses) and it works fine. Is your server running on the same machine as your "main" application? Could it be that the server machine can't resolve the mail server name? Have you tried specifying the mail server by IP address rather than name?

Yes the mail server is running locally and I have tried the following addresses for my server and it still won't email from a servlet: 127.0.0.1, localhost, and my computer's name. I even downloaded other mail servers only to get the same results, the code works from a main class but not a servlet. I've tried it with a firewall enabled and disabled, but still it doesn't work from a servlet only main.

Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920

posted Feb 21, 2004 11:15:00

0

That is puzzling. What seems most strange is that you seem to be getting a silent failure. Is there any way you can increase the amount of logging in either the servlet container or the mail server? Do you get any errors logged if you give it a nonexistent mail server address?

Tod Checker
Greenhorn

Joined: Feb 19, 2004
Posts: 27

posted Feb 21, 2004 11:44:00

0

I did as you mentioned and this is the error I got sun.net.smtp.SmtpProtocolException: at sun.net.smtp.SmtpClient.issueCommand(SmtpClient.java:48) at sun.net.smtp.SmtpClient.closeServer(SmtpClient.java:38) at sun.net.NetworkClient.openServer(NetworkClient.java:117) at sun.net.smtp.SmtpClient.openServer(SmtpClient.java:111) at sun.net.smtp.SmtpClient.<init>(SmtpClient.java:156) at emailer.email_message.defineMessage(email_message.java:75) at emailer.email_message.run(email_message.java:48) line 75 in email_message.java is where I instaniate a new SmtpClient using that fake server name. I tried looking at the log file generated by the server and it only logs out going errors. Do you have any other suggestions?

I am having a problem along the same grounds with the display of time in the email message. Everything works fine but the "Sent:" time is in a different zone and I have tried to set the time to the system time and also with new Date() but no success. Does anybody have an idea as to how do I force the sent time in the email message? Thanks in advance.