1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| String host = "auth.smtp.1and1.fr"; //Saic Mail
String port = "587";
host = "smtp.gmail.com";
port ="587";
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", username);
props.put("mail.smtp.password", password);
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.ssl.enable", "false");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.tls", "true");
props.put("mail.smtp.port", port);
props.put("mail.smtp.socketFactory.class", "javax.net.SocketFactory");
props.put("mail.smtp.ssl.trust", host);
Session session;
session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
username, pword );
}
});
try {
Transport transport = session.getTransport("smtp");
transport.connect(host, username, password);
transport.close();
System.out.println ("connect ok"); |
Partager