2010-04-21 4 views
3

전자 메일을 보내거나 POP/IMAP에 연결하지 않고 사용자 SMTP 서버 자격 증명을 확인할 수있는 방법이 있습니까? 작성하려고 시도한 일부 코드는 실패합니다. 거기에없는 것을 찾을 수 있습니까?실제로 전자 메일을 보내지 않고 JavaMail SMTP 자격 증명 확인

이메일/비밀번호는 걱정하지 마십시오. 나는 그것이 거기다는 것을 알고있다.

참고 : 코드를 시도하는 경우. 케이스 1은 올바른 신임 정보를 제공 할 때 통과해야합니다. 실패하면 다른 사람이 암호를 변경했습니다. 다른 이메일 주소를 사용해야합니다.



import java.util.Properties; 

import javax.mail.Authenticator; 
import javax.mail.MessagingException; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import javax.mail.Transport; 

public class EmailTest { 

public static void main(String[] args) { 
    EmailHelper eh = new EmailHelper(); 

    /* GMail Setting for SMTP using STARTTLS */ 
    String name = "AAA"; 
    String email = "[email protected]"; 
    String smtpHost = "smtp.gmail.com"; 
    String serverPort = "587"; 
    String requireAuth = "true"; 
    String dontuseAuth = "false"; 
    String userName = email; // same as username for GMAIL 
    String password = "zaq12wsx"; 
    String incorrectPassword = "someRandomPassword"; 
    String enableSTARTTLS = "true"; 
    String dontenableSTARTTLS = "false"; 

    try { 
    /* only valid case */ 
    eh.sendMail(name, email, smtpHost, serverPort, requireAuth, 
    userName, password, enableSTARTTLS); 
    System.out.println("Case 1 Passed"); 

    /* should fail since starttls is required for GMAIL. */ 
    eh.sendMail(name, email, smtpHost, serverPort, requireAuth, 
    userName, password, dontenableSTARTTLS); 
    System.out.println("Case 2 Passed"); 

    /* should fail since GMAIL requires authentication */ 
    eh.sendMail(name, email, smtpHost, serverPort, dontuseAuth, "", "", 
    dontenableSTARTTLS); 
    System.out.println("Case 3 Passed"); 

    /* should fail. password is incorrect and starttls is not enabled */ 
    eh.sendMail(name, email, smtpHost, serverPort, requireAuth, 
    userName, incorrectPassword, dontenableSTARTTLS); 
    System.out.println("Case 4 Passed"); 
    } catch (MessagingException e) { 
    e.printStackTrace(); 
    } 
} 

} 

class EmailHelper { 

private Properties properties = null; 
private Authenticator authenticator = null; 
private Session session = null; 

public void sendMail(String name, String email, String smtpHost, 
    String serverPort, String requireAuth, String userName, 
    String password, String enableSTARTTLS) throws MessagingException { 
    properties = System.getProperties(); 
    properties.put("mail.smtp.host", smtpHost); 
    properties.put("mail.smtp.port", serverPort); 
    properties.put("mail.smtp.starttls.enable", enableSTARTTLS); 
    properties.put("mail.smtp.auth", requireAuth); 
    properties.put("mail.smtp.timeout", 20000); 

    authenticator = new SMTPAuthenticator(userName, password); 

    session = Session.getInstance(properties, authenticator); 

    // session.setDebug(true); 

    Transport tr = session.getTransport("smtp"); 
    tr.connect(); 
    /* 
    * do I need more than just connect? Since when i try to send email with 
    * incorrect credentials it fails to do so. But I want to check 
    * credentials without sending an email. Assume that POP3/IMAP username 
    * is not same as the SMTP username, since that might be one of the 
    * cases 
    */ 
} 
} 

class SMTPAuthenticator extends Authenticator { 

private String userName = null; 
private String password = null; 

public SMTPAuthenticator(String userName, String password) { 
    this.userName = userName; 
    this.password = password; 

} 

@Override 
public PasswordAuthentication getPasswordAuthentication() { 
    return new PasswordAuthentication(userName, password); 
} 
}
+0

잘 작동합니다. 귀하의 질문은 무엇인가? –

+0

잘 작동하지 않습니다. 내가 말했듯이 2/3/4은 GMail이 허용하지 않는 설정 때문에 어떤 종류의 오류를보고해야합니다. 그래서 .connect() 이상으로 뭔가가 필요할 것입니다. 정확히 무엇입니까? 그것은 내가 생각하려고하는 것입니다. – DarK

답변

2

SMTP 프로토콜 자체 때문에이 문제가 발생합니다. 인증은 프로토콜에서 필수 사항이 아닙니다. "connect"메소드를 호출하면 최소한 "EHLO"명령을 수행하여 지원되는 확장을 리턴합니다. 인증은 RFC에 정의 된 확장입니다. JavaMail은 명령을 지원하려고 시도하고 서버가 자격 증명을 제공 한 경우 "AUTH"명령을 시도합니다. 인증 확장 정보는 "STARTTLS"(확장 기능)를 수행 한 후에 만 ​​전송 될 수 있습니다. 이는 일반 채널에서 PLAIN 인증을 사용하는 것이 안전하지 않기 때문입니다. 이것이 JavaMail이 지원되는 확장을 업데이트하는 "STARTTLS"후에 두 번째 "EHLO"를 수행하는 이유입니다.

그래서 간단한 해결책은 "mail.smtp.starttls.enable"을 true로 설정하는 것입니다. JavaMail (1.4.5 이상)에서 서버가 지원한다고 말하면 "STARTTLS"명령이 전송되고 그렇지 않은 경우 업데이트 된 확장을 가져오고 필요할 경우 JavaMail이 "AUTH"를 수행 할 수 있음을 의미합니다.

관련 문제