Search code examples
javaservletswindows-7smtpjakarta-mail

What is blocking my Java mail?


I have a web application developed with "pure" JSP, Servlet and Hibernate. It is also working with Java Mail. The Java mail function is working almost every machine we tested here, until today. It didn't work on the deployment server (it is actually a windows 7 PC I think bcs it has a GUI). It gave me the below error

com.sun.mail.util.MailConnectException: Couldn't connect to host, port: mail.xxx.com, 25; timeout -1;
  nested exception is:
        java.net.ConnectException: Connection timed out: connect
        at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2053)
        at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:69
7)
        at javax.mail.Service.connect(Service.java:386)
        at javax.mail.Service.connect(Service.java:245)
        at javax.mail.Service.connect(Service.java:194)
        at javax.mail.Transport.send0(Transport.java:253)
        at javax.mail.Transport.send(Transport.java:124)
        at RemindeWorker.Listener.MailClient.sendEmail(MailClient.java:65)
        at Filter.AuthenticationFilter_Level1.doFilter(AuthenticationFilter_Leve
l1.java:99)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:241)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:208)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
alve.java:220)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
alve.java:122)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(Authentica
torBase.java:503)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
ava:170)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j
ava:103)
        at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:
950)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
ve.java:116)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.jav
a:421)
        at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp
11Processor.java:1070)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(
AbstractProtocol.java:611)
        at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoin
t.java:316)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.
java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor
.java:617)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskTh
read.java:61)
        at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.ConnectException: Connection timed out: connect
        at java.net.DualStackPlainSocketImpl.connect0(Native Method)
        at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketI
mpl.java:79)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.ja
va:345)
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocket
Impl.java:206)
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java
:188)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
        at java.net.Socket.connect(Socket.java:589)
        at java.net.Socket.connect(Socket.java:538)
        at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:312)
        at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:236)
        at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2019)
        ... 25 more
Level1 executed
NUM is: 1

Below is my Code.

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package RemindeWorker.Listener;

import java.util.List;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author Yohan
 */
public class MailClient 
{

    public void sendEmail(final String userName, final String password, final String host, final String html, final List<String>emails, String subject) throws MessagingException
    {
        System.out.println("User Name: "+userName);
        System.out.println("Password: "+password);
        System.out.println("Host: "+host);

//          String host="mail.xxx.com";  
//          final String user="[email protected]";//change accordingly  
//          final String password="knight55";//change accordingly  



           //Get the session object  
           Properties props = new Properties();  
           props.put("mail.smtp.host",host);  
           props.put("mail.smtp.auth", "true");  

           Session session = Session.getDefaultInstance(props,  
            new javax.mail.Authenticator() {  
              protected PasswordAuthentication getPasswordAuthentication() {  
            return new PasswordAuthentication(userName,password);  
              }  
            });  

           if(!emails.isEmpty())
           {
                   //Compose the message  
                    InternetAddress[] address = new InternetAddress[emails.size()];
                    for (int i = 0; i < emails.size(); i++) {
                        address[i] = new InternetAddress(emails.get(i));
                    }

                     MimeMessage message = new MimeMessage(session);  
                     message.setFrom(new InternetAddress(userName));  
                      message.setRecipients(Message.RecipientType.TO, address);
                     message.setSubject(subject);  

                     message.setContent(html,"text/html");

                    //send the message  
                     Transport.send(message);  

                     System.out.println("message sent successfully...");  
           }
           else
           {
               System.out.println("No Recieptions");
           }


    }
}

What kind of thing is stopping my application from sending emails? The sever is having JRE 8 and XAMPP. Server admin says the firewall is turned off!


Solution

  • The issue was that port 25 was blocked in the server by somehow. The default port for SMTP is port 25. I changed the port for the Java mail using Properties and it worked.