无法连接到SMTP主机:localhost,端口:25?

时间:2013-01-08 06:19:09

标签: tomcat7 javamail sendmail portlet liferay-6

我正在使用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

1 个答案:

答案 0 :(得分:3)

您的应用程序正在使用您在root.xml文件中设置的所有属性。

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

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