gpt4 book ai didi

java - 发送电子邮件无需身份验证

转载 作者:行者123 更新时间:2023-12-01 06:16:58 25 4
gpt4 key购买 nike

我正在尝试开发一个代码,允许使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com