Voici mon code
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 MailSendClass {
public static void main (String [] args){
// Recipient's email ID needs to be mentioned.
String to = "[email protected]";
// Sender's email ID needs to be mentioned
String from = "[email protected]";
// 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("Thanks for registering on our website!");
// Now set the actual message
message.setText("Welcome To Job Portal !!!! Again Thanks ");
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
Et je reçois cette erreur à chaque fois
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:1706)
at com.Sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.Java:525)
at javax.mail.Service.connect(Service.Java:291)
at javax.mail.Service.connect(Service.Java:172)
at javax.mail.Service.connect(Service.Java:121)
at javax.mail.Transport.send0(Transport.Java:190)
at javax.mail.Transport.send(Transport.Java:120)
at MailSendClass.main(MailSendClass.Java:58)
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:345)
at Java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.Java:206)
at Java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.Java:188)
at Java.net.PlainSocketImpl.connect(PlainSocketImpl.Java:172)
at Java.net.SocksSocketImpl.connect(SocksSocketImpl.Java:392)
at Java.net.Socket.connect(Socket.Java:589)
at Java.net.Socket.connect(Socket.Java:538)
at com.Sun.mail.util.SocketFetcher.createSocket(SocketFetcher.Java:284)
at com.Sun.mail.util.SocketFetcher.getSocket(SocketFetcher.Java:227)
at com.Sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.Java:1672)
... 7 more
BUILD SUCCESSFUL (total time: 3 seconds)
Je ne comprends pas pourquoi cela se produit. S'il vous plaît aidez-moi à corriger cette erreur.
L'erreur est auto-explicative: javax.mail.MessagingException: Could not connect to SMTP Host: localhost, port: 25;
Vous n'avez pas de serveurSMTPsur localhost, mais vous le configurez ici:
// Assuming you are sending email from localhost
String Host = "localhost";
...
// Setup mail server
properties.setProperty("mail.smtp.Host", Host);
Donc vous devez:
Vous devez utiliser le serveur SMTP gratuit de Google comme test.
mail.Host=smtp.gmail.com
mail.username=//your gmail
mail.password=//your password
mail.defaultEncoding=UTF-8
mail.smtp.auth=true
mail.smtp.starttls.required=true
mail.smtp.starttls.enable=true
mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
mail.smtp.socketFactory.fallback=false
mail.smtp.port=465
mail.smtp.socketFactory.port=465
Ensuite, connectez-vous avec votre compte Gmail et activez les applications moins sécurisées .
Vous devriez regarder ces deux lignes:
javax.mail.MessagingException: Could not connect to SMTP Host: localhost, port: 25;
Caused by: Java.net.ConnectException: Connection refused: connect
L'erreur est: "Il n'y a rien d'écoute sur localhost sur le port 25".
Vous essayez d'utiliser localhost: 25 comme serveur de messagerie, mais il n'y a pas de serveur.
Voici la solution de travail bro. c'est garanti
1) Tout d’abord, ouvrez votre compte Gmail à partir duquel vous souhaitez envoyer un courrier, comme dans votre cas "" [email protected] "
2) ouvrez ce lien ci-dessous https://support.google.com/accounts/answer/6010255?hl=fr
3) Cliquez sur "Accédez à la section" Applications moins sécurisées "dans Mon compte." option
4) Puis allumez-le
5) c'est tout (: