User: tobias  
  Date: 01/01/22 10:28:59

  Added:       documentation HowTo-JavaMail.html
  Log:
  initial checking. Thanks to Simone Bordet and Michel de Groot
  
  Revision  Changes    Path
  1.1                  newsite/documentation/HowTo-JavaMail.html
  
  Index: HowTo-JavaMail.html
  ===================================================================
  <h1>HowTo: Using JavaMail in JBoss</h1>
  
  <h2>Introduction</h2>
  JBoss has a built-in implementation of the JavaMail API. You can use this service 
from inside and outside EJBs. We describe here how to use the service.
  
  <h2>Original contributor</h2>
  Simone Bordet - [EMAIL PROTECTED]
  
  <h2>Doc writer</h2>
  Michel de Groot - [EMAIL PROTECTED]
  
  <h2>Applicable to</h2>
  JBoss 2.1 since 21st January 2001
  
  <h2>Installation & Configuration</h2>
  <ol>          
  <li> Edit conf/&lt;yourconfig&gt;/jboss.jcml and find Mail Service MBean (almost on 
the bottom).<p>
  
  a) Replace the User and Password attributes values with the user name and
  password used to connect to your mail server. You can find these values in your
  mail program. The mail service will use this account to send mails, so be sure that 
this mail
  account works properly (test it with your mail program for example).<p>
  
  b) Replace the ConfigurationFile attribute value with the file containing the mail 
settings. Default is "mail.properties", which is also in the conf/&lt;yourconfig&gt; 
directory. This file will be edited in step 2.<p>
  
  c) Replace the JNDIName attribute value with the JNDI name for your mail session. 
The default is "Mail". This JNDI name will be used in jboss.xml to identify the 
resource. This is explained in more detail in step 4.<p>
  </li>
  
  <li> Edit the mail properties file you identified in step 1b. By default, this is 
conf/&lt;yourconfig&gt;/mail.properties.<br>
  Edit the following lines:<br>
  <pre>
  mail.user = sa005697  // the user to connect with; same as in step 1a
  mail.pop3.host = pop3.wolmail.nl      // the pop host to store the mail on
  mail.smtp.host = smtp.wolmail.nl      // the smtp host to send the mail to 
  mail.from = [EMAIL PROTECTED]    // the 'from' field that is filled in by 
default in e-mails
  </pre>
  You can find most value in your mail program. You might want to inspect the JavaMail 
specification for more details.<p>
  
  The last line, mail.debug, should be set to 'true' for now. This will provide you 
with verbose debugging information. Once you have everything running correctly, you 
can set it to false.
  </li>
  
  <li> Edit the ejb-jar.xml of the EJB that uses the mail service.
  In your EJB, specify a &lt;resource-ref&gt; like this:
  <pre>
  &lt;ejb-jar&gt;
        &lt;enterprise-beans&gt;
                &lt;session&gt; 
                        &lt;ejb-name&gt;Mailer&lt;/ejb-name&gt; 
                        &lt;home&gt;some.package.MailerHome&lt;/home&gt; 
                        &lt;remote&gt;some.package.Mailer&lt;/remote&gt; 
                        &lt;ejb-class&gt;some.package.MailerEJB&lt;/ejb-class&gt; 
                        &lt;session-type&gt;Stateless&lt;/session-type&gt; 
                        &lt;transaction-type&gt;Container&lt;/transaction-type&gt; 
  
                        &lt;resource-ref&gt;
                                &lt;res-ref-name&gt;mail/MyMail&lt;/res-ref-name&gt; 
                                &lt;res-type&gt;javax.mail.Session&lt;/res-type&gt; 
                                &lt;res-auth&gt;Container&lt;/res-auth&gt; 
                        &lt;/resource-ref&gt; 
  
                &lt;/session&gt;
        &lt;/enterprise-beans&gt; 
  &lt;/ejb-jar&gt;  
  </pre>
  This will tell the EJB container that the EJB uses a javax.mail.Session resource 
named mail/MyMail and that authorization is container managed.<br>
  
  You can change the name if you like, but be sure to use the same name in step 6, in 
