我正在尝试开发一个代码,允许使用 javamail 发送邮件而无需身份验证。
Properties properties = System.getProperties();
properties.put("mail.smtp.auth", "false");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.port", 587);
Session session = Session.getInstance(properties);
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("xxxxxx@hotmail.com"));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress("yyyyyyy@gmail.com"));
message.setSubject("This is the Subject Line!");
message.setText("This is actual message");
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (MessagingException mex) {
mex.printStackTrace();
}
但是当我执行它时,我得到了这个异常
com.sun.mail.smtp.SMTPSendFailedException: 530-5.5.1 Authentication Required.
是否可以不认证就发送邮件?我错过了什么吗?
最佳答案
试试这个
Properties props = new Properties();
props.put("mail.smtp.host", mailHost);
props.put("mail.smtp.port", port);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
session.setDebug(true);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(fromAddress));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddress));
message.setSubject(subject);
message.setContent(body, "text/html; charset=utf-8");
message.setText(body);
Transport.send(message);
关于java - 发送电子邮件无需身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22378844/