2013-03-12 44 views
0

我需要實現使用jboss seam發送郵件的故障轉移功能。我試圖在mail-service.xml中配置兩個JNDINames。但我沒有得到如何在代碼中設置第二個JNDI名稱。 此外,我沒有得到如何設置代碼中的smtp主機。 我的代碼:爲Jboss Seam郵件配置故障轉移

@Name("emailService") 
@AutoCreate 
public class EmailService { 
private static final Log logger = LogFactory.getLog(EmailService.class); 

@In(create = true) 
private Renderer renderer; 

@Asynchronous 
public void sendMessage(@Duration long delay, String template, 
     Object infoNeededForTemplate) { 
    MailSession mailSession = new MailSession(); 
    try { 
     Contexts.getEventContext().set("currentMail", infoNeededForTemplate); 
     renderer.render(template); 
     logger.info("Email send to " + ((Mail) infoNeededForTemplate).getToEmail()); 
    } catch (Exception e) { 
     logger.error("Error while sending mail: Message = " + e.getMessage()); 
     try { 
      renderer.render(template); 
      logger.info("Email send to " + ((Mail) infoNeededForTemplate).getToEmail()); 
     } catch (Exception e1) { 
      logger.error("Error while sending mail: Message = " + e1.getMessage()); 
     } 
    } 
} 

}

我的mail-service.xml中:

<?xml version="1.0" encoding="UTF-8"?> 
<server> 
<mbean code="org.jboss.mail.MailService" name="jboss:service=Mail"> 
    <attribute name="JNDIName">java:/Mail</attribute> 
    <attribute name="User">nobody</attribute> 
    <attribute name="Password">password</attribute> 
    <attribute name="Configuration"> 
    <!-- A test configuration --> 
    <configuration> 
    <!-- Change to your mail server prototocol --> 
    <property name="mail.store.protocol" value="pop3"/> 
    <property name="mail.transport.protocol" value="smtp"/> 

    <!-- Change to the user who will receive mail --> 
    <property name="mail.user" value="nobody"/> 

    <!-- Change to the mail server --> 
    <property name="mail.pop3.host" value="pop3.nosuchhost.nosuchdomain.com"/> 

    <!-- Change to the SMTP gateway server --> 
    <property name="mail.smtp.host" value="HOST_1"/> 

    <!-- The mail server port --> 
    <property name="mail.smtp.port" value="25"/> 

    <!-- Change to the address mail will be from --> 
    <property name="mail.from" value="[email protected]"/> 

    <!-- Enable debugging output from the javamail classes --> 
    <property name="mail.debug" value="true"/> 
    <property name="mail.smtp.auth" value="false"/> 
    <property name="mail.smtp.starttls.enable" value="false"/> 
    </configuration> 
</attribute> 
<depends>jboss:service=Naming</depends> 
</mbean> 

<mbean code="org.jboss.mail.MailService" name="jboss:service=Mail"> 
<attribute name="JNDIName">java:/Mail1</attribute> 
<attribute name="User">nobody</attribute> 
<attribute name="Password">password</attribute> 
<attribute name="Configuration"> 
    <!-- A test configuration --> 
    <configuration> 
    <!-- Change to your mail server prototocol --> 
    <property name="mail.store.protocol" value="pop3"/> 
    <property name="mail.transport.protocol" value="smtp"/> 

    <!-- Change to the user who will receive mail --> 
    <property name="mail.user" value="nobody"/> 

    <!-- Change to the mail server --> 
    <property name="mail.pop3.host" value="pop3.nosuchhost.nosuchdomain.com"/> 

    <!-- Change to the SMTP gateway server --> 
    <property name="mail.smtp.host" value="HOST_2"/> 

    <!-- The mail server port --> 
    <property name="mail.smtp.port" value="25"/> 

    <!-- Change to the address mail will be from --> 
    <property name="mail.from" value="[email protected]"/> 

    <!-- Enable debugging output from the javamail classes --> 
    <property name="mail.debug" value="true"/> 
    <property name="mail.smtp.auth" value="false"/> 
    <property name="mail.smtp.starttls.enable" value="false"/> 


    </configuration> 
</attribute> 
<depends>jboss:service=Naming</depends> 
</mbean> 
</server> 

能否請你幫我在任我怎樣才能配置&使用兩種JNDIs或者設置SMTP主機服務器在發送郵件? 非常感謝。

問候, SAURABH

回答

0

您可以在Transport.connect方法明確指定主機名;有關詳細信息,請參閱javadocs。

+0

非常感謝您的回覆。正如我在問題中提到的,我沒有使用Javamail功能,而是使用Jboss Seam。因此,對我來說,設置運輸運行將不起作用。我需要1.將不同的JNDIName注入到現有上下文中2.爲現有上下文注入不同的主機。 如果有可能使用帶有接縫的Transport對象,請問您可以舉一些例子嗎?謝謝。 – Saurabhcdt 2013-03-13 06:26:51

+0

我對Seam一無所知,所以我不知道它是如何限制你使用JavaMail API的。 – 2013-03-13 19:45:08

+0

我仍在尋找答案。任何人都可以幫忙嗎??? – Saurabhcdt 2013-04-12 09:47:24