Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.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/email/3.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
使用MS exchange server从java发送邮件,并给出连接超时_Java_Email_Jakarta Mail_Exchange Server - Fatal编程技术网

使用MS exchange server从java发送邮件,并给出连接超时

使用MS exchange server从java发送邮件,并给出连接超时,java,email,jakarta-mail,exchange-server,Java,Email,Jakarta Mail,Exchange Server,我正在尝试从我的java web应用程序发送邮件,其中我的邮件服务器主机是exg6.exghost.com,但它的连接超时了。当我的主机是smtp.gmail.com时,同样的代码也可以工作 我的代码是 Properties props = new Properties(); props.put("mail.smtp.host", "exg6.exghost.com"); props.put("mail.transport.protocol", "smtp");

我正在尝试从我的java web应用程序发送邮件,其中我的邮件服务器主机是exg6.exghost.com,但它的连接超时了。当我的主机是smtp.gmail.com时,同样的代码也可以工作

我的代码是

Properties props = new Properties();
        props.put("mail.smtp.host", "exg6.exghost.com");
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.auth", "false");

        Session session1 = Session.getInstance(props,
            new javax.mail.Authenticator() {
                protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
                    return new javax.mail.PasswordAuthentication(username,password);
                }
            });

        try {

            Message message = new MimeMessage(session1);
            message.setFrom(new InternetAddress(username));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse("xyz@gmail.com"));
            message.setSubject("Testing Subject");
            message.setText("Dear Mail Crawler," +
                    "\n\n No spam to my email, please!");

            Transport.send(message);

            System.out.println("Done");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
我还尝试了mail.smtp.authtrue,但连接再次超时

请提供帮助。

有关详细信息,请参阅JavaMail常见问题解答

看起来您正在使用默认(纯文本)SMTP端口进行连接。您的服务器很可能只接受SMTP over SSL端口上的连接。添加以下内容:

props.put("mail.smtp.ssl.enable", "true");

主机exg6.exghost.com是否正在侦听正确的端口?我如何知道主机是否正在侦听正确的端口?我终于让它正常工作了。参考已接受的答案