2013-01-08 179 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設置。 –

相關問題