无法使用javamail API通过Gmail发送邮件

时间:2019-05-10 13:56:10

标签: java api gmail javamail

我正在尝试通过Java Web App发送电子邮件。我正在使用javamail api 1.6.2,并将我的Google帐户设置为接受来自每个应用程序选项的连接。

这是错误:

com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 587; timeout -1;  
nested exception is:  
    java.net.ConnectException: Connection refused: connect
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2209)  
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:740)  
    at javax.mail.Service.connect(Service.java:366)  
    at javax.mail.Service.connect(Service.java:246)  
    at test.mail.TestMail.sendFromGMail(TestMail.java:51)
    at test.mail.TestMail.main(TestMail.java:19)  
Caused by: java.net.ConnectException: Connection refused: connect  
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)  
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)  
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)  
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.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:359)  
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:238)  
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2175)  
    ... 5 more  

代码:

package test.mail;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;

public class TestMail {
private static String USER_NAME = "my.id";  // GMail user name (just the 
part 
before "@gmail.com")
private static String PASSWORD = "my.password"; // GMail password
private static String RECIPIENT = "distination@gmail.com";




    public static void main(String[] args) {
    String from = USER_NAME;
    String pass = PASSWORD;
    String[] to = { RECIPIENT }; // list of recipient email addresses
    String subject = "Java send mail example";
    String body = "Welcome to JavaMail!";

    sendFromGMail(from, pass, to, subject, body);
}

private static void sendFromGMail(String from, String pass, String[] to, String subject, String body) {
    Properties props = System.getProperties();
    String host = "smtp.gmail.com";
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.user", from);
    props.put("mail.smtp.password", pass);
    props.put("mail.smtp.port", "587");
    props.put("mail.smtp.auth", "true");

    Session session = Session.getDefaultInstance(props);
    MimeMessage message = new MimeMessage(session);

    try {
        message.setFrom(new InternetAddress(from));
        InternetAddress[] toAddress = new InternetAddress[to.length];

        // To get the array of addresses
        for( int i = 0; i < to.length; i++ ) {
            toAddress[i] = new InternetAddress(to[i]);
        }

        for( int i = 0; i < toAddress.length; i++) {
            message.addRecipient(Message.RecipientType.TO, toAddress[i]);
        }

        message.setSubject(subject);
        message.setText(body);
        Transport transport = session.getTransport("smtp");
        transport.connect(host, from, pass);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }
    catch (AddressException ae) {
        ae.printStackTrace();
    }
    catch (MessagingException me) {
        me.printStackTrace();
    }
}
 }

1 个答案:

答案 0 :(得分:0)

此问题可能与防火墙或防病毒软件有关,有时会阻止连接。检查JavaMail常见问题解答,可能会有所帮助:https://javaee.github.io/javamail/FAQ