我正在尝试使用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