[jboss-user] [Beginners Corner] - Re: Problem with sending Email from jBoss

j-n00b do-not-reply at jboss.com
Thu Jan 22 08:38:51 EST 2009


Sorry, I forgot to send the snippet where the mail session is injected. So let's do it step by step.


As provider you could use any mail server, you have access to (including GMX, gmail or whatever). If you don't have one available, you can install your own. I use JES (Java Email Server) for that purpose. It is a real simple and easy to use server (only 2 config files!), which is quite perfect for testing issues. 


Assuming you have JES running, your mail-service.xml could look like this:
<?xml version="1.0" encoding="UTF-8"?>
  | 
  | <server>
  |   <mbean code="org.jboss.mail.MailService"
  |          name="jboss:service=Mail">
  |          
  |     <attribute name="JNDIName">mail/MailSession</attribute>
  | 
  |     <!-- mail server login data, not required for JES -->
  |     <attribute name="User">homer</attribute>
  |     <attribute name="Password">simpson</attribute>
  | 
  |     <attribute name="Configuration">
  |       <configuration>
  |         <property name="mail.store.protocol" value="pop3"/>
  |         <property name="mail.transport.protocol" value="smtp"/>
  | 
  |         <!-- who receives the mail, if no recipient is specified  -->
  |         <property name="mail.user" value="nobody"/>
  | 
  |         <!-- Change to the mail server  -->
  |         <property name="mail.pop3.host" value="localhost"/>
  | 
  |         <!-- Change to the SMTP gateway server -->
  |         <property name="mail.smtp.host" value="localhost"/>
  |         
  |         <!-- The mail server port -->
  |         <property name="mail.smtp.port" value="25"/>
  |         
  |         <!-- who is the sender of the mail, if none is specified -->
  |         <property name="mail.from" value="mailmaster at asdf.de"/>
  | 
  |         <!-- Enable debugging output from the javamail classes -->
  |         <property name="mail.debug" value="false"/>
  |       </configuration>
  |       
  |     </attribute>
  |     <depends>jboss:service=Naming</depends>
  |   </mbean>
  |   
  | </server>
At least, this config works for me :-)


Back in your application, you can use the earlier code snippet in an EJB to send a mail. There is no need to look up the mail session or something, this is done by JBoss via Dependency Injection. Here is a simple example:
package ae;
  | 
  | import java.net.InetAddress;
  | import java.util.Date;
  | 
  | import javax.annotation.Resource;
  | import javax.ejb.ActivationConfigProperty;
  | import javax.ejb.MessageDriven;
  | import javax.ejb.MessageDrivenContext;
  | import javax.jms.JMSException;
  | import javax.jms.Message;
  | import javax.jms.MessageListener;
  | import javax.jms.TextMessage;
  | import javax.mail.Address;
  | import javax.mail.Session;
  | import javax.mail.Transport;
  | import javax.mail.internet.InternetAddress;
  | import javax.mail.internet.MimeMessage;
  | 
  | import org.apache.log4j.Logger;
  | 
  | @MessageDriven(activationConfig = {
  | 		@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
  | 		@ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/q2m") })
  | public class Queue2MailBean implements MessageListener {
  | 
  | 	private static final Logger log = Logger.getLogger(Queue2MailBean.class);
  | 
  | 	@Resource(mappedName = "mail/MailSession")
  | 	private Session session;
  | 
  | 	public void onMessage(Message inMessage) {
  | 
  | 		try {
  | 
  | 			// text message
  | 			if (inMessage instanceof TextMessage) {
  | 				log.info("processing incomming TextMessage");
  | 
  | 				TextMessage textMsg = (TextMessage) inMessage;
  | 				int number = textMsg.getIntProperty("asdf");
  | 				sendMail("andre at asdf.de", "Received number " + number);
  | 			}
  | 
  | 			// other message types are currently not supported!
  | 			else {
  | 				log.warn("Message of wrong type: " + inMessage.getClass().getName());
  | 			}
  | 
  | 		} catch (JMSException e) {
  | 			log.error("Q2M: Error while receiving JMS message!", e);
  | 		}
  | 	}
  | 
  | 	private void sendMail(String recipient, String text) {
  | 		try {
  | 			Address[] to = InternetAddress.parse(recipient, false);
  | 
  | 			// create message
  | 			javax.mail.Message message = new MimeMessage(session);
  | 			message.setFrom();
  | 			message.setRecipients(javax.mail.Message.RecipientType.TO, to);
  | 			message.setSubject(Queue2MailBean.class.getSimpleName());
  | 			message.setSentDate(new Date());
  | 			message.setText(text);
  | 
  | 			// Send message
  | 			Transport.send(message);
  | 		} catch (Exception e) {
  | 			log.error(e, e);
  | 		}
  | 	}
  | }
My Bean reads TextMessages from a Queue named "q2m" (must be deployed manually or via a "-service.xml" contained in the ejb-jar. Within the text messages, there is an int property stored under key "asdf", which is forwarded to my JES mail address. The sendMail() method uses the MailSession injected by JBoss (see @Resource) to send the mail using the default FROM attribute.


If you don't like to use Dependency Injection, you can lookup the mail session yourself:
session = (Session) new InitialContext().lookup("Mail");
Please note, that this does not work outside of the server VM, because the mail session is bound to the "java" namespace, see serverlog: 2009-01-22 14:21:38,187 INFO  [org.jboss.mail.MailService] (main) Mail Service bound to java:/Mail.


Are there any questions left?

cheers,
Andre


View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4204006#4204006

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4204006



More information about the jboss-user mailing list