2011-12-08 57 views
0

嗯,我想檢查用戶提供的數據是否是正確的Gmail帳戶。我通過向他們發送確認電子郵件來完成此操作。如果未發送電子郵件,則會引發AuthenticationFailedException。但是當我把正確的它繼續拋出異常。此外,如果我從頭開始輸入正確的數據並在之後進行更改,即使新數據爲假,它仍會繼續發送確認電子郵件。 我使用上面的代碼。我認爲它保持了首次創建的會話,但我如何每次創建一個新會話?我如何檢查gmail身份驗證?

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

} 

}

+1

我真的不會建議捕獲用戶的個人電子郵件用戶名和密碼。有這麼多的安全問題。我建議您查看Google提供的其他身份驗證機制:http://code.google.com/apis/accounts/docs/GettingStarted.html – extols

回答

0

insted的的session = Session.getDefaultInstance(props, this);

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

,你將被罰款!

相關問題