Probleme envoie email avec java

Fermé
xdev10 Messages postés 8 Date d'inscription vendredi 27 mars 2015 Statut Membre Dernière intervention 23 décembre 2015 - 23 déc. 2015 à 12:50
xdev10 Messages postés 8 Date d'inscription vendredi 27 mars 2015 Statut Membre Dernière intervention 23 décembre 2015 - 23 déc. 2015 à 13:09
Bonjour, j'ai essaye d'envoyer un email avec java mais une interruption a ete declenchee et j'ai pas compris comment la corrigee , merci de m'aider

package testEmail;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class main {

public static void main(String[] args) {
// Recipient's email ID needs to be mentioned.
String to = "daliayoub@yahoo.fr";

// Sender's email ID needs to be mentioned
String from = "csoufiene@gmail.com";


// Assuming you are sending email from localhost
String host = "localhost";

// Get system properties
Properties properties = System.getProperties();

// Setup mail server
properties.setProperty("mail.smtp.host", host);

// Get the default Session object.
Session session = Session.getDefaultInstance(properties);

try{
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);

// Set From: header field of the header.
message.setFrom(new InternetAddress(from));

// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

// Set Subject: header field
message.setSubject("This is the Subject Line!");

// Now set the actual message
message.setText("This is actual message");

// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}

}




javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
nested exception is:
java.net.ConnectException: Connection refused: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1972)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:642)
at javax.mail.Service.connect(Service.java:295)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
at javax.mail.Transport.send0(Transport.java:194)
at javax.mail.Transport.send(Transport.java:124)
at testEmail.main.main(main.java:51)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:319)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:233)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1938)
... 7 more


A voir également:

2 réponses

KX Messages postés 16733 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 31 janvier 2024 3 015
23 déc. 2015 à 13:08
Bonjour,

Dans ton code tu as mis
String host = "localhost";

Mais est-ce que tu as bien un serveur SMTP sur ta machine locale ?
A priori non, d'où l'exception
Could not connect to SMTP host: localhost


Si tu veux envoyer un mail Gmail tu devrais plutôt essayer avec
host = "smtp.gmail.com"
. Il faudra changer le port aussi, pour Gmail ce sera le 465, et t'authentifier avec ton compte Gmail.

Voir un (vieux) code complet : https://mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/

Attention : pour éviter le spam Google limite le nombre d'envoi de mails par jour, si ton programme venait à envoyer trop de mails ton compte pourrait être désactivé : https://support.google.com/a/answer/166852?hl=fr
1
jordane45 Messages postés 38144 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 21 avril 2024 4 650
23 déc. 2015 à 12:56
Bonjour,

Ton programme java est hébergé sur un serveur ?
Se serveur dispose d'un serveur SMTP ?

Par ce que ... LOCALHOST signifie : PC SUR LEQUEL SE TROUVE LE PROGRAMME
Et...


// Setup mail server
properties.setProperty("mail.smtp.host", host);

.. Es tu sûr que le serveur smtp que tu utilises est bien : "mail.smtp.host" ?

Là...Je pense que tu as un souci de configuration...

0
xdev10 Messages postés 8 Date d'inscription vendredi 27 mars 2015 Statut Membre Dernière intervention 23 décembre 2015
23 déc. 2015 à 13:09
bon je suis debutant pour java , j'ai effectue une petite recherche sur google ,comment envoyer un mail depuis java et c'est ce que j'ai trouve , donc pour l'hebergement je pense pas ..
j'ai effectue quelques changement sur le code
Properties properties = new Properties();
properties.put("mail.transport.protocol", "smtp");
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.port", "25");
il m'a affiche
530 5.7.0 Must issue a STARTTLS command first. gb9sm9661467wjb.26 - gsmtp
0