如何检查Gmail上的身份验证?

时间:2011-12-08 13:18:31

标签: java email session gmail

我想检查用户提供的数据是否是正确的gmail帐户。我通过向他们发送确认电子邮件来执行此操作。如果未发送电子邮件,则抛出AuthenticationFailedException。但是,当我把正确的,它继续抛出异常。此外,如果我从头开始输入正确的数据并在之后进行更改,即使新数据为假,它也会继续发送确认电子邮件。 我使用上面的代码。我假设它保留了首次创建的Session。但我如何每次创建一个新会话?

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");
                                                                }

}

}

2 个答案:

答案 0 :(得分:2)

您缺少在属性对象中将用户标识和密码设置为身份验证所需的身份验证部分。

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

查看此博客以获取完整示例http://www.techlabs4u.com/2010/08/java-code-to-send-email-using-java-mail.html

答案 1 :(得分:0)

而不是session = Session.getDefaultInstance(props, this);

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

你会没事的!