Java 我如何在gmail上检查身份验证?

Java 我如何在gmail上检查身份验证?,java,email,session,gmail,Java,Email,Session,Gmail,我想检查用户输入的数据是否是正确的gmail帐户。我通过向他们发送确认电子邮件来实现这一点。如果电子邮件未发送,则会引发AuthenticationFailedException。但当我输入正确的值时,它会继续抛出异常。此外,如果我从一开始就输入正确的数据,然后再进行更改,即使新数据是错误的,它也会继续发送确认电子邮件。 我使用上面的代码。我假设它会保留第一次创建的会话,但是如何每次都创建一个新会话呢 public class GmailSender extends javax.mail.Aut

我想检查用户输入的数据是否是正确的gmail帐户。我通过向他们发送确认电子邮件来实现这一点。如果电子邮件未发送,则会引发AuthenticationFailedException。但当我输入正确的值时,它会继续抛出异常。此外,如果我从一开始就输入正确的数据,然后再进行更改,即使新数据是错误的,它也会继续发送确认电子邮件。 我使用上面的代码。我假设它会保留第一次创建的会话,但是如何每次都创建一个新会话呢

public class GmailSender extends javax.mail.Authenticator {
private String mailhost = "smtp.gmail.com";
private String user;
private String password;
private Session session;

public GmailSender(String user, String password) {
    this.user = user;
    this.password = password;

    Properties props = new Properties();
    props.setProperty("mail.transport.protocol", "smtp");
    props.setProperty("mail.host", mailhost);
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.socketFactory.fallback", "false");
    props.setProperty("mail.smtp.quitwait", "false");

    session = Session.getDefaultInstance(props, this);

}

protected PasswordAuthentication getPasswordAuthentication() {

    return new PasswordAuthentication(user, password);
}   


public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {

    MimeMessage message = new MimeMessage(session);
    DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
    message.setSender(new InternetAddress(sender));
    message.setSubject(subject);
    message.setDataHandler(handler);
    if (recipients.indexOf(',') > 0)
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
    else
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
    Transport.send(message);
}

public class ByteArrayDataSource implements DataSource {
    private byte[] data;
    private String type;

    public ByteArrayDataSource(byte[] data, String type)    {
        super();
        this.data = data;
        this.type = type;
                                                            }

    public ByteArrayDataSource(byte[] data) {
        super();
        this.data = data;
                                            }

    public void setType(String type)    {
        this.type = type;
                                        }

    public String getContentType() {
        if (type == null)
            return "application/octet-stream";
        else
            return type;
                                    }

    public InputStream getInputStream() throws IOException  {
        return new ByteArrayInputStream(data);
                                                            }

    public String getName() {
        return "ByteArrayDataSource";
                            }

    public OutputStream getOutputStream() throws IOException    {
        throw new IOException("Not Supported");
                                                                }

}

}您缺少身份验证部分,需要在properties对象中将userid和密码设置为authentication

props.put("mail.smtp.user", userid);
props.put("mail.smtp.password", password); 

查看此博客以获取完整的示例,而不是
session=session.getDefaultInstance(props,this)

使用
session=session.getInstance(props,this)


你会没事的

我真的不建议捕获用户的个人电子邮件用户名和密码。这有很多安全问题。我建议看看谷歌提供的其他身份验证机制: