2010-04-21 239 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

它工作正常。你的問題是什麼? – 2010-04-21 09:54:01

+0

它不能正常工作。正如我提到2/3/4的情況下應報告某種錯誤,因爲所做的設置不是GMail接受的設置。 所以我可能需要的東西不僅僅是.connect() 究竟是什麼?那是我想要的。 – DarK 2010-04-21 12:16:12

回答

2

由於SMTP協議本身,您有此問題。協議中不強制認證。當你調用方法「connect」時,你至少會做一個「EHLO」命令來返回受支持的擴展。身份驗證是在RFC中定義的擴展。如果服務器說它支持,並且您提供了憑據,則JavaMail將嘗試執行「AUTH」命令而不是。事情是驗證擴展信息可能只有在你完成了「STARTTLS」(也是一個擴展)之後纔會發送,因爲在一個空閒信道上使用例如PLAIN驗證是不安全的。這就是爲什麼JavaMail在「STARTTLS」之後執行第二個「EHLO」以更新受支持的擴展。

因此,針對您的一個簡單解決方案問題是始終將「mail.smtp.starttls.enable」設置爲true。在JavaMail(至少1.4.5)中,這意味着如果服務器說它支持(否則不會),那麼將發送「STARTTLS」命令,那麼您將獲得更新的擴展,並且如果需要,JavaMail可以執行「AUTH」。

相關問題