Spring发送邮件示例

 

1.制作一个邮件发送类(SendMail.java)代码如下:

package cn.ssh.struts.util;

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

public class SendMail {
private MailSender mailSender;
private SimpleMailMessage mailMessage;

public SendMail() {
   
}

public SimpleMailMessage getMailMessage() {
    return mailMessage;
}
public void setMailMessage(SimpleMailMessage mailMessage) {
    this.mailMessage = mailMessage;
}
public MailSender getMailSender() {
    return mailSender;
}
public void setMailSender(MailSender mailSender) {
    this.mailSender = mailSender;
}

public void sendMail() {
    SimpleMailMessage message = new SimpleMailMessage(mailMessage);
    //设置email内容,
    message.setText("Hello 我是杨占辉.这是一个用Spring发送的测试邮件.");
   
    try {
      mailSender.send(message);
    } catch (MailException e) {
      // TODO Auto-generated catch block
      System.out.println("O . 发送Email失败了.");
      e.printStackTrace();
    }
}
}

2.在applicationContext.xml中进行相应的如下配置:

<!-- ******************************** 以下为邮件自动发送示例的配置 ********************************** -->

<!-- mailSender -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host">
        <value>smtp.163.com</value>
    </property>
   <property name="javaMailProperties">
    <props>
           <prop key="mail.smtp.auth">true</prop><!-- 如果要使用用户名和密码验证,这一步需要 -->
           <prop key="mail.smtp.timeout">25000</prop>
    </props>
    </property>
   <property name="username">
       <value>yangzhanhui</value>
   </property>
   <property name="password">
        <value>yangzhanhui</value>
   </property>
</bean>

<!-- 简单的message -->
<bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage">
   <property name="to">
    <value>yangzhanhui@neusoft.com</value>
   </property>
   <property name="from">
    <value>yangzhanhui@163.com</value>
   </property>
   <property name="subject"> <!-- Email 标题 -->
    <value>你好,朋友.</value>
   </property>
</bean>

<!-- sendMail -->
<bean id="sendMail" class="cn.ssh.struts.util.SendMail">
   <property name="mailMessage">
    <ref bean="mailMessage"/>
   </property>
   <property name="mailSender">
    <ref bean="mailSender"/>
   </property>
</bean>

3.在Action中调用邮件自动发送业务,代码如下:

public class UserAction extends Action {
/*
* Generated Methods
*/

private User user;

private UserService userService;

private SendMail sendMail;

/**
* @return the sendMail
*/
public SendMail getSendMail() {
   return sendMail;
}

/**
* @param sendMail the sendMail to set
*/
public void setSendMail(SendMail sendMail) {
   this.sendMail = sendMail;
}

public User getUser() {
   return user;
}

public void setUser(User user) {
   this.user = user;
}

public UserService getUserService() {
   return userService;
}

public void setUserService(UserService userService) {
   this.userService = userService;
}

/**
* Method execute
*
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
   UserForm userForm = (UserForm) form;
  
   user.setUsername(userForm.getUsername());
   user.setPassword(userForm.getPassword());

   if (userService.login(user)) {
   
   sendMail.sendMail();
    return mapping.findForward("success");
   
   } else {
   
    return mapping.findForward("fail");
   
   }
  

}
}

补充说明:
我在进行完以上所有工作以后,直接启动Tomcat进行测试,但是出现了下面的错误信息:

ERROR ContextLoader:211 - Context initialization failed
org.springframework.beans.factory.BeanDefinitionStoreException: Error registering bean with name 'javaMailSender' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Class that bean class [org.springframework.mail.javamail.JavaMailSenderImpl] depends on not found; nested exception is java.lang.NoClassDefFoundError: javax/mail/MessagingException
java.lang.NoClassDefFoundError: javax/mail/MessagingException

经过调查,解决方法如下:

把j2ee中的activation.jar,和mail.jar包复制到Tomcat中的common/lib目录下,重新发布就成功了.

以下转载于其它网络资源:
   搞了很久都不明白是什么回事,因为在测试时都没有问题,而且提示中的包在项目里都有.最后通过网上搜索把问题解决了,原来在使用Spring这样的框架的一些丰富功能时,都要在完整的J2EE容器下运行,因为Spring只不过提供J2EE容器功能的又一种调用方式罢了,所以把j2ee中的activation.jar,和mail.jar包复制到Tomcat中的common/lib目录下就发布成功了.

posted @ 2009-02-07 22:28  WenEric  阅读(790)  评论(0编辑  收藏  举报