Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1970000
  • 博文数量: 606
  • 博客积分: 9991
  • 博客等级: 中将
  • 技术积分: 5725
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-17 19:07
文章分类

全部博文(606)

文章存档

2011年(10)

2010年(67)

2009年(155)

2008年(386)

分类:

2008-09-03 12:45:12

做了个spring发送纯文本文件以及发送带附件的邮件的例子,共享之。
      1. SpringMail类
     

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessagePreparator;

import javax.mail.internet.MimeMessage;
import javax.mail.MessagingException;
import javax.mail.Message;
import javax.mail.internet.InternetAddress;
import javax.activation.FileDataSource;
import javax.activation.DataHandler;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeBodyPart;
import javax.mail.Multipart;

import java.util.List;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;

/**
 * spring的邮件发送例子
 * 
@author Amigo Xie(xiexingxing1121@126.com)
 * 
@since 2007/04/28 11:30
 
*/

public class SpringMail {

    
public static void main(String[] args) throws Exception {
        ApplicationContext ctx 
= new FileSystemXmlApplicationContext(
                
"src/applicationContext.xml");
        JavaMailSender sender 
= (JavaMailSender) ctx.getBean("mailSender");
        SpringMail springMail 
= new SpringMail();
        
        
//测试发送只有文本信息的简单测试
        springMail.sendTextMail(sender);
        
        
//测试发送带附件的邮件
        springMail.sendMimeMessage(sender);
    }

    
    
/**
     * 测试发送只有文本信息的简单测试
     * 
@param sender 邮件发送器
     * 
@throws Exception
     
*/

    
private void sendTextMail(JavaMailSender sender) throws Exception {
        SimpleMailMessage mail 
= new SimpleMailMessage();
        mail.setTo(
"xiexingxing1121@126.com");
        mail.setFrom(
"xiexingxing1121@126.com");
        mail.setSubject(
"test by amigo");
        mail.setText(
"spring Mail的简单测试");
        sender.send(mail);
        
        System.out.println(
"成功发送文本文件!");
    }

    
    
/**
     * 发送带附件的邮件
     * 
@param sender 邮件发送器 
     * 
@throws Exception
     
*/

    
private void sendMimeMessage(final JavaMailSender sender) throws Exception {
        
//附件文件集合
        final List files = new ArrayList();
        MimeMessagePreparator mimeMail 
= new MimeMessagePreparator() {
            
public void prepare(MimeMessage mimeMessage) throws MessagingException {
                mimeMessage.setRecipient(Message.RecipientType.TO, 
                        
new InternetAddress("xiexingxing1121@126.com"));
                mimeMessage.setFrom(
new InternetAddress("xiexingxing1121@126.com"));
                mimeMessage.setSubject(
"Spring发送带附件的邮件""gb2312"); 
                
                Multipart mp 
= new MimeMultipart();
                
                
//向Multipart添加正文
                MimeBodyPart content = new MimeBodyPart();
                content.setText(
"内含spring邮件发送的例子,请查收!");
                
                
//向MimeMessage添加(Multipart代表正文)
                mp.addBodyPart(content);
                files.add(
"src/SpringMail.java");
                files.add(
"src/applicationContext.xml");
                
                
//向Multipart添加附件
                Iterator it = files.iterator();
                
while(it.hasNext()) {
                    MimeBodyPart attachFile 
= new MimeBodyPart();
                    String filename 
= it.next().toString();
                    FileDataSource fds 
= new FileDataSource(filename);
                    attachFile.setDataHandler(
new DataHandler(fds));
                    attachFile.setFileName(fds.getName());
                    mp.addBodyPart(attachFile);
                }

                
                files.clear();
                
                
//向Multipart添加MimeMessage
                mimeMessage.setContent(mp);
                mimeMessage.setSentDate(
new Date());
            }

        }
;

        
//发送带附件的邮件
        sender.send(mimeMail);
        
        System.out.println(
"成功发送带附件邮件!");
    }

}


      2. spring的配置文件
     
xml version="1.0" encoding="UTF-8"?>
DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "">

<beans>
    
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        
<property name="host">
            
<value>smtp.126.comvalue>
        
property>
        
<property name="javaMailProperties">
            
<props>
                
<prop key="mail.smtp.auth">trueprop>
                
<prop key="mail.smtp.timeout">25000prop>
            
props>
        
property>
        
<property name="username">
            
<value>xiexingxing1121value>
        
property>
        
<property name="password">
            
<value>value>
        
property>
    
bean>
beans>

    刚才发现一bug,当附件名为中文时,会出现中文乱码问题,对sendMimeMessage方法进行了部分修改,如下:
   
               sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
                files.add(
"src/SpringMail.java");
                files.add(
"src/applicationContext.xml");
                files.add(
"src/谢星星.xml");
                
                
//向Multipart添加附件
                Iterator it = files.iterator();
             
while (it.hasNext()) {
                    MimeBodyPart attachFile 
= new MimeBodyPart();
                    String filename 
= it.next().toString();
                    FileDataSource fds 
= new FileDataSource(filename);
                    attachFile.setDataHandler(
new DataHandler(fds));
                    attachFile.setFileName(
"=?GBK?B?"+enc.encode(fds.getName().getBytes())+"?=");
                    mp.addBodyPart(attachFile);
                }
 
阅读(945) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~