File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes JSP using javamail Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "JSP using javamail" Watch "JSP using javamail" New topic
Author

JSP using javamail

Anthony Smith
Ranch Hand

Joined: Sep 10, 2001
Posts: 285
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

Joined: Jun 30, 2001
Posts: 129
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

Joined: Jun 13, 2000
Posts: 3340
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


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Anthony Smith
Ranch Hand

Joined: Sep 10, 2001
Posts: 285
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

Joined: Dec 16, 2000
Posts: 625
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

Joined: Sep 10, 2001
Posts: 285
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

Joined: Dec 16, 2000
Posts: 625
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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSP using javamail