使用spring发送邮件

第一步

   spring 邮件相关配置

         
	  
			           
			             smtp.qiye.163.com  
			           
			           
			             25  
			         
			         
			           
			             
			                 true  
		                 25000  
			               
			           
			           
			             xxx  
			           
			           
			            xxx  
			         
	   

     

    第二步

         编写发送邮件的工具类

         

package com.yundao.kdy.framework.util;

import java.io.File;

import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;

import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import com.yundao.common.util.ConfigUtils;


@Component
public class SendEmailService {
	@Resource(name = "mailSender")
	private JavaMailSenderImpl senderImpl;

/*//	public void send(File file, String date) {
//		send(file, date, ConfigUtils.getConfig("accept.report.email"));
//	}
*/	public void send(File file, String date) {
		MimeMessage mailMessage = senderImpl.createMimeMessage();
		try {
			// 设置utf-8或GBK编码,否则邮件会有乱码
			MimeMessageHelper messageHelper = new MimeMessageHelper(
					mailMessage, true, "utf-8");
			//收件人
			String[] recs = ConfigUtils.getConfig("accept.report.email").split(";");
			//抄送人
			String[] ccs = ConfigUtils.getConfig("cc.report.email").split(";");
			//密送人
			String[] bccs = ConfigUtils.getConfig("bcc.report.email").split(";");
			
			messageHelper.setTo(recs);
			if(ccs!=null && ccs.length>0){
				messageHelper.setCc(ccs);
			}
			if(ccs!=null && ccs.length>0){
				messageHelper.setBcc(bccs);
			}
			messageHelper.setFrom(senderImpl.getUsername());// 发送者
			messageHelper.setSubject(date+"会员统计报表");// 主题
			// 邮件内容,注意加参数true
			messageHelper.setText("

附件为 " + date + " 的会员统计报表

", true); // 附件内容 messageHelper.addAttachment(MimeUtility.encodeWord(file.getName()), file); senderImpl.send(mailMessage); } catch (Exception e) { e.printStackTrace(); } } }

   





你可能感兴趣的:(pring邮件发送,java)