package com.edu.dk.portal.app.zp.util;

import java.util.Date;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Authenticator;
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;

/**
 * @describe:邮件服务类
 * 
 * @date 2013-9-11
 * @author yuanlin.su
 */
public final class MailService extends Authenticator {
	public MailService() {
	}

	private static MailService mailService = null;

	public synchronized static MailService getInstance() {
		if (mailService == null) {
			mailService = new MailService();
		}
		return mailService;
	}

	private Session getMailSession() {
		Properties props = new Properties();
		props.setProperty("mail.smtp.auth", SystemProperties.getProperty("mail.send.smtp.auth"));
		props.setProperty("mail.transport.protocol", SystemProperties.getProperty("mail.send.transport.protocol"));
		props.setProperty("mail.store.protocol", SystemProperties.getProperty("mail.send.store.protocol"));
		props.put("mail.smtp.host", SystemProperties.getProperty("mail.send.smtp.host"));
		Session session = Session.getInstance(props, this);
		return session;
	}

	@Override
	protected PasswordAuthentication getPasswordAuthentication() {
		return new PasswordAuthentication(SystemProperties.getProperty("mail.send.username"), SystemProperties.getProperty("mail.send.password"));
	}

	/**
	 * 发送邮件
	 * 
	 * @param subject
	 *            主题
	 * @param message
	 *            消息内容
	 */
	public void sendMail(String subject, String message, Address[] address) {
		// 根据session创建一个邮件消息
		Message mailMessage = new MimeMessage(getMailSession());

		try {
			// 创建邮件发送者地址
			Address from = new InternetAddress(SystemProperties.getProperty("mail.send.address"));
			// 设置邮件消息的发送者
			mailMessage.setFrom(from);
			// 创建邮件的接收者地址,并设置到邮件消息中
			mailMessage.setRecipients(Message.RecipientType.TO, address);
			// 设置邮件消息的主题
			mailMessage.setSubject(subject);
			// 设置邮件消息发送的时间
			mailMessage.setSentDate(new Date());

			MimeBodyPart mbp1 = new MimeBodyPart();
			mbp1.setContent(message, "text/html;charset=utf-8");

			Multipart mp = new MimeMultipart();
			mp.addBodyPart(mbp1);

			mailMessage.setContent(mp);
			// 发送邮件
			Transport.send(mailMessage);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
最近下载更多
1358849392  LV21 2022年12月27日
yliu123  LV9 2021年9月17日
1211413075  LV14 2019年6月1日
请叫我小C  LV19 2016年11月1日
fhhdrf  LV4 2016年4月12日
旧戏子  LV11 2015年11月19日
hsjkks  LV3 2015年4月27日
1743524389  LV10 2015年3月2日
如风的爱  LV28 2014年7月20日
danniel  LV4 2013年9月18日
最近浏览更多
lzx602  LV3 4月15日
1358849392  LV21 2022年12月27日
kahvia  LV1 2022年5月8日
adcxiaoxueren 2022年4月20日
暂无贡献等级
WeiZZZ  LV1 2022年3月28日
yliu123  LV9 2021年9月17日
TerryGaoBo  LV9 2021年8月16日
luesjim  LV11 2021年6月18日
linhe27  LV7 2021年3月20日
pxqtsht  LV15 2021年1月28日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友