我正在尝试开发一种代码,允许在没有身份验证的情况下使用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.
是否可以在没有身份验证的情况下发送邮件?我错过了什么吗?
答案 0 :(得分:0)
试试这个
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);
答案 1 :(得分:0)
Gmail特别不支持未经授权的邮件发送。实际上,大多数公共SMTP服务器都不再使用,因为这会导致滥用太多。
您可能能够找到不需要身份验证的公共smtp,但它们很少见。您可以随时运行自己的smtp或简单地...添加身份验证。
特别是如果您使用的是Gmail,您只需传递凭据即可?