2010-06-17 3 views

답변

16

이 질문 : 'Verify mail server connection programmatically in ColdFusion'이 허용 대답의 일부로서 자바 솔루션이있다 :이 시도했습니다

int port = 587; 
String host = "smtp.gmail.com"; 
String user = "[email protected]"; 
String pwd = "email password"; 

try { 
    Properties props = new Properties(); 
    // required for gmail 
    props.put("mail.smtp.starttls.enable","true"); 
    props.put("mail.smtp.auth", "true"); 
    // or use getDefaultInstance instance if desired... 
    Session session = Session.getInstance(props, null); 
    Transport transport = session.getTransport("smtp"); 
    transport.connect(host, port, user, pwd); 
    transport.close(); 
    System.out.println("success"); 
} 
catch(AuthenticationFailedException e) { 
     System.out.println("AuthenticationFailedException - for authentication failures"); 
     e.printStackTrace(); 
} 
catch(MessagingException e) { 
     System.out.println("for other failures"); 
     e.printStackTrace(); 
} 
+0

을하고 Gmail이 아닌 다른 주소에 대해 작동하지 않습니다 - 항상 성공을 인쇄합니다. 왜 그런가? 내가 smtp.1and1.com 및 잘못된 자격 증명을 사용하여 여전히 성공을 인쇄하고 있습니다 – ntgCleaner

+0

올바른 솔루션을 사용해 보았지만 정확한 데이터 (지금 해당 이메일 데이터 설정으로 이메일을 보내고 있습니다)를 얻었습니다. 535 잘못된 인증 데이터 이유가 무엇입니까? ? –

5
public boolean confirmSMTP(String host, String port, String username, String password, String auth, String enctype) { 
    boolean result = false; 
    try { 
     Properties props = new Properties(); 
     if (auth.equals(true)) { 
      props.setProperty("mail.smtp.auth", "true"); 
     } else { 
      props.setProperty("mail.smtp.auth", "false"); 
     } 
     if (enctype.endsWith("TLS")) { 
      props.setProperty("mail.smtp.starttls.enable", "true"); 
     } else if (enctype.endsWith("SSL")) { 
      props.setProperty("mail.smtp.startssl.enable", "true"); 
     } 
     Session session = Session.getInstance(props, null); 
     Transport transport = session.getTransport("smtp"); 
     int portint = Integer.parseInt(port); 
     transport.connect(host, portint, username, password); 
     transport.close(); 
     result = true; 

    } catch(AuthenticationFailedException e) { 
     Logging.addMsg(e.toString(), "SMTP: Authentication Failed", false, true); 

    } catch(MessagingException e) { 
     Logging.addMsg(e.toString(), "SMTP: Messaging Exception Occurred", false, true); 
    } catch (Exception e) { 
     Logging.addMsg(e.toString(), "SMTP: Unknown Exception", false, true); 
    } 

    return result; 
} 
+1

박, 나는 이것을 시험해 보았습니다. 그러나 위의 대답과 마찬가지로, 저는해서는 안될 때 성공 메시지를 얻었습니다. 내가하는 일에 뭔가 이상이있을 수 있니? – ntgCleaner

관련 문제