2010-06-17 24 views

cevap

16

Bu soru: 'Verify mail server connection programmatically in ColdFusion' kabul edilen yanıt bir parçası olarak bir java çözümü vardır: Bu denedim

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

ve gmail dışındaki adresler için çalışmıyor - hep başarıyı yazdırır. Bir fikrin neden? Ben smtp.1and1.com kullanıyorum ve yanlış kimlik bilgileri hala baskı başardı – ntgCleaner

+0

Çözümünüzü denedim ama doğru verilerle bile (şu anda bu e-posta veri ayarları ile e-posta gönderiyorum) Aldığım: 535 Hatalı kimlik doğrulama verileri Herhangi bir fikir neden ? –

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

Bak, bunu denedim, ama aynen yukarıdaki cevap gibi, olmamam gerektiğinde bir başarı mesajı alıyorum. Yaptığım şeyle ilgili bir sorun olabilir mi? – ntgCleaner

İlgili konular