linux mail java 失败_Java,无法在linux上发送邮件

javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1

at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2182)

at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:726)

at javax.mail.Service.connect(Service.java:388)

at javax.mail.Service.connect(Service.java:246)

at javax.mail.Service.connect(Service.java:195)

at javax.mail.Transport.send0(Transport.java:254)

at javax.mail.Transport.send(Transport.java:124)

at io.paratek.updates.util.Mail.sendMail(Mail.java:34)

at io.paratek.updates.MailTest.main(MailTest.java:8)

我的代码

public class Mail {

public static void sendMail(String subject, String contents, String to) {

Properties props = System.getProperties();

props.put("mail.smtp.host", "smtp.gmail.com");

props.put("mail.smtp.socketFactory.port", "465");

props.put("mail.smtp.socketFactory.class",

"javax.net.ssl.SSLSocketFactory");

props.put("mail.smtp.auth", "true");

props.put("mail.smtp.port", "465");

Session session = Session.getInstance(props,

new javax.mail.Authenticator() {

protected PasswordAuthentication getPasswordAuthentication() {

return new PasswordAuthentication("myemail", "mypasswd");

}

});

try {

MimeMessage message = new MimeMessage(session);

message.setFrom(new InternetAddress(to));

message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

message.setSubject(subject);

message.setText(contents);

Transport.send(message);

System.out.println("Sent message successfully....");

} catch (MessagingException mex) {

mex.printStackTrace();

}

}

}

我在两个系统上都使用Oracle JDK 181。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
>