• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Tomcat and email

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone tell me why this runs in a main but not in a servlet?
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Tod Checker
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic