package org.mail;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Date;
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.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

import org.entity.MailModel;

/**
 * 发送一封邮件
 * @说明 注意这里并没有实际发送而是保存在了C盘临时文件中,真是发送的话,请参考我的博客
 * @author fei.teng
 * @version 1.0
 * @since
 */
public class SendMail {

	public static void sendMail(MailModel mail) throws Exception {
		Properties props = new Properties();
		props.put("mail.smtp.auth", "true");
		Session session = Session.getInstance(props);
		Message message = new MimeMessage(session);
		InternetAddress from = new InternetAddress();
		from.setPersonal(MimeUtility.encodeText("风中落叶<cuisuqiang@163.com>"));
		message.setFrom(from);
		InternetAddress to = new InternetAddress(mail.getTo());
		message.setRecipient(Message.RecipientType.TO, to);
		// 是否抄送
		if (null != mail.getCopy() && !"".equals(mail.getCopy())) {
			InternetAddress copy = new InternetAddress(mail.getCopy());
			message.setRecipient(Message.RecipientType.CC, copy);
		}
		message.setSubject(MimeUtility.encodeText(mail.getTitle()));
		message.setSentDate(new Date());
		// 指定为混合关系
		MimeMultipart msgMultipart = new MimeMultipart("mixed");
		message.setContent(msgMultipart);
		MimeBodyPart content = new MimeBodyPart();
		msgMultipart.addBodyPart(content);
		// 依赖关系
		MimeMultipart bodyMultipart = new MimeMultipart("related");
		content.setContent(bodyMultipart);
		MimeBodyPart htmlPart = new MimeBodyPart();
		// 组装的顺序非常重要
		bodyMultipart.addBodyPart(htmlPart);
		MimeBodyPart in_bg = new MimeBodyPart();
		bodyMultipart.addBodyPart(in_bg);

		DataSource bgsou = new FileDataSource(mail.getBgPath());
		DataHandler bghd = new DataHandler(bgsou);
		in_bg.setDataHandler(bghd);
		in_bg.setHeader("Content-Location", "bg.jpg");
		// 是否使用了背景音乐
		if (null == mail.getMusicPath() || "".equals(mail.getMusicPath())) {
			String start = "<html><body background='bg.jpg'>";
			String end = "</body></html>";
			htmlPart.setContent(start + mail.getContext() + end,"text/html;charset=UTF-8");
		} else {
			MimeBodyPart in_Part = new MimeBodyPart();
			bodyMultipart.addBodyPart(in_Part);
			DataSource gifds = new FileDataSource(mail.getMusicPath());
			DataHandler gifdh = new DataHandler(gifds);
			in_Part.setDataHandler(gifdh);
			in_Part.setHeader("Content-Location", "bg.mp3");
			String start = "<html><head><bgsound src='bg.mp3' loop='-1'></head><body background='bg.jpg'>";
			String end = "</body></html>";
			htmlPart.setContent(start + mail.getContext() + end,"text/html;charset=UTF-8");
		}
		// 组装附件
		if (null != mail.getFilePath() && !"".equals(mail.getFilePath())) {			
			MimeBodyPart file = new MimeBodyPart();
			FileDataSource file_datasource = new FileDataSource(mail
					.getFilePath());
			DataHandler dh = new DataHandler(file_datasource);
			file.setDataHandler(dh);
			file.setFileName(MimeUtility.encodeText(dh.getName()));
			msgMultipart.addBodyPart(file);
		}
		message.saveChanges();
		// 保存邮件
		OutputStream ips = new FileOutputStream("C:\\tmp\\test.eml");
		message.writeTo(ips);
		ips.close();
		System.out.println("------------发送完毕------------");
		// 删除临时文件
		if (null != mail.getMusicPath() && !"".equals(mail.getMusicPath())) {
			File file = new File(mail.getMusicPath());
			file.delete();
		}
		if (null != mail.getFilePath() && !"".equals(mail.getFilePath())) {
			File file = new File(mail.getFilePath());
			file.delete();
		}
	}

}
最近下载更多
annazhang  LV29 2023年4月7日
chennguo  LV6 2019年4月24日
你爸爸123  LV7 2018年11月1日
Guess me  LV1 2018年6月23日
君子风彪  LV13 2018年5月19日
2360633889  LV4 2017年12月15日
孤寒双月泪  LV14 2017年5月19日
matain  LV9 2017年5月10日
anhuaer  LV8 2017年1月5日
svwdasf  LV1 2016年12月19日
最近浏览更多
Wky123456 2023年11月2日
暂无贡献等级
神龙摆尾无拘束  LV2 2023年3月17日
微信网友_6248713511227392  LV11 2022年12月5日
WeiZZZ  LV1 2022年3月28日
二十一画  LV10 2021年11月14日
山鬼非鬼  LV1 2021年10月6日
linhe27  LV7 2021年3月20日
喵小兵  LV10 2021年3月13日
ccandll  LV1 2021年3月10日
ming_123_9715  LV23 2021年3月6日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友