在java中发送邮件时出错

时间:2016-05-11 10:21:10

标签: java javamail

final int port = 587;
String host = "mail.website.com";  
final String user = "abc@website.com";
final String password = "password";  

String to = "abc@yourmail.com"; 

Properties props = new Properties();  
props.put("mail.smtp.host", host);  
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(user, password);  
        }  
    });  

try {  
    MimeMessage message = new MimeMessage(session);  
    message.setFrom(new InternetAddress(user));  
    message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));  
    message.setSubject("demo");  
    message.setText("Hello");  

    Transport.send(message);  

    System.out.println("done");  

} catch (MessagingException e) {
    e.printStackTrace();
} 

错误:

  

com.sun.mail.util.MailConnectException:无法连接到主机端口:

1 个答案:

答案 0 :(得分:0)

您的港口似乎已关闭,您确定是正确的吗?我敢打赌25465 ...

POP3 - port 110
IMAP - port 143
SMTP - port 25
HTTP - port 80
Secure SMTP (SSMTP) - port 465
Secure IMAP (IMAP4-SSL) - port 585
IMAP4 over SSL (IMAPS) - port 993
Secure POP3 (SSL-POP) - port 995