无法通过SMTP服务器以Java发送邮件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法通过SMTP服务器以Java发送邮件相关的知识,希望对你有一定的参考价值。
[当我尝试从(sp@gmail.com)之类的个人电子邮件中以java发送邮件时,它已成功发送。
但是当我使用公司电子邮件(sp@example.com)时,会抛出Authentication failed exception
。我正在使用TLS身份验证,它已成功连接到主机。
[当我手动登录电子邮件时,它将始终要求两步验证。即使我禁用了两步验证并做了更改以降低安全性,它仍然要求进行两步验证,因为在输入用户名和密码后会显示此消息:
2-Step Verification
Based on your organization's policy, you need to turn on 2-step verification. Contact your administrator to learn more.
Enter one of your 8-digit backup codes
在这种情况下我该怎么办?因为这是我在这家公司的首要任务,所以如果您能帮助我,我将非常高兴。我该如何解决这个问题?
我的代码:
String to = "abc@example.com";
String user = "sp@example.com";
String pass = "1234";
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.example.com");
props.put("mail.smtp.port", "587");
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(user,pass);
}
});
session.setDebug(true);
try
{
/* Create an instance of MimeMessage,
it accept MIME types and headers
*/
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(user));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject(sub);
message.setText(msg);
/* Transport class is used to deliver the message to the recipients */
Transport.send(message);
}
catch(Exception e)
{
e.printStackTrace();
}
错误消息:
535 5.7.3 Authentication unsuccessful javax.mail.AuthenticationFailedException at javax.mail.Service.connect(Service.java:319) at javax.mail.Service.connect(Service.java:169) at javax.mail.Service.connect(Service.java:118) at javax.mail.Transport.send0(Transport.java:188) at javax.mail.Transport.send(Transport.java:118) java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:748)
SMTP配置:
configuration:props.put("mail.smtp.host", "smtp.oceaneering.com"); props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
答案
String pass =“ 1234”; -使用应用专用密码回复此行,该密码必须在设置页的Google邮件中生成。
以上是关于无法通过SMTP服务器以Java发送邮件的主要内容,如果未能解决你的问题,请参考以下文章
无法通过 gmail 从新创建的电子邮件中使用 phpMailer 发送 SMTP 邮件 [重复]