【问题标题】:Could not connect to SMTP host: localhost, port: 25?无法连接到 SMTP 主机:localhost,端口:25?
【发布时间】:2012-12-21 23:15:56
【问题描述】:

我正在使用 liferay 6 并创建了一个自定义类..我想创建邮件通知功能...我在我的类中编写了以下代码

private void SendEmail(NotificationObject pNotificatonObj,
            String[] pReciepientAddresses) throws MessagingException {

        log.info("In SendMail");
        Properties props = new Properties();
        props.put("mail.debug", "true");
        props.put("mail.smtp.socketFactory.fallback", "false");
        Session session = Session.getInstance(props);
        Message msg = new MimeMessage(session);
        InternetAddress addressFrom = new InternetAddress(
                pNotificatonObj.get_From());
        msg.setFrom(addressFrom);
        // InternetAddress addressTo = new
        // InternetAddress(pNotificatonObj.get_To());

        InternetAddress[] addressTo = new InternetAddress[pReciepientAddresses.length];
        log.info("ADDRESS ARRAY LENGTH In Send Mail: - " + pReciepientAddresses.length);
        for (int i = 0; i < pReciepientAddresses.length; i++) {
            log.info("ADDRESS ARRAY LENGTH In Send Mail: - " + pReciepientAddresses[i]);
            addressTo[i] = new InternetAddress(pReciepientAddresses[i]);
        }
        // log.info("INTERNET ADRESS ARRAY LENGTH : - " + addressTo1.length);
        msg.setRecipients(RecipientType.TO, addressTo);

        // msg.addRecipients(Message.RecipientType.TO, addressTo);
        // Setting the Subject and Content Type
        msg.setSubject(pNotificatonObj.get_Subject());
        msg.setContent(pNotificatonObj.get_HtmlString().toString().toString(),
                "text/html");
        Transport.send(msg);
        log.info("Send Mail Leave");
    }

我在tomcatserver目录的root.xml文件中写了以下内容

<Resource
                     name="mail/MailSession"
                     auth="Container"
                     type="javax.mail.Session"
                     mail.imap.host="localhost"
                     mail.pop.host="localhost"
                     mail.store.protocol="imap"
                     mail.transport.protocol="smtp"
                     mail.smtp.host="smtp.gmail.com"
                     mail.smtp.port="465"
                     mail.smtp.auth="true"
                     mail.smtp.starttls.enable="true"
                     mail.smtp.user="My@gmail.com" //MyEmailId
                     password="*******" //My password
                     mail.smtp.socketFactory.class="javax.net.ssl.SSLSocketFactory"
   />

但是它给了我以下错误...谁能帮帮我..我在哪里做错了

javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
  nested exception is:
    java.net.ConnectException: Connection refused: connect

【问题讨论】:

    标签: tomcat7 jakarta-mail sendmail portlet liferay-6


    【解决方案1】:

    您在 root.xml 文件中设置的这些属性都没有被您的应用程序使用。

    您需要更改您的应用程序以使用 JNDI 查找 JavaMail 会话,而不是使用 Session.netInstance 自己创建它,或者您需要更改您的应用程序以在用于创建的 Properties 对象上设置所有这些属性新的会话对象。

    别忘了阅读common mistakeshow to connect to Gmail 的JavaMail 常见问题解答。 (提示:您不需要任何 socketFactory 属性。)

    【讨论】:

    • 我按照你说的做了...我在我的 portal-ext-porperties 文件 mail.session.jndi.name=mail/MailSession 中放入了以下代码,但我仍然遇到同样的错误跨度>
    • 对不起,我不明白你做了什么。你把什么“以下代码”放在哪里?也许您可以发布您尝试过的更新代码和配置?
    猜你喜欢
    • 2016-07-29
    • 1970-01-01
    • 1970-01-01
    • 2014-10-23
    • 2011-11-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-09
    • 1970-01-01
    相关资源
    最近更新 更多