javax.mail.MessagingException:无法连接到 SMTP 主机:smtp.gmail.com,端口:25; [英] javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 25;

查看:36
本文介绍了javax.mail.MessagingException:无法连接到 SMTP 主机:smtp.gmail.com,端口:25;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用来发送简单邮件的代码

The Code I'm Using to send a simple mail

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import java.io.*;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;

public class SendMailUsingAuthentication
{

  private static final String SMTP_HOST_NAME = "smtp.gmail.com";
  private static final String SMTP_AUTH_USER = "myemail@gmail.com";
  private static final String SMTP_AUTH_PWD  = "mypassword";

  public static void main(String args[]) throws Exception
  {

  }

  public void postMail( String recipients[ ], String subject,String message , String from) throws MessagingException
  {
    try {
        boolean debug = false;

        Properties props = new Properties();
        props.put("mail.smtp.host", SMTP_HOST_NAME);
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.auth", "true");
        Authenticator auth = new SMTPAuthenticator();
        Session session = Session.getDefaultInstance(props, auth);
        session.setDebug(debug);

        Message msg = new MimeMessage(session);

        InternetAddress addressFrom = new InternetAddress(from);
        msg.setFrom(addressFrom);
        InternetAddress[] addressTo = new InternetAddress[recipients.length];
        for (int i = 0; i < recipients.length; i++) {
            addressTo[i] = new InternetAddress(recipients[i]);
        }
        msg.setRecipients(Message.RecipientType.TO, addressTo);

        msg.setSubject(subject);

        msg.setContent(message, "text/plain");

        Transport.send(msg);

    } 
    catch (Throwable e) 
    {
        e.printStackTrace();
    }
 }
/**
* SimpleAuthenticator is used to do simple authentication
* when the SMTP server requires it.
*/
private class SMTPAuthenticator extends javax.mail.Authenticator
{
    public PasswordAuthentication getPasswordAuthentication()
    {
        String username = SMTP_AUTH_USER;
        String password = SMTP_AUTH_PWD;
        return new PasswordAuthentication(username, password);
    }
}
}

当我在我的本地机器上运行这段代码时,它工作正常......但是当我将它部署到服务器上时,它给了我这个异常

When I'm running this code on my Local machine, it's working fine.... But When I deployed it on the server it's giving me this exception

javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 25;
  nested exception is:
    java.net.ConnectException: Connection timed out
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1282)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)
    at javax.mail.Service.connect(Service.java:297)
    at javax.mail.Service.connect(Service.java:156)
    at javax.mail.Service.connect(Service.java:105)
    at javax.mail.Transport.send0(Transport.java:168)
    at javax.mail.Transport.send(Transport.java:98)
    at SendMailUsingAuthentication.postMail(SendMailUsingAuthentication.java:97)
    at RegistrationServlet.doGet(RegistrationServlet.java:98)
    at RegistrationServlet.doPost(RegistrationServlet.java:125)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
    at org.apache.coyote.ajp.AjpProcessor.process(AjpProcessor.java:196)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:300)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
Caused by: java.net.ConnectException: Connection timed out
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
    at java.net.Socket.connect(Socket.java:529)
    at java.net.Socket.connect(Socket.java:478)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:232)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1250)

推荐答案

听起来像是防火墙问题(防火墙会默默丢弃不允许的数据包,这就是您会看到连接超时的原因.

Sounds like a firewall problem (firewalls silently drop packets that are not allowed, which is why you would be seeing a Connection Timeout.

你可以试试吗

H:> telnet smtp.gmail.com 25
Connecting To smtp.gmail.com...Could not open connection to the host, on port 25
: Connect failed

在您的服务器上,看看您是否获得连接?(我在这里的防火墙后面不允许连接到 gmail)

on your server and see whether you get a connection? (I'm behind a firewall here that does not allow connections to gmail)

这篇关于javax.mail.MessagingException:无法连接到 SMTP 主机:smtp.gmail.com,端口:25;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