-1

I have a JSP code. I try to send an smtp email by that. It has an error in:

Transport.send(message); .

Can anybody help me?

here is the code:

 <%@ page import="java.io.*,java.util.*,javax.mail.*, javax.mail.Service"%>
 <%@ page import="javax.mail.internet.*,javax.activation.*"%>
 <%@ page import="javax.servlet.http.*,javax.servlet.*" %>
 <%
 String result;
 // Recipient's email ID needs to be mentioned.
 String to = "email@gmx.com";

 // Sender's email ID needs to be mentioned
 String from = "email@gmx.com";

 // Assuming you are sending email from localhost
 String host = "mail.gmx.com";

 // Get system properties object
 Properties properties = System.getProperties();

 // Setup mail server
 properties.setProperty("mail.smtp.host", host);

 // Get the default Session object.
 Session mailSession = Session.getDefaultInstance(properties);


  // Create a default MimeMessage object.
  MimeMessage message = new MimeMessage(mailSession);
  // Set From: header field of the header.
  message.setFrom(new InternetAddress(from));
  // Set To: header field of the header.
  message.addRecipient(Message.RecipientType.TO,
                           new InternetAddress(to));
  // Set Subject: header field
  message.setSubject("This is the Subject Line!");
  // Now set the actual message
  message.setText("This is actual message");
  // Send message
  Transport.send(message);
  result = "Sent message successfully....";

%>
<html>
<head>
<title>Send Email using JSP</title>
</head>
<body>
<center>
<h1>Send Email using JSP</h1>
</center>
<p align="center">
<% 
out.println("Result: " + result + "\n");
%>
</p>
</body>
</html>

PS. I use mail.jar and activation.jar in the class path. I will be appreciate your helps.

Here is the error messages that I have got:

 org.apache.jasper.JasperException: An exception occurred processing JSP page /mail.jsp      
 at line 37

 34:   // Now set the actual message
 35:   message.setText("This is actual message");
 36:   // Send message
 37:   Transport.send(message);
 38:   result = "Sent message successfully....";
 39: 
 40: %>


 Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.ja 
 va:510)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:401)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
com.ish.system.runtime.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilt     er.java:67)
 root cause

 javax.servlet.ServletException: javax.mail.MessagingException: Could not connect to                       
 SMTP      host: mail.gmx.com, port: 25;
 nested exception is:
java.net.SocketException: Permission denied: connect
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.jav     a:862)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:     791)
org.apache.jsp.mail_jsp._jspService(mail_jsp.java:124)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
com.ish.system.runtime.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilt     er.java:67)
 root cause

 javax.mail.MessagingException: Could not connect to SMTP host: mail.gmx.com, port: 25;
   nested exception is:
java.net.SocketException: Permission denied: connect
com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1282)
com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)
javax.mail.Service.connect(Service.java:275)
javax.mail.Service.connect(Service.java:156)
javax.mail.Service.connect(Service.java:105)
javax.mail.Transport.send0(Transport.java:168)
javax.mail.Transport.send(Transport.java:98)
org.apache.jsp.mail_jsp._jspService(mail_jsp.java:98)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
com.ish.system.runtime.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilt     er.java:67)
 root cause

 java.net.SocketException: Permission denied: connect
java.net.PlainSocketImpl.socketConnect(Native Method)
java.net.PlainSocketImpl.doConnect(Unknown Source)
java.net.PlainSocketImpl.connectToAddress(Unknown Source)
java.net.PlainSocketImpl.connect(Unknown Source)
java.net.SocksSocketImpl.connect(Unknown Source)
java.net.Socket.connect(Unknown Source)
java.net.Socket.connect(Unknown Source)
com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:232)
com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)
com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1250)
com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)
javax.mail.Service.connect(Service.java:275)
javax.mail.Service.connect(Service.java:156)
javax.mail.Service.connect(Service.java:105)
javax.mail.Transport.send0(Transport.java:168)
javax.mail.Transport.send(Transport.java:98)
org.apache.jsp.mail_jsp._jspService(mail_jsp.java:98)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
com.ish.system.runtime.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilt     er.java:67)
user1188627
  • 1
  • 1
  • 2

1 Answers1

1
javax.mail.MessagingException: Could not connect to SMTP host: mail.gmx.com, port: 25;  
nested exception is: 
java.net.SocketException: Permission denied: connect

This is a rather low level problem. The operating system platform-specified user who is executing the current Java runtime environment is not allowed to open a network connection to the given host.

This can have many possible causes, depending on the rights of the user, the degree of higher-level control on it, the Firewall settings, the operating system used, etcetera. At least, the problem is not in your Java code (disclaimer: I haven't read it, the whole exception is already the answer at its own, so you may still have some unrelated mistakes in your Java code).

I'll mention some common causes to that you understand where you should look for the solution:

  • The webapplication is deployed to a 3rd party host which doesn't allow your webapplication to connect any hosts other than the 3rd party host itself. Solution: upgrade account so that you can use host's own mail server (or maybe you already have it without the need to upgrade), or migrate to another host.

  • The operating system platform-specific non-admin user account is not allowed to make any network connections. The admin account must change the user's account settings accordingly.

  • The operating system platform has a Firewall installed with too restrictive configuration. Try disabling the Firewall altogether and retest to exclude it from being suspect and then reconfigure the Firewall if necessary.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555