我试图从 java 发送邮件,但在我尝试的每种情况下我都会收到相同的错误。我使用的是 Windows 8,在我遇到套接字问题之前,但我在带有 xp 的虚拟机中运行此代码,它给我发送了同样的错误!我读到这可能是我的 jdk、防火墙、防病毒或网络问题,但我已经尝试过了,但我没有解决方案。我希望有人能告诉这里发生了什么!我将代码留在这里:
package mail;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class Mail
{ // SE DECLARAN LAS VARIABLES DE CORREO , DOMINIO, PUERTO Y PASS
private static final String SMTP_HOST_NAME = "smtp.gmail.trust";
private static final int SMTP_HOST_PORT = 587;
private static final String SMTP_AUTH_USER = "josevalverde93@gmail.com";
private static final String SMTP_AUTH_PWD = "password";
public static void main(String[] args) throws Exception {
new Mail().test();
}
public void test() throws Exception {
Properties props = new Properties();`
props.put("mail.transport.protocol", "smtps");`
props.put("mail.smtps.trust", SMTP_HOST_NAME);`
props.put("mail.smtps.auth", "true");`
props.put("mail.smtps.quitwait", "false");`
props.setProperty("mail.smtp.host", "stmp.gmail.com");`
props.setProperty("mail.smtp.starttls.enable", "true");`
props.setProperty("mail.smtp.port", "587");`
props.setProperty("mail.smtp.user", "josevalverde93@gmail.com");`
props.setProperty("mai.smtp.auth", "true");`
Session mailSession = Session.getDefaultInstance(props);`
mailSession.setDebug(true); Transport transport = mailSession.getTransport();`
MimeMessage message = new MimeMessage(mailSession); `
message.setSubject("Testing SMTP-SSL");`
// Aqui VA EL TITULO DEL EMAIL
message.setContent("Este correo ha sido enviado desde Netbeans con Java", "text/plain");
// Aqui VA EL CONTENIDO,,
message.addRecipient(Message.RecipientType.TO, new InternetAddress("andima9886@gmail.com"));
// DESTINARIO
transport.connect (SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER, SMTP_AUTH_PWD);
transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
transport.close();
}
}
输出是:
DEBUG: setDebug: JavaMail version 1.5.0
DEBUG: getProvider() returning
javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.gmail.trust", port 587, isSSL true
Exception in thread "main" com.sun.mail.util.MailConnectException: Couldn't connect to
host, port: smtp.gmail.trust, 587; timeout -1;
nested exception is:
java.net.UnknownHostException: smtp.gmail.trust
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1961)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
at javax.mail.Service.connect(Service.java:345)
at mail.Mail.test(Mail.java:44)
at mail.Mail.main(Mail.java:17)
Caused by: java.net.UnknownHostException: smtp.gmail.trust
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:176)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
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:297)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:229)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1927)
... 4 more
Java Result: 1
谢谢你,来自哥斯达黎加的问候。