有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么在Javamail中连接超时?

我试图在eclipse中使用javamail通过gmail发送电子邮件。每次我得到javax。邮政消息异常

例外情况

 DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
    DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
    DEBUG SMTP: useEhlo true, useAuth true
    DEBUG SMTP: useEhlo true, useAuth true
    DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587, isSSL false
    javax.mail.MessagingException: Could not connect to SMTP host: 

smtp.gmail.com, port: 587;
  nested exception is:
    java.net.ConnectException: Connection timed out: connect

代码

package org.itc.utility;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.itc.model.User;

public class SendEmail {
    public User sendingEmail(User user)
    {
        Properties props = new Properties();  
        props.put("mail.smtp.host", "smtp.gmail.com");  
        props.put("mail.smtp.auth", "true");  
        props.put("mail.debug", "true");  
        props.put("mail.smtp.port", 587);  
        props.put("mail.smtp.socketFactory.port", 587);  
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.transport.protocol", "smtp");
        Session mailSession = null;

        mailSession = Session.getInstance(props,  
                new javax.mail.Authenticator() {  
            protected PasswordAuthentication getPasswordAuthentication() {  
                return new PasswordAuthentication("<yogeshvbhat@gmail.com>", "<zc516z918>");  
            }  
        });  


        try {


            Transport transport = mailSession.getTransport();

            MimeMessage message = new MimeMessage(mailSession);

            message.setSubject("Sample Subject");
            message.setFrom(new InternetAddress("yogeshvbhat@gmail.com"));
            String []to = new String[]{"yogeshyvb@gmail.com"};
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to[0]));
            String body = "Sample text";
            message.setContent(body,"text/html");
            transport.connect();

            transport.sendMessage(message,message.getRecipients(Message.RecipientType.TO));
            transport.close();
        } catch (Exception exception) {
                System.out.println(exception);
        }
        return user;
    }
}

每次它跳过try块时,我都会得到异常。有什么帮助吗


共 (2) 个答案

  1. # 1 楼答案

    一般来说,连接超时有几个可能的原因,包括服务器脱机、网络链路故障和网络路由问题1

    但在这种情况下,最有可能的解释是防火墙阻止了你对gmail邮件服务器的访问。这通常是作为一种监视/控制组织发出的电子邮件的手段,或者作为一种防止恶意垃圾邮件或受攻击的机器发出垃圾邮件的方式

    Firewall is disabled

    这可能不是你的计算机防火墙或你(家庭)网络的防火墙做的封锁。它可能会被组织的网络管理员或ISP管理员阻止


    1-一般来说,这些事情可能会中断从您的机器到服务器的所有网络流量,因此值得使用“ping”、“traceroute”、“tcptraceroute”等进行一些探索

  2. # 2 楼答案

    仅添加以下属性:

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");
    

    另外,检查你的gmail设置:

    Set up 2-Step Verification