0

我需要实现故障转移功能以使用 jboss seam 发送邮件。我尝试在 mail-service.xml 中配置两个 JNDIName。但我不知道如何在代码中设置第二个 JNDI 名称。另外,我不知道如何在代码中设置 smtp 主机。我的代码:

@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());  
        }  
    }  
}  

}

我的邮件服务.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="nobody@abc.com"/>  

    <!-- 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="nobody@abc.com"/>  

    <!-- 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>

您能否帮助我了解如何配置和使用两个 JNDI 或即时设置 SMTP 主机服务器以发送邮件?提前非常感谢。

问候, 索拉布

4

1 回答 1

0

您可以在 Transport.connect 方法中明确指定主机名;有关详细信息,请参阅 javadocs。

于 2013-03-12T20:57:05.203 回答