8

To verify smtp server credentials shall I use transport.connect()?

Session session = Session.getInstance(properties,authenticator);

 Transport tr=session.getTransport("smtp");

 tr.connect();

Is it correct method to check smtp server credentials?

Harshal Patil
  • 6,659
  • 8
  • 41
  • 57
Roshan
  • 2,019
  • 8
  • 36
  • 56

2 Answers2

21

This question: 'Verify mail server connection programmatically in ColdFusion' has a java solution as part of the accepted answer:

int port = 587;
String host = "smtp.gmail.com";
String user = "username@gmail.com";
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();
 }
Community
  • 1
  • 1
tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • I've tried this and it does not work for addresses other than gmail - always prints success. Any idea why? I am using smtp.1and1.com and incorrect credentials still print success – ntgCleaner Feb 10 '16 at 15:47
  • I tried your solution but even with correct data (I am sending emails with those email data settings right now) I get: 535 Incorrect authentication data Any idea why? – Francisco Souza May 18 '16 at 21:06
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;
}
lobzik
  • 10,974
  • 1
  • 27
  • 32
jBak
  • 51
  • 1
  • 1
  • 1
    Bak, I tried this, but just like the answer above, I'm getting a success message when I should not be. Could there be something wrong with what I'm doing? – ntgCleaner Feb 10 '16 at 15:56