package cn.lbj;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;


public class EmailUtils {

	private String emailIP;
	private String username;
	private String authorizationCode;
	private String myEmail;
	private String emailPort;

	// 是否显示日志
	private boolean showDebugInfo;

	/**
	 * @param emailIP
	 *            发件人的SMTP服务器地址,如果是本机,可以使用localhost<br>
	 *            假设使用的是QQ邮箱,IP为smtp.qq.com
	 * @param username
	 *            发送邮箱的登陆用户名<br>
	 *            如:123456@qq.com,username = 123456
	 * @param authorizationCode
	 *            发送邮箱的授权码
	 * @param myEmail
	 *            发送邮箱(自己的)
	 * 
	 * @param emailPort
	 *            发送邮箱的端口, 一般是25
	 */
	public EmailUtils(String emailIP, String username, String authorizationCode, String myEmail, String emailPort) {
		this.emailIP = emailIP;
		this.username = username;
		this.authorizationCode = authorizationCode;
		this.myEmail = myEmail;
		this.emailPort = emailPort;
	}

	/**
	 * @param emailIP
	 *            发件人的SMTP服务器地址,如果是本机,可以使用localhost<br>
	 *            假设使用的是QQ邮箱,IP为smtp.qq.com
	 * @param username
	 *            发送邮箱的登陆用户名<br>
	 *            如:123456@qq.com,username = 123456
	 * @param authorizationCode
	 *            发送邮箱的授权码
	 * @param myEmail
	 *            发送邮箱(自己的)
	 * 
	 * @param emailPort
	 *            发送邮箱的端口, 一般是25
	 * 
	 * @param showDebugInfo
	 *            是否要显示日志,默认不显示,设置true为显示
	 */
	public EmailUtils(String emailIP, String username, String authorizationCode, String myEmail, String emailPort,
			boolean isShowDebugInfo) {
		this.emailIP = emailIP;
		this.username = username;
		this.authorizationCode = authorizationCode;
		this.myEmail = myEmail;
		this.emailPort = emailPort;
		this.showDebugInfo = isShowDebugInfo;
	}

	public void setShowDebugInfo(boolean showDebugInfo) {
		this.showDebugInfo = showDebugInfo;
	}

	public boolean isShowDebugInfo() {
		return showDebugInfo;
	}

	public String getEmailIP() {
		return emailIP;
	}

	public String getUsername() {
		return username;
	}

	public String getAuthorizationCode() {
		return authorizationCode;
	}

	public String getMyEmail() {
		return myEmail;
	}

	public String getEmailPort() {
		return emailPort;
	}

	/**
	 * 发送邮件的方法
	 * 
	 * @param to
	 *            收件人邮箱
	 * @param subject
	 *            标题文本信息
	 * @param emailMsg
	 *            邮件内容信息
	 * @throws AddressException
	 *             收件人邮箱地址错误
	 * @throws MessagingException
	 *             邮件内容错误
	 */
	public void sendMail(String to, String subject, String emailMsg) throws AddressException, MessagingException {

		// 连接邮件服务器的参数配置
		Properties props = new Properties();
		
		// 发送邮件的邮箱需要登陆验证之的才能发送
		props.setProperty("mail.smtp.auth", "true");

		// 设置传输协议
		props.setProperty("mail.transport.protocol", "smtp");

		// 设置发件人的SMTP服务器地址
		props.setProperty("mail.smtp.host", emailIP);

		// 发件人自己的账号
		props.setProperty("mail.user", myEmail);

		// 访问SMTP服务时需要提供的密码
		props.setProperty("mail.password", authorizationCode);
		props.setProperty("mail.smtp.port", emailPort);
		props.setProperty("mail.smtp.starttls.enable", "true");

		// 绑定用户名和密码
		Authenticator auth = new Authenticator() {
			public PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(username, authorizationCode);
			}
		};

		// 创建定义整个应用程序所需的环境信息的 Session 对象
		Session session = Session.getInstance(props, auth);

		// 是否要显示日志,默认不显示
		if (showDebugInfo) {
			// 设置调试信息在控制台打印出来
			session.setDebug(true);
		}

		// 创建邮件的实例对象
		Message message = new MimeMessage(session);

		// 设置发件邮箱
		message.setFrom(new InternetAddress(myEmail));

		/*
		 * 设置收件人地址(可以增加多个收件人、抄送、密送) MimeMessage.RecipientType.TO:发送
		 * MimeMessage.RecipientType.CC:抄送 MimeMessage.RecipientType.BCC:密送
		 */
		message.setRecipient(RecipientType.TO, new InternetAddress(to));

		// 设置邮件主题(标题)
		message.setSubject(subject);

		// 设置邮件正文和MIME格式及编码
		message.setContent(emailMsg, "text/html;charset=utf-8");

		// 发送邮件
		Transport.send(message);
	}

	@Override
	public String toString() {
		return "EmailUtils [emailIP=" + emailIP + ", username=" + username + ", authorizationCode=" + authorizationCode
				+ ", myEmail=" + myEmail + ", emailPort=" + emailPort + "]";
	}
}
最近下载更多
wuziayng1232  LV10 2023年2月20日
Tuxxxxx  LV3 2023年1月3日
微信网友_6094489572675584  LV1 2022年8月18日
橙橙无话可说  LV1 2021年12月11日
歪歪歪  LV8 2021年9月17日
dapeng0011  LV13 2021年8月20日
1355862436  LV9 2020年1月28日
geekcjj  LV18 2019年12月30日
13787836732  LV5 2019年8月22日
tq1314  LV2 2019年8月1日
最近浏览更多
lzx602  LV3 4月15日
guaixia163  LV13 4月7日
Shengyi188 2023年12月18日
暂无贡献等级
liangge2115  LV27 2023年12月3日
EFWAGGFAWGR 2023年10月18日
暂无贡献等级
2661033436  LV2 2023年4月15日
wuziayng1232  LV10 2023年2月20日
Tuxxxxx  LV3 2023年1月3日
heqian  LV16 2022年10月14日
爱情戴罪的羔羊  LV7 2022年9月17日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友