jackyrong

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

最近一直在学习台湾林信良的<<spring 技术手册》,写的十分不错,入门的好书,今逐渐将其心得和重要理论笔记之。

spring对mail的支持是org.springframework.mail.MailSender接口来定义的,其中的org.springframework.mail.javamail.JavamailSenderImpl这个实现类提供了对javamail的支持,下面简单介绍其用法,首先看下不用注入的方法,再看用注入的方法。

1、发送简单邮件的例子
     首先,我们要需要springframwork 1.2.8中lib目录下的j2ee目录的activation.jar和mail.jar文件,将它们放到工程的lib目录下去。
  发送一个简单邮件的例子如下
  import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.SimpleMailMessage;

public class SimpleMailDemo {
    public static void main(String[] args) throws Exception {
        JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
        // 设定 Mail Server
        senderImpl.setHost("mail.xxxxx");
        
      //SMTP验证时,需要用户名和密码
        senderImpl.setUsername("abc");
        senderImpl.setPassword("defdf");
       
        // 建立邮件讯息
        SimpleMailMessage mailMessage = new SimpleMailMessage();
       
        // 设定收件人、寄件人、主题与内文
        mailMessage.setTo(hello@abc.com);
        mailMessage.setFrom(der@def.com);
        mailMessage.setSubject("Test");
        mailMessage.setText("This is a test!!!");
       
        // 传送邮件
        senderImpl.send(mailMessage);
       
        System.out.println("邮件传送OK..");
    }
}

2、发送HTML格式的邮件
    要发送HTML格式的话,要用org.springframework.mail.javamail.MimeMessageHelper来建立HTML邮件
  import org.springframework.mail.javamail.JavaMailSenderImpl;
import javax.mail.internet.MimeMessage;
import org.springframework.mail.javamail.MimeMessageHelper;

public class HTMLMailDemo {
    public static void main(String[] args) throws Exception {
        JavaMailSenderImpl senderImpl =
            new JavaMailSenderImpl();
        
      // 设定 Mail Server
        senderImpl.setHost("mail.xxxxx");
        
      //SMTP验证时,需要用户名和密码
        senderImpl.setUsername("abc");
        senderImpl.setPassword("defdf");
        
        // 建立邮件讯息
        MimeMessage mailMessage =
            senderImpl.createMimeMessage();
        MimeMessageHelper messageHelper =
            new MimeMessageHelper(mailMessage);
               
        // 设定收件人、寄件人、主题与内文
        messageHelper.setTo("xxx@your_mail_server.com");
        messageHelper.setFrom("xxx@your_mail_server.com");
        messageHelper.setSubject("Test");
        messageHelper.setText(
                "<html><head></head><body><h1>Hello! Spring!"
                + "</h1></body></html>", true);
       
        // 传送邮件
        senderImpl.send(mailMessage);
       
        System.out.println("邮件传送OK...");
    }
}
 
3、发送带附件的邮件
    这时要用MimeMessageHelper的addAttachement()方法
 import java.io.File;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import javax.mail.internet.MimeMessage;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.MimeMessageHelper;

public class AttachedFileDemo {
    public static void main(String[] args) throws Exception {
        JavaMailSenderImpl senderImpl =
                      new JavaMailSenderImpl();
        
                //SMTP验证时,需要用户名和密码
        senderImpl.setUsername("abc");
        senderImpl.setPassword("defdf");

                MimeMessage mailMessage =     senderImpl.createMimeMessage();
        MimeMessageHelper messageHelper =  new MimeMessageHelper(mailMessage, true);
                
           messageHelper.setTo("def@erer.net");
        messageHelper.setFrom("bfg@tom.com");
        messageHelper.setSubject("Test");
        messageHelper.setText(
            "<html><head></head><body><h1>Hello! Spring!"
            + "</h1></body></html>", true);
       
        FileSystemResource file =
                new FileSystemResource(
                           new File("d:/test.rar"));
        messageHelper.addAttachment("test.rar", file);
        
               senderImpl.send(mailMessage);
       
        System.out.println("OK");
    }
}


4、用注入的方式来配置邮件,和之前的差不多拉
  <?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.xxx.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) {
      // TODO Auto-generated catch block
      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 {

  /**
   * @param args
   */
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    ApplicationContext context = new ClassPathXmlApplicationContext(
        "test/mail/mail.xml");

    TestSenderMail sender = (TestSenderMail) context
        .getBean("testMailSender");
    sender.sendMail();
  }
  }

 

posted on 2006-08-17 20:17  jackyrong的世界  阅读(8545)  评论(2编辑  收藏  举报