关闭

java mail发送多个附件

标签: springsmtpmaven
238人阅读 评论(0) 收藏 举报
分类:
1、service层接口
package tv.bgsc.paymentplatform.mail.test;

public interface EmailService {

	/**********
	 * 单人邮件
	 * @param mailto    接收人邮件地址
	 * @param text      邮件内容
	 * @param title     邮件标题
	 * @throws Exception
	 */
	public void sendMail(String mailto,String text,String title) throws Exception;
	
	
	/**********
	 * 群发邮件
	 * @param mailto    接收人邮件地址
	 * @param text      邮件内容
	 * @param title     邮件标题
	 */
	public void sendBatcMail(String[] mailto,String text,String title);
	
	
	/**********
	 * 单人发送文件
	 * @param mailto    接收人邮件地址
	 * @param text      邮件内容
	 * @param title     邮件标题
	 * @param filePath  文件路径
	 * @throws Exception
	 */
	public void sendMailWithFile(String mailto,String text,String title,String[] filePath)throws Exception;
	
	
	/************
	 * 群发文件
	 * @param mailto    接收人邮件地址
	 * @param text      邮件内容
	 * @param title     邮件标题
	 * @param filePath  文件路径
	 * @throws Exception
	 */
	public void sendBatchMailWithFile(String[] mailto,String text,String title,String[] filePath)throws Exception;
}
<pre name="code" class="java">2、maven 依赖包
<span style="white-space:pre">		</span><dependency>
			<groupId>javax.mail</groupId>
			<artifactId>mail</artifactId>
			<version>1.4.4</version>
		</dependency>
		<dependency>
			<groupId>javax.activation</groupId>
			<artifactId>activation</artifactId>
			<version>1.1</version>
		</dependency>



<pre name="code" class="java">3、实现发送与接收的实例代码
package tv.bgsc.paymentplatform.mail.test;

import java.util.ArrayList;
import java.util.List;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
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 javax.mail.internet.MimeUtility;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

/********
 * 
 * @description:邮件服务器实现
 * @author:Administrator
 * @date:2015年11月16日 下午7:42:43
 *
 */

@Service("emailService")
public class EmailServiceImpl implements EmailService {

	@Autowired
	private JavaMailSenderImpl javaMailSender;
	
