2019-12-05 本文已影响0人
青丝如梦
使用Java验证smtp服务器凭据,无实际操作
依赖:
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
代码:
import java.util.Properties;
import javax.mail.AuthenticationFailedException;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
/**
* @author
* 日期 2019/12/5 13:58
* 描述
* @version 1.0
* @since 1.0
*/
public class ConfirmSmtp {
public static void main(String[] args) {
int port = 25;
String host = "smtp.qq.cn";
String user = "xxxxxxxxx@qq.com";
String pwd = "xxxxxxxxx";
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();
} catch (Exception e) {
System.out.println("other exception");
e.printStackTrace();
}
}
}