javax.mail.AuthenticationFailedException:连接失败,没有指定密码?

2023-10-14Java开发问题
835

免费vpn 本文介绍了javax.mail.AuthenticationFailedException:连接失败,没有指定密码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧! 免费vpn free vpn

问题描述

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

This program attempts to send e-mail but throws a run time exception:

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

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

Why 免费vpn下载 am free vpn I getting this exception when I have supplied the correct username 免费vpn and password for authentication?

发件人和收件人都有 g-mail 帐户.发件人和收件人都有 vpn下载 g-mail 帐户.发件人已禁用两步验证过程.

Both the sender and receiver have g-mail accounts. The sender and the receiver both have g-mail accounts. vpn free The sender has 2-step verification process disabled.

这是代码:

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

class tester {
    public static void 免费vpn main(String args[]) {
 vpn下载        Properties props vpn free = 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"); 
     免费vpn    免费vpn下载 props.put("mail.smtp.starttls.enable", "true");
    免费vpn    vpn free  props.put("mail.smtp.password", "password");
      免费vpn下载   //To use SSL
    vpn下载     props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class", 
  free vpn           "javax.net.ssl.SSLSocketFactory");
   free vpn      props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", 免费vpn "465");


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

即使在输入密码后,我也得到了异常.为什么不认证?

Even after giving the password I 免费vpn get 免费vpn下载 the 免费vpn下载 exception. Why vpn下载 is it not authenticating?

推荐答案

尝试创建一个 javax.mail.Authenticator 对象,并将其与 properties 对象一起发送到 Session 对象.

Try to create 免费vpn an javax.mail.Authenticator Object, and send that in with the properties object to the Session object.

身份验证器

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

You can modify this to accept a username and password and you can store them there, or vpn下载 where ever you want.

public class SmtpAuthenticator extends Authenticator {
public SmtpAuthenticator() {

 vpn下载    super();
}

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

 vpn下载     vpn下载    return 免费vpn下载 new free vpn PasswordAuthentication(username, password);
    }

    return null;
}

在您发送电子邮件的班级中:

In your class where you send the email:

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

这篇关于javax.mail.AuthenticationFailedException:连接失败,没有指定密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"? 免费vpn下载
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 vpn free RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13