View Javadoc

    1   /***
    2    * @(#)MailServiceImpl.java
    3    *
    4    * JFoxSOAF, Service-Oriented Application Framework
    5    *
    6    * Copyright(c) JFoxSOAF Team
    7    *
    8    * Licensed under the GNU LGPL, Version 2.1 (the "License");
    9    * you may not use this file except in compliance with the License.
    10   * You may obtain a copy of the License at
    11   *
    12   * http://www.gnu.org/copyleft/lesser.html
    13   *
    14   * Unless required by applicable law or agreed to in writing, software
    15   * distributed under the License is distributed on an "AS IS" BASIS,
    16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    17   * See the License for the specific language governing permissions and
    18   * limitations under the License.
    19   *
    20   * For more information, please visit:
    21   * http://www.jfox.cn/confluence/display/JFoxSOAF/Home
    22   * http://www.huihoo.org/jfox/jfoxsoaf
    23   */
    24
    25  package org.huihoo.jfox.soaf.services.mail;
    26
    27  import javax.mail.MessagingException;
    28  import javax.mail.Session;
    29  import javax.mail.Transport;
    30
    31  import org.apache.commons.mail.Email;
    32  import org.apache.commons.mail.EmailException;
    33  import org.huihoo.jfox.soaf.exception.MailException;
    34  import org.huihoo.jfox.soaf.exception.ServiceLocatorException;
    35  import org.huihoo.jfox.soaf.services.ejb.ServiceLocator;
    36
    37  /***
    38   * <p>
    39   * Mail service implementation.
    40   * </p>
    41   *
    42   * @author <a href="mailto:founder_chen@yahoo.com.cn">Peter Cheng </a>
    43   * @version $Revision: 1.1 $ $Date: 2005/05/22 06:50:11 $
    44   * @version Revision: 1.0
    45   */
    46
    47  public class MailServiceImpl implements MailService {
    48
    49      private ServiceLocator serviceLocator;
    50
    51      public MailServiceImpl(ServiceLocator serviceLocator) {
    52          this.serviceLocator = serviceLocator;
    53      }
    54
    55      /***
    56       * @see org.huihoo.jfox.soaf.services.mail.MailService#sendMail(org.apache.commons.mail.Email)
    57       */
    58      public void sendMail(Email email) throws MailException {
    59          try {
    60              email.send();
    61          } catch (EmailException e) {
    62              throw new MailException("Send mail failed " + e);
    63          }
    64      }
    65
    66      /***
    67       * @see org.huihoo.jfox.soaf.services.mail.MailService#sendMailWithSession(org.apache.commons.mail.Email)
    68       */
    69      public void sendMailWithSession(Email email, String mailSessionName)
    70              throws MailException {
    71          try {
    72              Session session = serviceLocator.getMailSession(mailSessionName);
    73              email.setMailSession(session);
    74              email.send();
    75          } catch (ServiceLocatorException e) {
    76              throw new MailException("Lookup mail session failed ", e);
    77          } catch (EmailException e) {
    78              throw new MailException("Send mail failed ", e);
    79          }
    80      }
    81
    82      /***
    83       * @see org.huihoo.jfox.soaf.services.mail.MailService#sendMail(org.huihoo.jfox.soaf.services.mail.MailMessage)
    84       */
    85      public void sendMail(MailMessage mailMessage) throws MailException {
    86          try {
    87              Transport.send(mailMessage.getMimeMessage());
    88          } catch (MessagingException e) {
    89              throw new MailException(e);
    90          }
    91      }
    92  }