Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过smtp使用java代码发送电子邮件_Java_Smtp_Jakarta Mail - Fatal编程技术网

通过smtp使用java代码发送电子邮件

通过smtp使用java代码发送电子邮件,java,smtp,jakarta-mail,Java,Smtp,Jakarta Mail,我得到以下错误和超时异常 输入代码: Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host","smtp.gmail.com"); props.put("mail.smtp.port", "587"); props.put("mail.debug",

我得到以下错误和超时异常

输入代码:

Properties props = new Properties();  
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host","smtp.gmail.com"); 
props.put("mail.smtp.port", "587");
props.put("mail.debug", "true");
调试SMTP:尝试连接到主机“SMTP.gmail.com”,端口587,isSSL true Exceptioninthread“main”java.lang.RuntimeException:com.sun.mail.util.MailConnectException:无法连接到主机,端口:smtp.gmail.com,587;超时-1

private static void sendFromGMail(String from, String pass, String[] to, String subject, String body) {
    Properties props = System.getProperties();
    String host = "smtp.gmail.com";
  //String host="localhost";
  //props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
    props.put("mail.smtp.starttls.enable", "true");
  //props.put("mail.smtp.host", host);
    props.put("mail.smtp.ssl.trust", host);
    props.put("mail.smtp.user", from);
    props.put("mail.smtp.password", pass);
    props.put("mail.smtp.port", "587");//587
    props.put("mail.smtp.auth", "true");
  //System.out.println("1");

    Session session = Session.getDefaultInstance(props);
    MimeMessage message = new MimeMessage(session);

    try {
         //System.out.println("2");

        message.setFrom(new InternetAddress(from));
        InternetAddress[] toAddress = new InternetAddress[to.length];

        // To get the array of addresses
        for( int i = 0; i < to.length; i++ ) {
            toAddress[i] = new InternetAddress(to[i]);
        }

        for( int i = 0; i < toAddress.length; i++) {
            message.addRecipient(Message.RecipientType.TO, toAddress[i]);
        }

         //System.out.println("3");

        message.setSubject(subject);
        message.setText(body);
        // System.out.println("4");

        Transport transport = session.getTransport("smtp");
        // System.out.println("5");

        transport.connect(host, from, pass);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
        //System.out.println("sent successfully...");
    }
    catch (AddressException ae) {
        ae.printStackTrace();
    }
    catch (MessagingException me) {
        me.printStackTrace();
    }
  }
我使用了以下jar文件

java-mail-1.4.4
javamail-smtp-1.4.2
pop3
mail-6.0.0-sources

忽略大多数其他答案和评论,它们是错误的或不相关的。其中很多都包括这些。您的原始代码很好

您很可能无法连接,因为存在阻止直接访问的防火墙。JavaMail常见问题解答已经发布


如果您无法直接连接,需要通过代理服务器连接,.

发布堆栈跟踪、生成堆栈跟踪的代码等,以重现问题。顺便说一句,您使用代码片段编写简单的英语,反之亦然versa@Aniketsahrawat Properties props=新属性();props.put(“mail.smtp.auth”,“true”);props.put(“mail.smtp.starttls.enable”、“true”);props.put(“mail.smtp.host”,host);props.put(“mail.smtp.port”,port);props.put(“mail.debug”,“true”);host=“smtp.gmail.com”;port=:587;yes..Session Session=Session.getInstance(props,new javax.mail.Authenticator(){protected PasswordAuthentication getPasswordAuthentication(){return new PasswordAuthentication(username,password);}}}};@Maurice perrySo属性“mail.smtp.ssl.trust”会有什么不同?
java-mail-1.4.4
javamail-smtp-1.4.2
pop3
mail-6.0.0-sources