2013-01-08 161 views
0

正在使用的Liferay 6和创建一个自定义class..i要创建邮件通知功能......我已经写在我的课无法连接到SMTP主机:localhost,端口:25?

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"); 
    } 

我已经在我的ROOT.xml文件写入以下的事情下面的代码tomcatserver目录

<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="[email protected]" //MyEmailId 
        password="*******" //My password 
        mail.smtp.socketFactory.class="javax.net.ssl.SSLSocketFactory" 
    /> 

但它给我下面的错误...谁能帮我out..where正在做错误

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

回答

3

无您的root.xml文件中设置的属性正被应用程序使用。

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

不要忘了阅读common mistakeshow to connect to Gmail的JavaMail FAQ。 (提示:你不需要任何socketFactory属性。)

+0

我已经做了你说的方式...我已经把下面的代码放在我的portal-ext-porperties文件中mail.session.jndi.name = mail/MailSession但我仍然有同样的错误 –

+0

对不起,我不明白你做了什么。你把什么“下面的代码”放在哪里?也许你可以发布你尝试过的更新代码和配置? –

+0

请参阅http://stackoverflow.com/a/8924168/535646如何访问您的JNDI设置。 –

相关问题