在用 java 编写电子邮件应用程序时,我得到了这个异常:
javax.mail.MessagingException: Could not connect to SMTP host: mail.simsystech.com, port: 8443;
nested exception is:
java.net.ConnectException: Connection timed out: 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)
&这是我的代码(按照本教程链接):
Properties properties = System.getProperties();
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.transport.protocol", "smtp");
properties.setProperty("mail.smtp.host", host);
// properties.put("mail.smtp.port", "8443");
properties.setProperty("mail.smtp.auth", "true");
properties.setProperty("mail.smtp.starttls.enable", "true");
final String user = "abc@xyz.com";
final String password = "*****";
Authenticator authenticator = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user,password);
}
};
Session session = Session.getDefaultInstance(properties,authenticator);
try{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(sid));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(rid));
message.setSubject(subject);
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(text);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
// String filename = "file.txt";
DataSource source = new FileDataSource(file);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(file);
multipart.addBodyPart(messageBodyPart);
// Send the complete message parts
message.setContent(multipart );
// Now set the actual message
// message.setText(text);
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException ex) {
ex.printStackTrace();
}
虽然我已经浏览了这些 stackoverflow 的链接:1、2
知道为什么我会收到此错误... :( :(