北大青鸟(上海云登)授权培训中心
软件开发 | 网络技术实训基地北大青鸟江老师
咨询电话:021-61113200、13122651497
您现在的位置:上海北大青鸟 > 学员天地 > 学习技巧 >

如何利用spring来发邮件

发布日期:2013-07-25 14:12


先来看看发送Email吧, 在Spring中发送Email是很简单的,使用Spring提高的MailSender和MailMessage就可以了,配置代码如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "spring-beans.dtd" >
    <beans>
    <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>
      </props>
    </property>
    <property name="username">
    <value>你的Email地址</value>
    </property>
    <property name="password">
    <value>你的Email密码</value>
    </property>

    </bean>
    <!-- 简单的message -->

    <bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage">
    <property name="to">
    <value>收件人地址</value>
    </property>
    <property name="from">
    <value>你的地址</value>
    </property>
    <property name="subject"> <!-- Email 标题 -->
    <value>A Spring Mail sender</value>
    </property>
    </bean>
    <!--   测试发送的类 -->

    <bean id="testMailSender" class="test.mail.TestSenderMail">
    <property name="mailMessage">
    <ref bean="mailMessage"/>
    </property>
    <property name="mailSender">
    <ref bean="mailSender"/>
    </property>
    </bean>

    </beans>
    上面的配置好以后就可以直接发送了, 看看TestSenderMail.java的代码:
    package test.mail;
    import org.springframework.mail.MailException;
    import org.springframework.mail.MailSender;
    import org.springframework.mail.SimpleMailMessage;
    public class TestSenderMail {
       private MailSender mailSender;
       private SimpleMailMessage mailMessage;
       public TestSenderMail() {        }
       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(" 测试Spring 发送Email.");
         try {
           mailSender.send(message);
        } catch (MailException e) {
            System.out.println("O . 发送Email失败了....");
            e.printStackTrace();
         }
       }
     }
    很简单吧. 下面是测试类: TestApp.java
    package test.mail;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public class TestApp {
      public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(         "test/mail/mail.xml");
        TestSenderMail sender = (TestSenderMail) context         .getBean("testMailSender");
         sender.sendMail();
       }
       //抛出如下异常,是瑞星监控的问题,关闭就可以了
       /**    * DEBUG SMTP: QUIT failed with 250 O . 发送Email失败了....
       * org.springframework.mail.MailSendException: Could not send mails: 354
        *     * com.sun.mail.smtp.SMTPSendFailedException: 354    *     * at    * com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1388)    * at com.sun.mail.smtp.SMTPTransport.finishData(SMTPTransport.java:1215) at    * com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:586)    */
    }
 

    呵呵, 现在就可以发送Email了.
    最后提醒一下,记得要将:activation.jar 和 mail.jar 两个包导入到工程