我试图用java发送一封电子邮件。这是我的密码。我所有的凭据都是正确的,但我还是得到了这个错误。我见过许多解决方案,其中说打开不那么安全的应用程序,但是现在这个功能被谷歌禁用了。那我该怎么解决呢。
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendEmail {
public static void main(String[] args) {
// Recipient's email ID needs to be mentioned.
String to = "sender@gmail.com";
// Sender's email ID needs to be mentioned
String from = "receiver@gmail.com";
// Assuming you are sending email from through gmails smtp
String host = "smtp.gmail.com";
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.port", "465");
properties.put("mail.smtp.ssl.enable", "true");
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
// Get the Session object.// and pass username and password
Session session = Session.getInstance(properties, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("receiver@gmail.com", "password");
}
});
// Used to debug SMTP issues
session.setDebug(true);
try {
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
// Set Subject: header field
message.setSubject("This is the Subject Line!");
// Now set the actual message
message.setText("This is actual message");
System.out.println("sending...");
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (MessagingException mex) {
mex.printStackTrace();
}
}
}这就是我要犯的错误。我在vs代码中的错误
发布于 2022-08-09 11:03:20
由于less secure app特性已被删除,我们必须按照以下步骤通过third party software即App password使用Gmail
步骤1:设置2步验证:- Google -> 2-步骤验证->输入密码,询问->打开(您可以使用SMS获取Gmail代码以激活2步骤验证)
步骤2:生成应用程序机密:-谷歌帐户->安全->应用密码->输入密码问->选择应用程序和设备.->例如其他(Java应用程序) ->输入应用程序名称,例如MyApp ->生成
步骤3:使用生成的应用程序机密:-复制16字符密码,在应用程序中使用16字符密码(而不是实际密码)和Gmail用户名。
这应该可以节省你的时间
https://stackoverflow.com/questions/73281984
复制相似问题