Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate smtp server credentials using java without actually sending mail

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?

like image 483
Roshan Avatar asked Jun 17 '10 10:06

Roshan


People also ask

Can I use SMTP without authentication?

With the new protocols in place, an authentication mechanism (like a password) is necessary to log in to an email service provider's SMTP server. This means that only verified users can send messages via that server – providing a basic level of security against the sending of unsolicited spam and phishing emails.


1 Answers

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 = "[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();
 }
like image 195
tim_yates Avatar answered Nov 13 '22 03:11

tim_yates