the code example.
  </li>
  
  <li> Edit the jboss.xml of the EJB that uses the mail service. If you don't have 
this file, create it and
  place it in the same directory as the ejb-jar.xml of the EJB. This file is JBoss 
specific and tells
  JBoss how to map the mail resource to the mail service provider in JBoss.<p>
  
  In this file, specify a &lt;resource-manager&gt; like this:
  <pre>
  &lt;jboss&gt;
        &lt;resource-managers&gt;
                &lt;resource-manager&gt;
                        &lt;res-name&gt;mail/MyMail&lt;/res-name&gt; 
                        &lt;res-jndi-name&gt;Mail&lt;/res-jndi-name&gt; 
                &lt;/resource-manager&gt; 
        &lt;/resource-managers&gt; 
  &lt;/jboss&gt;
  </pre>
  
  The name that you specify here is the name that you specified in step 3. The JNDI 
name that you specify here is the name that you specified in step 1c. 
  </li>
  
  <li> Edit the bin/run.bat file of your JBoss installation.
  Include ../lib/ext/mail.jar and ../lib/ext/activation.jar in the classpath 
explicitly. This assumes that you start JBoss from the bin directory. If not, you 
should modify the paths to the jars accordingly.<p>
  
  TO BE IMPROVED: This step should not be required; both mail.jar and activation.jar 
are correctly found during the ClassPathExtension scan, but somehow their classes 
cannot be found later. Maybe something missing in the manifest.mf files?
  </li>
  
  <li> Code example<br>
  This code example assumes that you are working from inside a JBoss container. For 
example, this is the case if the code is placed in a JBoss managed SessionBean.<p>
  
  TO BE IMPROVED: This code example does not use PortableRemoteObject, because I could 
not locate it anywhere in the JBoss jars. The code will work without it on JBoss. It 
should be used however to make the code more portable. I'm also not sure what happens 
in a distributed JBoss installation.
  
  <pre>
  import java.util.Date;
  import javax.ejb.SessionBean;
  import javax.naming.InitialContext;
  import javax.mail.Session;
  import javax.mail.internet.MimeMessage;
  import javax.mail.internet.InternetAddress;
  import javax.mail.Transport;
  import javax.mail.Address;
  import javax.mail.Message;
  //import javax.rmi.PortableRemoteObject;
  
  public class SomeEJB implements SessionBean {
        public void ejbCreate() {}
        
        public void ejbPostCreate() {}
  
        public void sendMails() throws java.rmi.RemoteException {
                Session session = null;
                try {
                        session = (Session)new 
InitialContext().lookup("java:comp/env/mail/MyMail");
                        //session = (Session)PortableRemoteObject.narrow(
                        //      new 
InitialContext().lookup("java:comp/env/mail/MyMail"), Session.class);
                } catch (javax.naming.NamingException e) {
                        e.printStackTrace();
                }
                        
                try {
                        MimeMessage m = new MimeMessage(session);
                        m.setFrom();
                        Address[] to = new InternetAddress[] {new
                        
InternetAddress("&lt;your_email_adres@&lt;your_provider&gt;.&lt;your_extension&gt;");
                        m.setRecipients(Message.RecipientType.TO, to);
                        m.setSubject("JavaMail Test");
                        m.setSentDate(new Date());
                        m.setContent("Test from inside EJB Using JBoss", "text/plain");
                        Transport.send(m);
                } catch (javax.mail.MessagingException e) {
                        e.printStackTrace();
                }               
        }
  
        public void ejbActivate() {}
        public void ejbPassivate() {}
        public void ejbRemove() {}
        public void setSessionContext(javax.ejb.SessionContext ec) {}
  }
  </pre>
  </li>
  
  <li> Using the JavaMail service with mail servers that require POP authentication 
before SMTP<br>
  You can do this by using:
  <pre>
        import javax.mail.Store;
  
        Store s = session.getStore();
        s.connect(); // POP authentication
        Transport.send(m);
  </pre>
  </li>
  
  
  
  
  

Reply via email to