javax.mail.MessagingException: ไม่สามารถเชื่อมต่อกับโฮสต์ SMTP: smtp.gmail.com พอร์ต: 587

ฉันมีแอปพลิเคชัน Java ที่ส่งอีเมล แต่เมื่อฉันรันโค้ด ฉันได้รับข้อยกเว้นต่อไปนี้ ฉันได้ตรวจสอบการตั้งค่าการตรวจสอบสิทธิ์ด้วย และด้วยพอร์ต 485 ที่ไม่มี แต่ยังคงมีข้อผิดพลาดอยู่

DEBUG: setDebug: JavaMail version 1.4.4
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 refused: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1934)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:638)
at javax.mail.Service.connect(Service.java:317)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
at javax.mail.Transport.send0(Transport.java:194)
at javax.mail.Transport.send(Transport.java:124)
at SimpleMailWithAttachment.<init>(SimpleMailWithAttachment.java:46)
at SimpleMailWithAttachment.main(SimpleMailWithAttachment.java:64)
    Caused by: java.net.ConnectException: Connection refused: connect

นี่คือรหัสที่ฉันใช้เพื่อพยายามส่งอีเมล

public SimpleMailWithAttachment() {
    Properties props = System.getProperties();
    props.setProperty("mail.smtp.port", "587");
    props.setProperty("mail.smtp.host", "smtp.gmail.com");
    props.setProperty("mail.smtp.starttls.enable", "true");
    props.setProperty("mail.smtp.auth", "true");

    Authenticator auth = new MyAuthenticator();
    Session smtpSession = Session.getInstance(props, auth);
    smtpSession.setDebug(true);

    MimeMessage message = new MimeMessage(smtpSession);
    try {
        message.setFrom(new InternetAddress(email));

        message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
        final String encoding = "UTF-8";

        message.setSubject(subject, encoding);
        message.setText(text, encoding);
        Transport.send(message);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

static class MyAuthenticator extends Authenticator {

    public PasswordAuthentication getPasswordAuthentication() {
        String username = "******@gmail.com";
        String password = "*****";
        return new PasswordAuthentication(username, password);

    }
}

person user407269    schedule 19.08.2013    source แหล่งที่มา


คำตอบ (2)


ฉันไม่คิดว่าคุณสามารถใช้ GMail ได้หากไม่มีการตรวจสอบสิทธิ์ คุณต้องใช้พอร์ต 465 ไม่ใช่ 587

ดู http://email.about.com/od/accessinggmail/f/Gmail_SMTP_Settings.htm

person Matti Lyra    schedule 19.08.2013
comment
ฉันได้ลองใช้โค้ดกับพอร์ต 465 แล้ว แต่ก็ยังล้มเหลวด้วยข้อยกเว้นเดียวกัน ข้อเสนอแนะใด ๆ เกี่ยวกับวิธีที่ฉันสามารถแก้ไขปัญหานี้? - person user407269; 19.08.2013

การเพิ่มคุณสมบัติเหล่านี้ช่วยฉันได้:

debug=true
spring.mail.host=smtp.gmail.com
spring.mail.port=587
[email protected]
spring.mail.password=somepwd

spring.mail.properties.mail.debug=true
spring.mail.properties.mail.transport.protocol=smtp
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.ssl.trust=*
spring.mail.properties.mail.smtp.starttls.enable=true

person Ganesh Jadhav    schedule 31.12.2020