现在的位置: 首页 > 综合 > 正文

\t\t用spring做一个javaMail功能的例子

2014年07月16日 ⁄ 综合 ⁄ 共 4967字 ⁄ 字号 评论关闭

前言:项目中要做一个发送邮件的功能,在网上搜了一些代码,说的都不是很完善。自己就整理了一个可以运行的项目。需要注意的地方是:如果发送人的邮箱是qq邮箱,那么需要在 设置->账户里面把√打上。spring里面的

<property name="host">
    <value>smtp.126.com</value>
   </property>
要配置成smtp.qq.com

  用spring做一个javaMail功能的例子 - xiaojunwei1987 - http://hi.baidu.com/

126邮箱同理。

步骤如下:

用spring做一个javaMail功能。

1、先建一个项目。java项目和web项目均可。

2、需要引入的jar包有:mail.jar,activation.jar(我用的是maven搭建的项目,在pom.xml中依赖了那两个jar包)

3、在MyEclipse的项目里面加入spring的jar包

4、在项目中的applicationContext-init.xml(名字可以任意改)文件中加入如下代码:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="mailSender" <property name="host">
    <value>smtp.126.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>xiaojunwei_1987</value>
   </property>
   <property name="password">

<!--这里的是配置自己邮箱的密码,这里我输入的是假的,以*代替了,呵呵要不你们就知道我的邮箱密码了-->
    <value>********</value>
   </property>
</bean>
</beans>

java 代码:我自己加的注释,不一定准确哦。

package com.techcenter.mail;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessagePreparator;
public class SpringMail {
public static void main(String[] args) throws Exception {
//读取spring配置文件
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-init.xml");
        //强制返回spring邮件发送器
        JavaMailSender sender = (JavaMailSender) ctx.getBean("mailSender");
        //创建对象并初始化
        SpringMail springMail = new SpringMail();          
    //测试发送只有文本信息的简单测试
    springMail.sendTextMail(sender);      
    //测试发送带附件的邮件
    springMail.sendMimeMessage(sender);       
    }  
    private void sendTextMail(JavaMailSender sender) throws Exception {
    //声明spring的简单邮件消息
        SimpleMailMessage mail = new SimpleMailMessage();
        mail.setTo("237888386@qq.com");      
        mail.setFrom("xiaojunwei_1987@126.com");
        mail.setSubject("test by amigo");
        mail.setText("spring Mail的简单测试");
        sender.send(mail);      
        System.out.println("成功发送文本文件!");       
    }  
    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(""));
                mimeMessage.setRecipient(Message.RecipientType.TO,new InternetAddress("237888386@qq.com"));
                mimeMessage.setFrom(new InternetAddress("xiaojunwei_1987@126.com"));
                mimeMessage.setSubject("Spring发送带附件的邮件", "gb2312");       
                //多部件的,可以看做一个邮件容器,包含正文、附件等
                Multipart mp = new MimeMultipart();              
                //向Multipart添加正文
                MimeBodyPart content = new MimeBodyPart();
                content.setText("内含spring邮件发送的例子,请查收!");              
                //向MimeMessage添加(Multipart代表正文)
                mp.addBodyPart(content);
                //向Multipart添加附件,可以添加多个附件;
                String path = System.getProperty("user.dir");
                System.out.println(path);

                // 这里是附件的路径,自己测试的时候可能要改的哦!
                files.add("e://hehe.txt");
                files.add(path+"\\src\\test\\java\\com\\techcenter\\mail\\SpringMail.java");   
                Iterator it = files.iterator();
                //遍历List,把附件放到邮件容器里面
                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("成功发送带附件邮件!");
    }
}

到这里程序已经结束了,已经实现了发送邮件的功能了。

抱歉!评论已关闭.