我正在嘗試使用GoDaddy電子郵件主機發送郵件,前兩天我使用Java郵件API註冊了郵件,但事實證明,它不是那麼容易實現,我得到了,這個錯誤:使用Javamail發送電子郵件
Could not connect to SMTP host: smtpout.asia.secureserver.net, port: 80, response: -1
我試圖端口3535,465,587,25,但仍然得到同樣的錯誤。相同的代碼下面已經過測試,發出使用Gmail電子郵件,以增加該代碼(這是我在這種情況下省略)工作:
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
MailSender.java:
public class MailSender {
private static String HOST = "smtpout.asia.secureserver.net";
private static String PORT = "80";
public static void sendMail(final Mail mail) throws MailException {
EmailValidator validtor = new EmailValidator();
if (validtor.validate(mail.getReceipient())) {
Properties props = new Properties();
props.put("mail.smtp.host", HOST);
props.put("mail.smtp.socketFactory.port", PORT);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", PORT);
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(mail.getUsername(),mail.getPassword());
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(mail.getSender()));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(mail.getReceipient()));
message.setSubject(mail.getSubject());
message.setText(mail.getBody());
Transport.send(message);
System.out.println("OK");
} catch (MessagingException e) {
throw new MailException(e.getMessage());
}
} else {
throw new MailException("Email address not valid.");
}
}
}
該類中的Mail
參數包含所有其他郵件信息,用戶名/密碼,發件人和收件人電子郵件地址字符串,該郵件經過測試可與Outlook & Thunderbird等電子郵件客戶端一起使用。
你用什麼端口Outlook/Thunderbird? – SLaks