Denis's Java Library

          The only documentation is the code itself

          使用Spring郵件發送Email

          Spring提供了一個發送電子郵件的高級抽象層,它向用戶屏蔽了底層郵件系統的一些細節,同時負責低層次的代表客戶端的 資源處理。Spring郵件抽象層的主要包為org.springframework.mail。它包括了發送電子郵件的主要接口MailSender和 封裝了簡單郵件的屬性如from, to,cc, subject, text的值對象叫做SimpleMailMessage。
          首先:我們定義一個發送郵件的接口:IMailManager.java
          /*
          * IMailManager.java
          * Copyright 2005, All rights reserved.
          */
          package test.mail.manager;

          import test.common.logic.IManager;
          import test.model.Order;

          /**
          * Note:this interface mainly deal with the sendOrder
          */
          public interface IMailManager extends IManager{

          void sendOrder(Order order);
          }

          然后實現這個接口:MailManager.java
          /*
          * MailManager.java
          * Copyright 2005, All rights reserved.
          */
          package test.mail.manager;

          import org.springframework.mail.MailException;
          import org.springframework.mail.MailSender;
          import org.springframework.mail.SimpleMailMessage;

          import test.common.logic.impl.Manager;
          import test.model.Order;

          /**
          * Note:the implements of IMailManager
          */
          public class MailManager extends Manager implements IMailManager {

          private MailSender mailSender;
          private SimpleMailMessage message;

          public void sendOrder(Order order) {
          SimpleMailMessage mailMessage = new SimpleMailMessage(this.message);
          mailMessage.setTo(order.getUser().getEmail());
          mailMessage.setText("Dear"
          + order.getUser().getFirstName()
          + order.getUser().getLastName()
          + ", thank you for placing order. Your order code is "
          + order.getCode());
          try{
          mailSender.send(mailMessage);
          }catch(MailException ex) {
          System.err.println(ex.getMessage());
          }

          }

          /**
          * @param mailSender The mailSender to set.
          */
          public void setMailSender(MailSender mailSender) {
          this.mailSender = mailSender;
          }
          /**
          * @param message The message to set.
          */
          public void setMessage(SimpleMailMessage message) {
          this.message = message;
          }
          }

          然后我們在Action 里面調用: SendMailAction.java
          /*
          * SendMail.java
          * Copyright 2005, All rights reserved.
          */
          package test.mail.action;

          import test.common.action.BaseAction;
          import test.mail.manager.IMailManager;
          import test.order.dao.IOrderDao;
          import test.model.Order;


          /**
          * Note: SendMailAction
          */
          public class SendMailAction extends BaseAction {
          private IMailManager mailManager;
          private IOrderDao orderDao;
          private long orderId;

          public String execute() throws Exception {
          Order order = orderDao.getOrder(orderId);
          mailManager.sendOrder(order);
          return SUCCESS;
          }


          /**
          * @return Returns the mailManager.
          */
          public IMailManager getMailManager() {
          return mailManager;
          }
          /**
          * @param mailManager The mailManager to set.
          */
          public void setMailManager(IMailManager mailManager) {
          this.mailManager = mailManager;
          }

          /**
          * @return Returns the orderDao.
          */
          public IOrderDao getOrderDao() {
          return orderDao;
          }
          /**
          * @param orderDao The orderDao to set.
          */
          public void setOrderDao(IOrderDao orderDao) {
          this.orderDao = orderDao;
          }
          /**
          * @return Returns the orderId.
          */
          public long getOrderId() {
          return orderId;
          }
          /**
          * @param orderId The orderId to set.
          */
          public void setOrderId(long orderId) {
          this.orderId = orderId;
          }
          }

          最后的就是配置了.在ApplicationContext.xml文件里加上如下的內容:
          <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
          <property name="host"><value>smtp服務器</value></property>
          <property name="username"><value>用戶名</value></property>
          <property name="password"><value>密碼</value></property>
          /**如果服務器要求驗證,加上此**/
          <property name="javaMailProperties">
          <props>
          <prop key="mail.smtp.auth">true</prop>
          <prop key="mail.smtp.timeout">25000</prop>
          </props>
          </property>
          </bean>

          <bean id="mailMessage"
          class="org.springframework.mail.SimpleMailMessage">
          <property name="from">
          <value>你的電子郵件地址</value>
          </property>
          <property name="subject">
          <value>郵件標題</value>
          </property>
          </bean>


          <bean id="mailManager" class=" test.mail.manager.MailManager" >
          <property name="mailSender">
          <ref bean="mailSender" />
          </property>
          <property name="message">
          <ref bean="mailMessage" />
          </property>
          </bean>
          在對應的action配置文件中加入:
          <bean id="SendMailAction"
          class=" test.mail.action.SendMailAction" singleton="false" >
          <property name="mailManager">
          <ref bean="mailManager" />
          </property>
          <property name="orderDao">
          <ref bean="orderDao"/>
          </property>
          </bean>

          在xwork配置文件中:
          <action name="sendMailBG" class="SendMailAction">
          <interceptor-ref name="defaultStack" />
          <result name="success" type="freemarker">success.ftl</result>
          <result name="error" type="freemarker">error.ftl</result>
          </action>

          posted on 2005-11-04 16:13 DenisLing 閱讀(525) 評論(0)  編輯  收藏 所屬分類: Spring


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 手机| 英山县| 临海市| 长治县| 新兴县| 壤塘县| 周至县| 乌鲁木齐县| 德江县| 东兰县| 武冈市| 故城县| 红河县| 全椒县| 娱乐| 崇阳县| 福海县| 铜山县| 盐源县| 佛教| 辰溪县| 马边| 霍州市| 汝阳县| 保亭| 邢台县| 木兰县| 怀集县| 扎鲁特旗| 柳州市| 黄平县| 万山特区| 西吉县| 达孜县| 丰顺县| 宁德市| 图们市| 莲花县| 垦利县| 韶山市| 宁明县|