	/*****
	 * 单人邮件发送
	 */
	@Override
	public void sendMail(String mailto, String text, String title)
			throws Exception {

		MimeMessage mimeMessage = javaMailSender.createMimeMessage();  
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");  
        messageHelper.setFrom(new InternetAddress(javaMailSender.getUsername()));  
        messageHelper.setSubject(title);  
        messageHelper.setText(text, true);  
        messageHelper.setTo(new InternetAddress(mailto));  
        mimeMessage = messageHelper.getMimeMessage();  
        javaMailSender.send(mimeMessage);  
        /*EmailThread et = new EmailThread(mimeMessage);  
        et.start();*/
	}

	
	/*******
	 * 群发邮件
	 */
	@Override
	public void sendBatcMail(String[] mailto, String text, String title) {

		for (int i = 0; i < mailto.length; i++) {  
            try {  
                sendMail(mailto[i], text, title);  
            } catch (Exception e) {  
                //logger.error("+++ sendBatchMail error email:"+mailto[i]);  
            }  
        }  
	}

	
	/**********
	 * 单人文件
	 */
	@Override
	public void sendMailWithFile(String mailto, String text, String title,
			String[] filePath) throws Exception {

		MimeMessage mimeMessage = javaMailSender.createMimeMessage();  
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");  
        messageHelper.setFrom(new InternetAddress(javaMailSender.getUsername()));  
        messageHelper.setSubject(title);  
        messageHelper.setText(text, true);  
        messageHelper.setTo(new InternetAddress(mailto));  
        mimeMessage = messageHelper.getMimeMessage();  
  
        if (filePath != null) {  
            BodyPart mdp = new MimeBodyPart();// 新建一个存放信件内容的BodyPart对象  
            mdp.setContent(text, "text/html;charset=UTF-8");// 给BodyPart对象设置内容和格式/编码方式  
            Multipart mm = new MimeMultipart();// 新建一个MimeMultipart对象用来存放BodyPart对象  
            mm.addBodyPart(mdp);// 将BodyPart加入到MimeMultipart对象中(可以加入多个BodyPart)  
            // 把mm作为消息对象的内容  
            MimeBodyPart filePart;  
            FileDataSource filedatasource;  
            // 逐个加入附件  
            for (int j = 0; j < filePath.length; j++) {  
                filePart = new MimeBodyPart();  
                filedatasource = new FileDataSource(filePath[j]);  
                filePart.setDataHandler(new DataHandler(filedatasource));  
                try {  
                    filePart.setFileName(MimeUtility.encodeText(filedatasource.getName()));  
                } catch (Exception e) {  
                    e.printStackTrace();  
                }  
                mm.addBodyPart(filePart);  
            }  
            mimeMessage.setContent(mm);  
        }  
        EmailThread et = new EmailThread(mimeMessage);  
        et.start();
	}

	
	/********
	 * 群发文件
	 */
	@Override
	public void sendBatchMailWithFile(String[] mailto, String text,
			String title, String[] filePath) throws Exception {

		MimeMessage mimeMessage = javaMailSender.createMimeMessage();  
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");  
        messageHelper.setFrom(new InternetAddress(MimeUtility.encodeText(javaMailSender.getUsername())));  
        messageHelper.setSubject(title);  
        if (filePath != null) {  
            BodyPart mdp = new MimeBodyPart();// 新建一个存放信件内容的BodyPart对象  
            mdp.setContent(text, "text/html;charset=UTF-8");// 给BodyPart对象设置内容和格式/编码方式  
            Multipart mm = new MimeMultipart();// 新建一个MimeMultipart对象用来存放BodyPart对象  
            mm.addBodyPart(mdp);// 将BodyPart加入到MimeMultipart对象中(可以加入多个BodyPart)  
            // 把mm作为消息对象的内容  
            MimeBodyPart filePart;  
            FileDataSource filedatasource;  
            // 逐个加入附件  
            for (int j = 0; j < filePath.length; j++) {  
                filePart = new MimeBodyPart();  
                filedatasource = new FileDataSource(filePath[j]);  
                filePart.setDataHandler(new DataHandler(filedatasource));  
                try {  
                    filePart.setFileName(MimeUtility.encodeText(filedatasource.getName()));  
                } catch (Exception e) {  
                    e.printStackTrace();  
                }  
                mm.addBodyPart(filePart);  
            }  
            mimeMessage.setContent(mm);  
        } else {  
            messageHelper.setText(text, true);  
        }  
  
        List<InternetAddress> list = new ArrayList<InternetAddress>();// 不能使用string类型的类型,这样只能发送一个收件人  
        for (int i = 0; i < mailto.length; i++) {  
            list.add(new InternetAddress(mailto[i]));  
        }  
        InternetAddress[] address = list.toArray(new InternetAddress[list.size()]);  
  
        mimeMessage.setRecipients(Message.RecipientType.TO, address);  
        mimeMessage = messageHelper.getMimeMessage();  
  
        EmailThread et = new EmailThread(mimeMessage);  
        et.start();
	}
	
	class EmailThread extends Thread{
		private final MimeMessage mimeMessage;  
		  
        public EmailThread(MimeMessage mimeMessage) {  
            this.mimeMessage = mimeMessage;  
        }  
  
        @Override  
        public void run() {  
            javaMailSender.send(mimeMessage);  
        }
	}
}



   
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:90558次
    • 积分:1237
    • 等级:
    • 排名:千里之外
    • 原创:42篇
    • 转载:17篇
    • 译文:0篇
    • 评论:13条
    最新评论