File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Win a copy of OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Tomcat and email" Watch "Tomcat and email" New topic

Tomcat and email

Tod Checker

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.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
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("");

//Pass your email address on your smtp server to
// the following method.

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

//Write the message header in the output stream.
msg.println("Subject: Test Msg from Servlet");

//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

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


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

Joined: Mar 22, 2000
Posts: 12868
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?
Tod Checker

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

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

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 ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Tod Checker

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:, 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

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

Joined: Feb 19, 2004
Posts: 27
I did as you mentioned and this is the error I got
at emailer.email_message.defineMessage(
line 75 in 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

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.
I agree. Here's the link:
subject: Tomcat and email