Jetez un oeil à this page pour obtenir un exemple comment envoyer des courriels. Vous devriez utiliser la classe Transport pour envoyer des emails.
public static void send(String smtpServer, String to, String from
, String subject, String body) throws Exception
{
Properties props = System.getProperties();
// -- Attaching to default Session, or we could start a new one --
props.put("mail.smtp.host", smtpServer);
Session session = Session.getDefaultInstance(props, null);
// -- Create a new message --
Message msg = new MimeMessage(session);
// -- Set the FROM and TO fields --
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to, false));
// -- We could include CC recipients too --
// if (cc != null)
// msg.setRecipients(Message.RecipientType.CC
// ,InternetAddress.parse(cc, false));
// -- Set the subject and body text --
msg.setSubject(subject);
msg.setText(body);
// -- Set some other header information --
msg.setHeader("X-Mailer", "LOTONtechEmail");
msg.setSentDate(new Date());
// -- Send the message --
Transport.send(msg);
System.out.println("Message sent OK.");
}
EDIT:
En raison de votre commentaire ...
Vérifiez si une personne obstrue le port 25 (pare-feu, autre application)
l'authentification pourrait aussi être un problème , que vous pouvez trouver un bon exemple here
props.put("mail.smtp.auth", "true");
Authenticator auth = new SMTPAuthenticator("[email protected]", "mypassword");
Pourquoi utilisez-vous URLConnection pour envoyer un e-mail? – skaffman