gpt4 book ai didi

javax.mail.AuthenticationFailedException Java 邮件

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:43:26 25 4
gpt4 key购买 nike

我做了一个小程序,可以将消息发送到我的电子邮件帐户。我的大部分代码似乎都可以正常工作。但是,当我的代码在我的程序中命中 transport.send 时,我遇到了 AuthenticationFailedException。我不确定为什么,因为我认为我已经正确设置了所有内容。这是我的代码。

JavaEmail.java

public static void main (String[] args) throws Exception{
//intialize logger
protected static Logger logger = LogManager.getLogger(JavaEmail.class.getName());

//smtp related parameters
private static String smtpUseremail;
private static String smtpReceiverEmailAddress;
private static String smtpUserpassword;
private static String smtpPortnumber;
private static String smtpHost;

private static String emailSubject;

//stack error message variable
private static String message;

try {

logger.info("-----------------------------");
logger.info("---------Starting up---------");
logger.info("-----------------------------");

initialize();

String body = "This is a test";

logger.info("Start to connect to email server");

sendFromGmail(smtpUseremail, smtpUserpassword, smtpReceiverEmailAddress, emailSubject, body);



// System.out.println("error message: " + message);

logger.info("Email Sent");


}
catch (Exception e){
logger.error(e);

// message = e.toString();

throw e;


}
}

//Setting up Email Server
private static void sendFromGmail(String from, String pass, String to, String subject, String body) {


Properties props = PropertiesUtil.loadProperties("main.properties");

props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", smtpHost);
props.put("mail.smtp.port", smtpPortnumber);

Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(smtpUseremail, smtpUserpassword);
}
});

MimeMessage message = new MimeMessage(session);



try{


message.setFrom(new InternetAddress(from));

message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));


message.setSubject(subject);

message.setText(body);

Transport.send(message);

logger.info("Message has been delievered, check your mail");

}

catch (MessagingException e) {
logger.error("Messaging Exception");

throw new RuntimeException(e);
}
}


//Initializing Properties
private static void initialize() throws Exception {

Properties props = PropertiesUtil.loadProperties("main.properties");

//smtp set up
smtpPortnumber = props.getProperty("portNumber");
smtpReceiverEmailAddress = props.getProperty("recipientEmailAddress");
smtpUseremail = props.getProperty("userEmailAddress");
smtpUserpassword = props.getProperty("userPassword");
smtpHost = props.getProperty("emailHost");
emailSubject = props.getProperty("");

}

}

main.properties

portNumber = 465
recipientEmailAddress = my@gmail.com
userPassword = secert
userEmailAddress = my@gmail.com
emailHost = smtp.gmail.com

错误

Exception in thread "main" java.lang.RuntimeException: javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbvWE
534-5.7.14 HIW4hBxefM-JI-wxqEK6ru2lJstC10IedXvhU_Tzbft1IqPFsot0wfjAWkrX2-gLIEG0NZ
534-5.7.14 V45KWaeagzWTb_v1nAe-BOZSR6YzPHQ7-cYVGYplvcaexmFziL3IMRgkCalmn-5FjreRkm
534-5.7.14 PjtRPd9nmjcosX0ce4DXSGn_y-qGOsW-ehOp2U3eQkSReObXi-a80ywNWiy3QuiV56f61h
534-5.7.14 yz8bmLpugqIYhh7DiNd6HpwvNHAo> Please log in via your web browser and
534-5.7.14 then try again.
534-5.7.14 Learn more at
534 5.7.14 https://support.google.com/mail/answer/78754 ob15sm12759947pdb.52 - gsmtp

at emailNotification.JavaEmail.sendFromGmail(JavaEmail.java:140)
at emailNotification.JavaEmail.main(JavaEmail.java:63)
Caused by: javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbvWE
534-5.7.14 HIW4hBxefM-JI-wxqEK6ru2lJstC10IedXvhU_Tzbft1IqPFsot0wfjAWkrX2-gLIEG0NZ
534-5.7.14 V45KWaeagzWTb_v1nAe-BOZSR6YzPHQ7-cYVGYplvcaexmFziL3IMRgkCalmn-5FjreRkm
534-5.7.14 PjtRPd9nmjcosX0ce4DXSGn_y-qGOsW-ehOp2U3eQkSReObXi-a80ywNWiy3QuiV56f61h
534-5.7.14 yz8bmLpugqIYhh7DiNd6HpwvNHAo> Please log in via your web browser and
534-5.7.14 then try again.
534-5.7.14 Learn more at
534 5.7.14 https://support.google.com/mail/answer/78754 ob15sm12759947pdb.52 - gsmtp

at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:809)
at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:752)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:669)
at javax.mail.Service.connect(Service.java:317)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
at javax.mail.Transport.send0(Transport.java:194)
at javax.mail.Transport.send(Transport.java:124)
at emailNotification.JavaEmail.sendFromGmail(JavaEmail.java:130)
... 1 more

最佳答案

转到浏览器,然后登录到您在代码中尝试的 gmail 帐户,然后转到此链接 https://www.google.com/settings/security/lesssecureapps

你会看到

enter image description here

您应该点击 TURN ON

关于javax.mail.AuthenticationFailedException Java 邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32303910/

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