Hier ist mein 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();
}
}
}
Und ich bekomme diesen Fehler immer
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)
Ich bekomme nicht den Fehler, warum dies passiert. Bitte helfen Sie mir, diesen Fehler zu beheben.
Fehler ist selbsterklärend: javax.mail.MessagingException: Could not connect to SMTP Host: localhost, port: 25;
Sie haben keinenSMTPserver auf localhost, aber Sie konfigurieren ihn dort:
// Assuming you are sending email from localhost
String Host = "localhost";
...
// Setup mail server
properties.setProperty("mail.smtp.Host", Host);
Sie müssen also
Sie sollten den kostenlosen Google SMTP-Server als Test verwenden.
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
Melden Sie sich als Nächstes mit Ihrem Google Mail-Konto an und aktivieren Sie weniger sichere Apps .
Sie sollten sich diese beiden Zeilen ansehen:
javax.mail.MessagingException: Could not connect to SMTP Host: localhost, port: 25;
Caused by: Java.net.ConnectException: Connection refused: connect
Der Fehler lautet: "Localhost wird an Port 25 nicht überwacht".
Sie versuchen, localhost: 25 als Mail-Server zu verwenden, dort ist jedoch kein Server vorhanden.
Hier ist die Arbeitslösung bro. es ist garantiert
1) Öffnen Sie zunächst Ihr Google Mail-Konto, von dem aus Sie E-Mails senden möchten, wie in Ihrem Fall "" [email protected] ".
2) Öffnen Sie diesen Link unten https://support.google.com/accounts/answer/6010255?hl=de
3) Klicken Sie in "Mein Konto" auf "Gehen Sie zum Abschnitt" Weniger sichere Apps "." Möglichkeit
4) Dann einschalten
5) das ist es (: