什么阻止了我的Java邮件?

时间:2015-05-26 08:30:27

标签: java servlets windows-7 smtp javamail

我有一个使用“纯”JSP,Servlet和Hibernate开发的Web应用程序。它也与Java Mail一起使用。 Java邮件功能几乎适用于我们在此测试的每台机器,直到今天。它在部署服务器上不起作用(它实际上是一台Windows 7 PC,我认为bcs它有一个GUI)。它给了我以下错误

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

以下是我的代码。

/*
 * 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="abc@xxx.com";//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");
           }


    }
}

阻止我的申请发送电子邮件是什么样的事情?服务器正在使用JRE 8和XAMPP。服务器管理员说防火墙已关闭!

1 个答案:

答案 0 :(得分:1)

问题是端口25在某种程度上被阻止在服务器中。 SMTP的默认端口是端口25.我使用Properties更改了Java邮件的端口,但它确实有效。