0

我试图从 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

谢谢你,来自哥斯达黎加的问候。

4

2 回答 2

1

如果您阅读它所说的异常UnknownHostException: smtp.gmail.trust。它找不到主机 smtp.gmail.trust。您可能指的是 smtp.gmail.com。

此外,查看您的代码,您在设置属性时有几个错误。

于 2013-10-15T18:11:43.640 回答
1

问题可能是“smtp.gmail.trust”。你试过“smtp.gmail.com”吗?

要使用 Gmail SMTP 发送邮件,请尝试以下代码,它适用于我:

public void sendMail(final String senderEmailID, final String password, javax.mail.Address [] addresses) {
    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");

    Session session = null;
    try {
        session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(senderEmailID, password);
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
        return;
    }

    try {
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(senderEmailID));
        message.setRecipients(Message.RecipientType.TO, addresses);

        message.setSubject("Subject");

        String messageBody = "<h1>Message</h1>";

        message.setContent(messageBody.toString(), "text/html");
        Transport.send(message);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
于 2013-10-15T18:48:22.810 回答