javax.mail.AuthenticationFailedException: 530 Error: A secure connection is requiered(such as ssl)
Properties prop = new Properties();
prop.setProperty("mail.transport.protocol", "smtp"); // 设置邮件发送协议
prop.setProperty("mail.host", "smtp.qq.com"); // 邮件服务器地址
//prop.setProperty("mail.smtps.ssl.enable", "true"); // 邮件ssl验证
prop.setProperty("mail.smtp.auth", "true"); // 邮件服务身份验证
//prop.setProperty("mail.smtp.localhost", "127.0.0.1");
Session session = Session.getDefaultInstance(prop);
// 收件人电子邮箱
String to = "xxxxx@qq.com";
// 发件人电子邮箱
String from = "xxxx@qq.com";
try{
// 创建默认的 MimeMessage 对象
MimeMessage message = new MimeMessage(session);
// Set From: 头部头字段
message.setFrom(new InternetAddress(from));
// Set To: 头部头字段
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
// Set Subject: 头部头字段
message.setSubject("This is the Subject Line!");
// 设置消息体
message.setText("This is actual message");
// 根据 Session 获取邮件传输对象
Transport transport = session.getTransport();
transport.connect("xxx@qq.com","xxxxx");
// 发送消息
transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
喵喔喔
相关分类