sending email from JSP page "Can not find host" or "auth fail" failed
I have been trying to write a piece of JSP code to general an email then send it to user. Unfortunately, I failed. I am stuck in here now.
Actuallyl, I have searched from Google and got some code about this functionality.
The first one is very similar to the code in the page [url]http://forum.java.sun.com/thread.jspa?threadID=760048&tstart=240[/url]
Because I use my private mailbox to test, I think there should be an authenticating process. I found such code in page [url]http://forum.java.sun.com/thread.jspa?threadID=709126[/url]
I did a little change. The most significant is I changed the "PopupAuthentication" to "PasswordAuthentication". But it does not work. The page is always in endless loading state. I don't know what happened. Because I am not very familiar with Java MAIL. If anybody can help, I will be very appreciate. Thanks
String host ="smtp.ihug.co.nz";
String from ="brabra@ihug.co.nz";
String to="bra@slingshot.co.nz";
String username="bra";
String password="bra";
// Get system properties
Properties props = System.getProperties();
// Setup mail server
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth","true");
props.put("mail.smtp.username", username);
props.put("mail.smtp.password", password);
// Setup authentication, get session
Authenticator auth =new PasswordAuthentication(username, password);;
// Get session
Session msession = Session.getDefaultInstance(props, auth);
// Define message
MimeMessage message =new MimeMessage(msession);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject("Your account is created");
message.setText("test");
Transport transport = msession.getTransport("smtp");
transport.connect(host,username,password);
transport.sendMessage(message,message.getAllRecipients());
transport.close();
[2656 byte] By [
Wanli@NZa] at [2007-10-3 2:53:32]

Your first step should be to write a small stand-alone program (not a JSP or servlet) that helps you learn how to send e-mail from Java. Along with that you should read the JavaMail FAQ:
http://java.sun.com/products/javamail/FAQ.html
It is extremely likely to mention your problem, whatever it is.
try replacing the following line
Authenticator auth = new PasswordAuthentication(username, password);;
with
Authenticator auth = new Authenticator(){
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username","password");
}
};
Thanks Clap's information. It is very useful. I am studying those questions.
And I have replaced the wrong code following Raee's suggestion. But there are some exceptions:
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: Access to default session denied
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:358)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:305)
root cause
java.lang.SecurityException: Access to default session denied
javax.mail.Session.getDefaultInstance(Session.java:175)
org.apache.jsp.SendMail_jsp._jspService(SendMail_jsp.java:73)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:305)
===========
Any suggestion. Does the code find the host. or the host simply refuses the host? what's happened?
> Thanks Clap's information. It is very useful. I am> studying those questions. > And I have replaced the wrong code following Raee's> suggestion. But there are some exceptions:> But have decided to ignore them.
But have decided to ignore them?Sorry, I am not sure your answer? Do you have any suggestion regarding this topic? I know little about Java Mail. Thanks
I tried to run the JSP page again. Same code as last time. This time the exception is:
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Could not connect to SMTP host: smtp.ihug.co.nz, port: 25;
nested exception is:
java.net.SocketException: Software caused connection abort: connect
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:867)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:800)
org.apache.jsp.SendMail_jsp._jspService(SendMail_jsp.java:99)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:305)
root cause
javax.mail.MessagingException: Could not connect to SMTP host: smtp.ihug.co.nz, port: 25;
nested exception is:
java.net.SocketException: Software caused connection abort: connect
com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:867)
com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:156)
javax.mail.Service.connect(Service.java:234)
javax.mail.Service.connect(Service.java:135)
org.apache.jsp.SendMail_jsp._jspService(SendMail_jsp.java:83)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:305)
note The full stack trace of the root cause is available in the Tomcat logs.
very strange. This time it shows that connection to host failed. Godish! Who could help. Thanks.
> But have decided to ignore them?
>
> Sorry, I am not sure your answer? Do you have any
> suggestion regarding this topic? I know little about
> Java Mail. Thanks
What I mean't was that you have decided to ignore the advice given by DrClap and to ignore the content of the reference he gave.
I work with Java Mail and find it very easy BUT I don't try to embed it directly in a JSP page. I create a class that can be used in a JSP page (but normaly used in a servlet) but can be tested outside of the JSP.
Sabre150, thanks for your notice. I think I'd better read those reference completely before I ask quesiton. :-). If you don't mind, could you please post your code here. I could study your code. Heap of thanks.
> If you don't mind, could you please post your code> here. I could study your code. Heap of thanks.My code is pretty much straight from the examples given in the Javamail distribution but using the authentication approach given in reply 2 .
Oh. Thanks. I should study more carefully. I am wondering about port issue in this thread: http://forum.java.sun.com/thread.jspa?threadID=666312&tstart=0 There is a guy saying port25 is blocked by mail server provider. If blocked, how we use email agent to send email?