2011-12-08 3 views
0

글쎄, 사용자가 입력 한 데이터가 정확한 Gmail 계정인지 확인하고 싶습니다. 확인 전자 메일을 보내면됩니다. 전자 메일이 보내지지 않으면 AuthenticationFailedException이 발생합니다. 하지만 내가 올바른 것을 넣으면 Exception을 던져 버린다. 또한, 나는 처음부터 올바른 데이터를 넣고 나중에 변경하면 새 데이터가 틀린 경우에도 확인 이메일을 계속 전송합니다. 위의 코드를 사용합니다. 나는 그것이 처음 만들어진 세션에 붙어 있다고 가정합니다.하지만 매번 새로운 세션을 어떻게 만듭니 까?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

답변

2

당신은 속성이 객체에 인증 관련 사용자 ID 및 암호를 설정하는 데 필요한 인증 부분을 누락되었습니다. 완전한 예를 들어

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

체크 아웃이 블로그는 session = Session.getDefaultInstance(props, this);

사용 session = Session.getInstance(props, this);

http://www.techlabs4u.com/2010/08/java-code-to-send-email-using-java-mail.html

0

Insted 당신은 잘 될 것입니다!

관련 문제