无法通过SMTP服务器以Java发送邮件

问题描述 投票:2回答:1

[当我尝试从([email protected])之类的个人电子邮件中以java发送邮件时,它已成功发送。

但是当我使用公司电子邮件([email protected])时,会抛出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 = "[email protected]";
String user = "[email protected]";
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(ThreadPo‌​olExecutor.java:624) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.r‌​un(TaskThread.java:6‌​1) 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");
java javamail
1个回答
0
投票

String pass =“ 1234”; -使用应用专用密码回复此行,该密码必须在设置页的Google邮件中生成。

© www.soinside.com 2019 - 2024. All rights reserved.