aspose file tools*
The moose likes Servlets and the fly likes Tomcat and email Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Tomcat and email" Watch "Tomcat and email" New topic
Author

Tomcat and email

Tod Checker
Greenhorn

Joined: Feb 19, 2004
Posts: 27
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");

//Pass your email address on your smtp server to
// the following method.
smtp.from("baldwin.richard@iname.com");

//Pass the email address of the recipient of the
// message to the next method.
smtp.to("baldwin@austin.cc.tx.us");
//Get an output stream for the message
PrintStream msg = smtp.startMessage();

//Write the message header in the output stream.
msg.println("To: baldwin@austin.cc.tx.us");
msg.println("Subject: Test Msg from Servlet");
msg.println();

//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();

out.println("<HTML>");
out.println("<HEAD><TITLE=Servlet06</TITLE></HEAD>");
out.println("<BODY>");

out.println("Email message sent<BR>");
out.println("</BODY></HTML>");
}catch( Exception e ) {
e.printStackTrace();
}//end catch
}//end doGet()
}//end class Servlet06
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12823
    
    5
Well, I don't know anything about the package you are using, but are you sure that the msg PrintStream gets flushed and closed here:

With respect to servers, I am using the Apache "James" Java based mail server. It was easy to get going. Have you been able to send mail through the PostCast server at all?
Bill
Tod Checker
Greenhorn

Joined: Feb 19, 2004
Posts: 27
Yes I have been able to send mail with PostCast and this code, but it only seems to work if I put it in a main class, which is why I don't understand the reason for in not working with a servlet.
Tod Checker
Greenhorn

Joined: Feb 19, 2004
Posts: 27
Can anyone tell me why this runs in a main but not in a servlet?
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
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?


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Tod Checker
Greenhorn

Joined: Feb 19, 2004
Posts: 27
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
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
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?
Mike Troxclaire
Greenhorn

Joined: Sep 01, 2004
Posts: 11
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.
 
wood burning stoves
 
subject: Tomcat and email