我正在尝试使用从这里获取的SSL示例发送电子邮件,并将其更改为以下凭据,但遇到了如下异常。请帮我解决这个问题。
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;
public class SimpleSSLMail {
private static final String SMTP_HOST_NAME = "xx.xx.xx.xx";
private static final int SMTP_HOST_PORT = 25 ;//465;
private static final String SMTP_AUTH_USER = "xxx@yyy.com";
private static final String SMTP_AUTH_PWD = "xxxx";
public static void main(String[] args) throws Exception{
new SimpleSSLMail().test();
}
public void test() throws Exception{
Properties props = new Properties();
props.put("mail.transport.protocol", "smtps");
props.put("mail.smtps.host", SMTP_HOST_NAME);
props.put("mail.smtps.auth", "true");
// props.put("mail.smtps.quitwait", "false");
Session mailSession = Session.getDefaultInstance(props);
mailSession.setDebug(true);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
message.setSubject("Testing SMTP-SSL");
message.setContent("This is a test", "text/plain");
message.addRecipient(Message.RecipientType.TO,
new InternetAddress("myid@gmail.com"));
transport.connect
(SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER, SMTP_AUTH_PWD);
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
}
}
异常:
Exception in thread "main" javax.mail.MessagingException: Could not connect to SMTP host:xx.xx.xx.xx, port: 25;
nested exception is:
java.net.ConnectException: Connection timed out: connect
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:275)