问题
public void emailTest() {
Properties properties=new Properties();
properties.put("mail.smtp.host", "email-smtp.us-east-1.amazonaws.com");
properties.put("mail.smtp.port", 587);
properties.put("mail.debug", "true");
try{
Session session=Session.getInstance(properties);
Message msg=new MimeMessage(session);
msg.setFrom(new InternetAddress("test@gmail.com", "Test"));
msg.setRecipient(RecipientType.TO, new InternetAddress("test@gmail.com", "Test"));
msg.setSubject("Test Subject");
msg.setText("Test Mail");
msg.saveChanges();
Transport transport=session.getTransport("smtp");
transport.connect("username","password");
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
}catch(Exception e){
e.printStackTrace();
}
}
POM : com.sun.mail javax.mail 1.5.2 provided
DEBUG: JavaMail version 1.4ea
DEBUG: java.io.FileNotFoundException: /usr/java/jdk1.8.0_144/jre/lib/javamail.providers (No such file or directory)
DEBUG: !anyLoaded
DEBUG: not loading resource: /META-INF/javamail.providers
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: !anyLoaded
DEBUG: not loading resource: /META-INF/javamail.address.map
DEBUG: java.io.FileNotFoundException: /usr/java/jdk1.8.0_144/jre/lib/javamail.address.map (No such file or directory)
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "email-smtp.us-east-1.amazonaws.com", port 25, isSSL false
回答1:
You're using an ancient version of JavaMail; please update if possible.
If you're running your JavaMail program within AWS, note that AWS has restrictions on how you use JavaMail and what SMTP host you can connect to. Consult their documentation for details. (Sorry, I don't have the link.)
If you're running outside of AWS, see the JavaMail FAQ for tips on debugging connection problems. Most likely you're behind a firewall that's preventing you from connecting directly.
来源:https://stackoverflow.com/questions/60194916/javax-mail-messagingexception-could-not-connect-to-smtp-host-email-smtp-us-eas