javax.mail.AuthenticationFailedException : failed to connect, 没有指定密码?

标签 java smtp gmail jakarta-mail

此程序尝试发送电子邮件但抛出运行时异常:

javax.mail.AuthenticationFailedException: failed to connect, no password specified?

当我提供了正确的用户名和密码进行身份验证时,为什么会出现此异常?

发件人和收件人都有 G-mail 帐户。发件人和收件人都有 G-mail 帐户。发件人已禁用两步验证过程。

这是代码:

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

class tester {
    public static void main(String args[]) {
        Properties props = new Properties();
        props.put("mail.smtp.host" , "smtp.gmail.com");
        props.put("mail.stmp.user" , "username");

        //To use TLS
        props.put("mail.smtp.auth", "true"); 
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.password", "password");
        //To use SSL
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class", 
            "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");


        Session session  = Session.getDefaultInstance( props , null);
        String to = "me@gmail.com";
        String from = "from@gmail.com";
        String subject = "Testing...";
        Message msg = new MimeMessage(session);
        try {
            msg.setFrom(new InternetAddress(from));
            msg.setRecipient(Message.RecipientType.TO, 
                new InternetAddress(to));
            msg.setSubject(subject);
            msg.setText("Working fine..!");
            Transport transport = session.getTransport("smtp");
            transport.connect("smtp.gmail.com" , 465 , "username", "password");
            transport.send(msg);
            System.out.println("fine!!");
        }
        catch(Exception exc) {
            System.out.println(exc);
        }
    }
}

即使在输入密码后,我仍然得到异常。为什么不进行身份验证?

最佳答案

尝试创建一个 javax.mail.Authenticator 对象,并将其与属性对象一起发送到 session 对象。

Authenticator 编辑:

您可以修改它以接受用户名和密码,您可以将它们存储在那里或任何您想要的地方。

public class SmtpAuthenticator extends Authenticator {
public SmtpAuthenticator() {

    super();
}

@Override
public PasswordAuthentication getPasswordAuthentication() {
 String username = "user";
 String password = "password";
    if ((username != null) && (username.length() > 0) && (password != null) 
      && (password.length   () > 0)) {

        return new PasswordAuthentication(username, password);
    }

    return null;
}

在您发送电子邮件的类(class)中:

SmtpAuthenticator authentication = new SmtpAuthenticator();
javax.mail.Message msg = new MimeMessage(Session
                    .getDefaultInstance(emailProperties, authenticator));

关于javax.mail.AuthenticationFailedException : failed to connect, 没有指定密码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6610572/

相关文章:

java - 通过SSL通过Cajo连接时出现javax.net.ssl.SSLHandshakeException

java - Ctrl+V 在 IntelliJ 中不起作用

laravel-4 - 通过Laravel使用gmail smtp:无法与主机smtp.gmail.com建立连接[连接超时#110]

java - 550 访问被拒绝 - 无效的 HELO 名称

python - 使用 smtplib 从 Gmail 接收回复 - Python

gmail - 无效的 JSON-LD?

java - Tomcat 中的线程池和请求处理

java - 如何在 java 中使用 split() 拆分字符为 "["的字符串

node.js - 与 Node 的 SMTP 连接

ruby-on-rails - 为 Rails 中的邮件将外部 CSS 转换为内联 CSS