邮件发送工具类

邮件发送工具类

package com.util;

import java.io.File;
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.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
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.MimeMultipart;
import javax.mail.internet.MimeUtility;

//import com.chinainsurance.application.platform.dto.domain.GgCodeTransferDto;

public class SendMail {
	private String mail_host = ""; // 发信服务器地址
	private String mail_port = "25"; // 发信服务器端口
	private String userName = ""; // 发件人账号
	private String password = ""; // 发件人密码

	/**
	 * 发送文本邮件
	 * 
	 * @param to
	 *            收件人
	 * @param subject
	 *            标题
	 * @param sentDate
	 *            发信日期
	 * @param text
	 *            文本内容
	 * @throws Exception
	 */
	public void sendTextMail(String to, String subject, Date sentDate,
			String text) throws Exception {
		Properties pro = System.getProperties();
		pro.put("mail.smtp.host", mail_host);
		pro.put("mail.smtp.port", mail_port);
		pro.put("mail.smtp.auth", "true");

		// 根据邮件会话属性和密码验证器构造一个发送邮件的session
		Session sendMailSession = Session.getDefaultInstance(pro,
				new Authenticator() {
					@Override
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(userName, password);
					}
				});
		// 根据session创建一个邮件消息
		Message mailMessage = new MimeMessage(sendMailSession);
		// 设置邮件消息的发送者
		mailMessage.setFrom(new InternetAddress(userName));
		// 创建邮件的接收者地址,并设置到邮件消息中
		mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(
				to));
		// 设置邮件消息的主题
		mailMessage.setSubject(subject);
		// 设置邮件消息发送的时间
		mailMessage.setSentDate(sentDate);
		// 设置邮件消息的主要内容
		mailMessage.setText(text);
		// 发送邮件
		Transport.send(mailMessage);
	}

	/**
	 * 发送Html邮件
	 * 
	 * @throws Exception
	 */
	public void sendHtmlMail(List<GgCodeTransferDto> toList,
			List<GgCodeTransferDto> ccList, String subject, Date sentDate,
			String text) throws Exception {
		Properties pro = System.getProperties();
		pro.put("mail.smtp.host", mail_host);
		pro.put("mail.smtp.port", mail_port);
		pro.put("mail.smtp.auth", "true");

		// 根据邮件会话属性和密码验证器构造一个发送邮件的session
		Session sendMailSession = Session.getDefaultInstance(pro,
				new Authenticator() {
					@Override
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(userName, password);
					}
				});
		// 根据session创建一个邮件消息
		Message mailMessage = new MimeMessage(sendMailSession);
		// 设置邮件消息的发送者
		mailMessage.setFrom(new InternetAddress(userName));

		InternetAddress[] address = new InternetAddress[toList.size()];
		InternetAddress[] addressCc = new InternetAddress[ccList.size()];
		// 创建邮件的接收者地址,并设置到邮件消息中
		if (toList != null && toList.size() > 0) {
			for (int i = 0; i < toList.size(); i++) {
				address[i] = new InternetAddress(toList.get(i).getOuterCode());
			}
		}
		
		// 抄送
		if (ccList != null && ccList.size() > 0) {
			for (int j = 0; j < ccList.size(); j++) {
				addressCc[j] = new InternetAddress(ccList.get(j).getOuterCode());
			}
		}

		if (address != null && address.length > 0) {
			mailMessage.setRecipients(Message.RecipientType.TO, address);
			if (addressCc != null && addressCc.length > 0) {
				mailMessage.setRecipients(Message.RecipientType.CC, addressCc);
			}
		} else {
			if (addressCc != null && addressCc.length > 0) {
				mailMessage.setRecipients(Message.RecipientType.TO, addressCc);	
			}
		}

		// 设置邮件消息的主题
		mailMessage.setSubject(subject);
		// 设置邮件消息发送的时间
		mailMessage.setSentDate(sentDate);

		// MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
		Multipart mainPart = new MimeMultipart();
		// 创建一个包含HTML内容的MimeBodyPart
		BodyPart html = new MimeBodyPart();
		// 设置HTML内容
		html.setContent(text, "text/html; charset=utf-8");
		mainPart.addBodyPart(html);
		// 将MiniMultipart对象设置为邮件内容
		mailMessage.setContent(mainPart);
		// 发送邮件
		Transport.send(mailMessage);
	}

	/**
	 * 发送Html邮件
	 * 
	 * @throws Exception
	 */
	public void sendHtmlMail2(String to, String cto, String subject,
			Date sentDate, String text) throws Exception {
		Properties pro = System.getProperties();
		pro.put("mail.smtp.host", mail_host);
		pro.put("mail.smtp.port", mail_port);
		pro.put("mail.smtp.auth", "true");

		// 根据邮件会话属性和密码验证器构造一个发送邮件的session
		Session sendMailSession = Session.getDefaultInstance(pro,
				new Authenticator() {
					@Override
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(userName, password);
					}
				});
		// 根据session创建一个邮件消息
		Message mailMessage = new MimeMessage(sendMailSession);
		// 设置邮件消息的发送者
		mailMessage.setFrom(new InternetAddress(userName));
		// 创建邮件的接收者地址,并设置到邮件消息中
		InternetAddress[] address = new InternetAddress[1];
		address[0] = new InternetAddress(to);
		mailMessage.setRecipients(Message.RecipientType.TO, address);

		// 抄送
		if (cto != null) {
			InternetAddress[] addressCc = new InternetAddress[1];
			addressCc[0] = new InternetAddress(cto);
			mailMessage.setRecipients(Message.RecipientType.CC, addressCc);
		}

		// 设置邮件消息的主题
		mailMessage.setSubject(subject);
		// 设置邮件消息发送的时间
		mailMessage.setSentDate(sentDate);

		// MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
		Multipart mainPart = new MimeMultipart();
		// 创建一个包含HTML内容的MimeBodyPart
		BodyPart html = new MimeBodyPart();
		// 设置HTML内容
		html.setContent(text, "text/html; charset=utf-8");
		mainPart.addBodyPart(html);
		// 将MiniMultipart对象设置为邮件内容
		mailMessage.setContent(mainPart);
		// 发送邮件
		Transport.send(mailMessage);
	}

	/**
	 * 发送内嵌图片邮件
	 * 
	 * @throws Exception
	 */
	public void sendImageMail() throws Exception {
		Properties pro = System.getProperties();
		pro.put("mail.smtp.host", mail_host);
		pro.put("mail.smtp.port", mail_port);
		pro.put("mail.smtp.auth", "true");

		// 根据邮件会话属性和密码验证器构造一个发送邮件的session
		Session sendMailSession = Session.getDefaultInstance(pro,
				new Authenticator() {
					@Override
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(userName, password);
					}
				});
		// 根据session创建一个邮件消息
		Message mailMessage = new MimeMessage(sendMailSession);
		// 设置邮件消息的发送者
		mailMessage.setFrom(new InternetAddress(userName));
		// 创建邮件的接收者地址,并设置到邮件消息中
		// mailMessage.setRecipient(Message.RecipientType.TO,
		// new InternetAddress(to));
		// 设置邮件消息的主题
		mailMessage.setSubject("Test Email");
		// 设置邮件消息发送的时间
		mailMessage.setSentDate(new Date());

		MimeMultipart multipart = new MimeMultipart("related");

		BodyPart messageBodyPart = new MimeBodyPart();
		String htmlText = "<html><body><img src='cid:image'/><div>this is a HTML email.</div></body></html>";
		messageBodyPart.setContent(htmlText, "text/html; charset=utf-8");
		multipart.addBodyPart(messageBodyPart);

		MimeBodyPart imageBodyPart = new MimeBodyPart();
		DataSource fds = new FileDataSource("1_jianggujin.jpg");
		imageBodyPart.setDataHandler(new DataHandler(fds));
		// imageBodyPart.setContentID("image");
		multipart.addBodyPart(imageBodyPart);

		mailMessage.setContent(multipart);
		// 发送邮件
		Transport.send(mailMessage);
	}

	/**
	 * 发送附件邮件
	 * 
	 * @throws Exception
	 */
	public void sendAttachmentMail() throws Exception {
		Properties pro = System.getProperties();
		pro.put("mail.smtp.host", mail_host);
		pro.put("mail.smtp.port", mail_port);
		pro.put("mail.smtp.auth", "true");

		// 根据邮件会话属性和密码验证器构造一个发送邮件的session
		Session sendMailSession = Session.getDefaultInstance(pro,
				new Authenticator() {
					@Override
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(userName, password);
					}
				});
		// 根据session创建一个邮件消息
		Message mailMessage = new MimeMessage(sendMailSession);
		// 设置邮件消息的发送者
		mailMessage.setFrom(new InternetAddress(userName));
		// 创建邮件的接收者地址,并设置到邮件消息中
		// mailMessage.setRecipient(Message.RecipientType.TO,
		// new InternetAddress(to));
		// 设置邮件消息的主题
		mailMessage.setSubject("Test Email");
		// 设置邮件消息发送的时间
		mailMessage.setSentDate(new Date());

		MimeMultipart multipart = new MimeMultipart("mixed");

		BodyPart messageBodyPart = new MimeBodyPart();
		String htmlText = "<html><body><div>this is a Attachment email.this email has a attachment!</div></body></html>";
		messageBodyPart.setContent(htmlText, "text/html; charset=utf-8");
		multipart.addBodyPart(messageBodyPart);

		MimeBodyPart imageBodyPart = new MimeBodyPart();
		File imageFile = new File("1_jianggujin.jpg");
		DataSource fds = new FileDataSource(imageFile);
		imageBodyPart.setDataHandler(new DataHandler(fds));
		imageBodyPart.setFileName(MimeUtility.encodeWord(imageFile.getName()));
		multipart.addBodyPart(imageBodyPart);

		mailMessage.setContent(multipart);
		// 发送邮件
		Transport.send(mailMessage);
	}

	/**
	 * 发送内嵌图片和附件邮件
	 * 
	 * @throws Exception
	 */
	public void sendImageAndAttachmentMail() throws Exception {
		Properties pro = System.getProperties();
		pro.put("mail.smtp.host", mail_host);
		pro.put("mail.smtp.port", mail_port);
		pro.put("mail.smtp.auth", "true");

		// 根据邮件会话属性和密码验证器构造一个发送邮件的session
		Session sendMailSession = Session.getDefaultInstance(pro,
				new Authenticator() {
					@Override
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(userName, password);
					}
				});
		// 根据session创建一个邮件消息
		Message mailMessage = new MimeMessage(sendMailSession);
		// 设置邮件消息的发送者
		mailMessage.setFrom(new InternetAddress(userName));
		// 创建邮件的接收者地址,并设置到邮件消息中
		// mailMessage.setRecipient(Message.RecipientType.TO,
		// new InternetAddress(to));
		// 设置邮件消息的主题
		mailMessage.setSubject("Test Email");
		// 设置邮件消息发送的时间
		mailMessage.setSentDate(new Date());

		// 正文
		MimeBodyPart text = new MimeBodyPart();
		text.setContent("我的头像:<img src='cid:headImg'>",
				"text/html;charset=UTF-8");

		// 正文图片
		MimeBodyPart image = new MimeBodyPart();
		image.setDataHandler(new DataHandler(new FileDataSource(
				"1_jianggujin.jpg")));
		// image.setContentID("headImg");

		// 附件
		MimeBodyPart attach = new MimeBodyPart();
		DataHandler dh = new DataHandler(new FileDataSource("1_jianggujin.jpg"));
		attach.setDataHandler(dh);
		attach.setFileName(MimeUtility.encodeWord(dh.getName()));

		// 描述关系:正文和图片
		MimeMultipart mp1 = new MimeMultipart();
		mp1.addBodyPart(text);
		mp1.addBodyPart(image);
		mp1.setSubType("related");

		// 正文
		MimeBodyPart content = new MimeBodyPart();
		content.setContent(mp1);

		MimeMultipart multipart = new MimeMultipart("mixed");
		multipart.addBodyPart(content);
		multipart.addBodyPart(attach);

		mailMessage.setContent(multipart);
		// 发送邮件
		Transport.send(mailMessage);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值