JavaMail邮件发送功能(包含多收件人 多抄送人 多附件)

最近公司在做发送邮件的项目,本人以前没有接触过,看了网上的代码之后,整理了一下自己的学习心得。希望跟大家一起分享!!!

这里包含 多收件人 多抄送人 多附件 

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

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
import javax.mail.internet.MimeMultipart;

import com.hc.mail.handler.MailHandler;

/**
 * @author 作者 hujingbo
 * @version 创建时间:2017-9-20 上午9:42:09 
 * 类说明 群发邮件 并且带有附件
 */
public class SendMultipleMail {

	public static void main(String[] args) throws Exception {

		List<String> filepath = new ArrayList<String>();
		filepath.add("D:/123.jpg");
		filepath.add("D:/students.xls");
		sendMail("1051064497@qq.com ,huxiaoming625@163.com","1051064497@qq.com ,huxiaoming625@163.com", "注册信息邮件","注册邮件,有附件", filepath);
	}

	/**
	 * @throws Exception
	 * @Description:
	 * @Anthor: hujingbo
	 * @param to
	 *            收件人列表
	 * @param cc
	 *            抄送人列表
	 * @param subject
	 *            发送邮件的主题
	 * @param body
	 *            发送邮件的内容
	 * @param filepath
	 *            添加附件的路径
	 * @return
	 * @throws
	 */
	public static boolean sendMail(String to ,String cc, String subject, String body,List<String> filepath) throws Exception {

		if (subject == null) {
			subject = "无主题邮件";
		}

		if (body == null) {
			body = "";
		}

		// 加载配置信息
		MailHandler md = new MailHandler();
		Properties prop = md.conf;
		String hostName = prop.getProperty("mail.host");
		String username = prop.getProperty("mail.username");
		String password = prop.getProperty("mail.password");
		String from = "service@pjsfax.com"; // 发件人信息
		
		System.out.println("配置信息:" + hostName + "/" + username + "/" + password);

		Session session = Session.getInstance(prop);
		session.setDebug(true);

		Message message = new MimeMessage(session);
		message.setFrom(new InternetAddress(from));
		message.setHeader("邮件标题", "dfsdfsdf");
		
		MimeMultipart multipart = new MimeMultipart();
		// 邮件正文
		MimeBodyPart text = new MimeBodyPart();
		text.setContent("这是邮件正文内容", "text/html;charset=UTF-8");
		multipart.addBodyPart(text);
		
		// 多个收件人地址
		InternetAddress[] addressesTo = null;
		if (to != null && to.trim().length() > 0) {
			String[] receiveList = to.split(",");
			int receiveCount = receiveList.length;
			if (receiveCount > 0) {
				addressesTo = new InternetAddress[receiveCount];
				for (int i = 0; i < receiveCount; i++) {
					addressesTo[i] = new InternetAddress(receiveList[i]);
				}
			}
		} else {
			System.out.println("None receiver!");
			return false;
		}
		
		// 多个抄送人地址
		InternetAddress[] addressesCc = null;
		if (cc != null && cc.trim().length() > 0) {
			String[] copyList = to.split(",");
			int copyCount = copyList.length;
			if (copyCount > 0) {
				addressesCc = new InternetAddress[copyCount];
				for (int i = 0; i < copyCount; i++) {
					addressesCc[i] = new InternetAddress(copyList[i]);
				}
			}
		}
		
		//多个附件
		if (filepath != null && filepath.size() > 0) {
			for (String path : filepath) {

				MimeBodyPart bodyPart = new MimeBodyPart();
				DataSource dh = new FileDataSource(path);
				bodyPart.setDataHandler(new DataHandler(dh));
				bodyPart.setFileName(dh.getName());

				multipart.addBodyPart(bodyPart);
			}

		} 
		
		message.setContent(multipart);
		message.setRecipients(RecipientType.TO, addressesTo);
		message.setRecipients(RecipientType.CC, addressesCc);
		message.setSubject(subject);
		message.setSentDate(new Date());
		message.saveChanges();

		Transport transport = session.getTransport();
		transport.connect(hostName, username, password);
		transport.sendMessage(message, message.getAllRecipients());
		transport.close();

		return true;
		
	}
	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值