Bonjour a tous;
il y a une semaine que je boucle sur un probleme de connection a un server mail en mode TLS et SSL mais sans resultas.j'ai pas compris l'astuce.
mon bout de code:
public void sendmail(){
try {
//System.setProperty("javax.net.debug", "all");
String protocol = "smtp";
String mailSmtpHost = "mon_servermail";
String socketFactory = "javax.net.ssl.SSLSocketFactory";
String port = "25";
String login = "toto";
String password = "****";
String from = "toto@****.***";
String smtpAuth = "true";
Properties props = new Properties();
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.auth", smtpAuth);
props.put("mail.smtp.socketFactory.class", socketFactory);
props.put("mail.smtp.socketFactory.port", port);
props.put("mail.smtp.port", port);
//props.put("mail.smtp.auth.plain.disable" ,"true");
// Creating mail session
session = Session.getDefaultInstance(props, new Authenticator() {
protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
return new javax.mail.PasswordAuthentication(login, password);
}
});
URLName url = new URLName(protocol, mailSmtpHost, Integer.parseInt(port), "", userName, password);
com.sun.mail.smtp.SMTPSSLTransport transport = new com.sun.mail.smtp.SMTPSSLTransport(session, url);
transport.setStartTLS(true);
// Constructing Message object
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse("xxxx@xxxx.xx", false));
msg.setSubject("test");
msg.setText("mail for test");
Transport.send(msg);
transport.close();
}
catch (MessagingException me)
{
me.printStackTrace();
System.out.println("Unable to send mail");
}
}
quelqu'un pourra m'aider?
merci.
Partager