File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSP using javamail

 
Anthony Smith
Ranch Hand
Posts: 285
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can get pretty much the exact code to run as a java file but not as a jsp. Why is that here is my error:
****************************
javax.servlet.ServletException: Sending failed;
nested exception is:
javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
nested exception is:
java.net.ConnectException: Connection refused: connect
**********************************************

<%@ page import="java.util.Properties, javax.mail.*, javax.mail.internet.*" %>
<%
String host = "riddler.gsptech.fedex.com";
String from = "anthony.smith@fedex.com";
String to = "anthony.smith@fedex.com";
String subject = "subject";
String messageText = "body";
boolean sessionDebug = true;
// Create some properties and get the default Session.
Properties props = System.getProperties();
props.put("mail.host", host);
props.put("mail.transport.protocol", "smtp");
Session mailSession = Session.getDefaultInstance(props, null);
// Set debug on the Session so we can see what is going on
// Passing false will not echo debug info, and passing true
// will.
mailSession.setDebug(sessionDebug);
// Instantiate a new MimeMessage and fill it with the
// required information.
Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
//msg.setSentDate(new Date());
msg.setText(messageText);
// Hand the message to the default transport service
// for delivery.
Transport.send(msg);
out.println("Mail was sent to " + to);
out.println(" from " + from);
out.println(" using host " + host + ".");
%>

<jsp:forward page="jetspeed/screen/Home"/>
 
sanj singh
Ranch Hand
Posts: 129
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Anthony
Try this.I think you are not setting your SMTP server.
Properties props=System.getProperties();
props.put("mail.smtp.host",nameOfYourSMTPServer);
regards
sanj
 
Carl Trusiak
Sheriff
Posts: 3340
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apparently some other application has already set the property mail.host in your system to localhost. Probably your webserver so, check the server.xml and all web.xml's to see which one is guilty.
When you make the call
Session.getDefaultInstance(props, null);
it gets the values already set if they are set and will set the values to what you pass in if they are not set.
You can override the defaults by calling
Session.getInstance(props, null);
Which will start the session with the properties you pass it wheather they have been set to something else in your system or not.


------------------
I Hope This Helps
Carl Trusiak, SCJP2
 
Anthony Smith
Ranch Hand
Posts: 285
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried them all and have had no luck....
Here is my updated code..........
<%@ page import="java.util.Properties, javax.mail.*, javax.mail.internet.*" %>

<%
String host = "riddler.gsptech.fedex.com";
String from = "anthony.smith@fedex.com";
String to = "anthony.smith@fedex.com";
String subject = "subject";
String messageText = "body";
boolean sessionDebug = true;
// Create some properties and get the default Session.
Properties props = System.getProperties();
props.put("mail.host", host);
props.put("mail.smtp.host","riddler.gsptech.fedex.com");
props.put("mail.transport.protocol", "smtp");
Session mailSession = Session.getInstance(props, null);

// Set debug on the Session so we can see what is going on
// Passing false will not echo debug info, and passing true
// will.
mailSession.setDebug(sessionDebug);
// Instantiate a new MimeMessage and fill it with the
// required information.
Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
//msg.setSentDate(new Date());
msg.setText(messageText);
// Hand the message to the default transport service
// for delivery.
Transport.send(msg);
out.println("Mail was sent to " + to);
out.println(" from " + from);
out.println(" using host " + host + ".");
%>

 
Sean Casey
Ranch Hand
Posts: 625
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is just a suggestion, but why don't you uses a servlet with your jsp page. The servlet can handle the sending mail, and it will be much easier to debug, than trying to figure out what is wrong with you jsp file. But that's just a suggestion.
 
Anthony Smith
Ranch Hand
Posts: 285
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Servlet sounds fine although I dont know a whole lot about them. How would I make the servlet go to whatever link that I would like after sending the email?
 
Sean Casey
Ranch Hand
Posts: 625
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use a RequestDispatcher object and then use the forward method to go to whichever page you need to after sending the email. For example you could forward to a page indicating the email had indeed been successfully sent.

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic