0

I need to implement failover functionality for sending mails using jboss seam. I tried to configure two JNDINames in mail-service.xml. But I'm not getting how i can set 2nd JNDI name in code. Also, I'm not getting how can i set smtp host in code. MY Code:

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

}

My 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="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>

Can you please help me on either how can i configure & use two JNDIs OR set SMTP host server on the fly for sending mail ? Thanks a lot in advance.

Regards, Saurabh

Saurabhcdt
  • 1,010
  • 1
  • 12
  • 24

1 Answers1

0

You can specify the host name explicitly in the Transport.connect method; see the javadocs for details.

Bill Shannon
  • 29,579
  • 6
  • 38
  • 40
  • Thanks a lot for you reply. As I mentioned in problem, I'm not using Javamail feature but using Jboss Seam. So for me, setting Transport on the fly will not work. I need to either 1. Inject different JNDIName to existing context 2. Inject different host for existing context. If its possible to use Transport object with seam, could you please post some example? Thanks. – Saurabhcdt Mar 13 '13 at 06:26
  • I don't know anything about Seam so I don't know how it restricts your use of the JavaMail APIs. – Bill Shannon Mar 13 '13 at 19:45