java发送邮件类

package com.jspxcms.core.domain;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.apache.commons.lang3.StringUtils;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import com.jspxcms.core.support.Configurable;

public class GlobalMail implements Configurable {
	public static final String PREFIX = "mail_";
	public static final String MAIL_SMTP_HOST = "mail_smtpHost";
	public static final String MAIL_SMTP_PORT = "mail_smtpPort";
	public static final String MAIL_SMTP_AUTH = "mail_smtpAuth";
	public static final String MAIL_SMTP_TIMEOUT = "mail_smtpTimeout";
	public static final String MAIL_SMTP_USERNAME = "mail_smtpUsername";
	public static final String MAIL_SMTP_PASSWORD = "mail_smtpPassword";
	public static final String MAIL_FROM = "mail_from";
	public static final String MAIL_TEST_TO = "mail_testTo";
	public static final String MAIL_TEST_SUBJTCT = "mail_testSubject";
	public static final String MAIL_TEST_TEXT = "mail_testText";

	private Map<String, String> customs;

	public GlobalMail() {
	}

	public GlobalMail(Map<String, String> customs) {
		this.customs = customs;
	}

	public void sendMail(String[] to, String subject, String text) {
		String smtpHost = getSmtpHost();
		if (StringUtils.isBlank(smtpHost)) {
			return;
		}
		Properties prop = new Properties();
		Boolean smtpAuth = getSmtpAuth();
		if (smtpAuth != null) {
			prop.put("mail.smtp.auth", smtpAuth.toString());
		}
		Integer smtpTimeout = getSmtpTimeout();
		if (smtpTimeout != null) {
			prop.put("mail.smtp.timeout", smtpTimeout.toString());
		}

		JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
		mailSender.setDefaultEncoding("UTF-8");
		mailSender.setJavaMailProperties(prop);
		mailSender.setHost(smtpHost);
		Integer port = getSmtpPort();
		if (port != null) {
			mailSender.setPort(port);
		}
		mailSender.setUsername(getSmtpUsername());
		mailSender.setPassword(getSmtpPassword());
		MimeMessage msg = mailSender.createMimeMessage();
		try {
			MimeMessageHelper helper = new MimeMessageHelper(msg, true, "UTF-8");
			helper.setFrom(getFrom());
			helper.setTo(to);
			helper.setSubject(subject);
			helper.setText(text, true);
		} catch (MessagingException e) {
			throw new RuntimeException(e);
		}
		mailSender.send(msg);
	}

	public String getSmtpHost() {
		return getCustoms().get(MAIL_SMTP_HOST);
	}

	public void setSmtpHost(String smtpHost) {
		if (StringUtils.isNotBlank(smtpHost)) {
			getCustoms().put(MAIL_SMTP_HOST, smtpHost);
		} else {
			getCustoms().remove(MAIL_SMTP_HOST);
		}
	}

	public Integer getSmtpPort() {
		String smtpPort = getCustoms().get(MAIL_SMTP_PORT);
		if (StringUtils.isNotBlank(smtpPort)) {
			return Integer.parseInt(smtpPort);
		} else {
			return null;
		}
	}

	public void setSmtpPort(Integer smtpPort) {
		if (smtpPort != null) {
			getCustoms().put(MAIL_SMTP_PORT, smtpPort.toString());
		} else {
			getCustoms().remove(MAIL_SMTP_PORT);
		}
	}

	public Boolean getSmtpAuth() {
		String smtpAuth = getCustoms().get(MAIL_SMTP_AUTH);
		if (StringUtils.isNotBlank(smtpAuth)) {
			return Boolean.parseBoolean(smtpAuth);
		} else {
			return null;
		}
	}

	public void setSmtpAuth(Boolean smtpAuth) {
		if (smtpAuth != null) {
			getCustoms().put(MAIL_SMTP_AUTH, smtpAuth.toString());
		} else {
			getCustoms().remove(MAIL_SMTP_AUTH);
		}
	}

	public Integer getSmtpTimeout() {
		String smtpTimeout = getCustoms().get(MAIL_SMTP_TIMEOUT);
		if (StringUtils.isNotBlank(smtpTimeout)) {
			return Integer.decode(smtpTimeout);
		} else {
			return null;
		}
	}

	public void setSmtpTimeout(Integer smtpTimeout) {
		if (smtpTimeout != null) {
			getCustoms().put(MAIL_SMTP_TIMEOUT, smtpTimeout.toString());
		} else {
			getCustoms().remove(MAIL_SMTP_TIMEOUT);
		}
	}

	public String getFrom() {
		return getCustoms().get(MAIL_FROM);
	}

	public void setFrom(String from) {
		if (StringUtils.isNotBlank(from)) {
			getCustoms().put(MAIL_FROM, from);
		} else {
			getCustoms().remove(MAIL_FROM);
		}
	}

	public String getSmtpUsername() {
		return getCustoms().get(MAIL_SMTP_USERNAME);
	}

	public void setSmtpUsername(String username) {
		if (StringUtils.isNotBlank(username)) {
			getCustoms().put(MAIL_SMTP_USERNAME, username);
		} else {
			getCustoms().remove(MAIL_SMTP_USERNAME);
		}
	}

	public String getSmtpPassword() {
		return getCustoms().get(MAIL_SMTP_PASSWORD);
	}

	public void setSmtpPassword(String password) {
		if (StringUtils.isNotBlank(password)) {
			getCustoms().put(MAIL_SMTP_PASSWORD, password);
		} else {
			getCustoms().remove(MAIL_SMTP_PASSWORD);
		}
	}

	public String getTestTo() {
		return getCustoms().get(MAIL_TEST_TO);
	}

	public void setTestTo(String testTo) {
		if (StringUtils.isNotBlank(testTo)) {
			getCustoms().put(MAIL_TEST_TO, testTo);
		} else {
			getCustoms().remove(MAIL_TEST_TO);
		}
	}

	public String getTestSubject() {
		return getCustoms().get(MAIL_TEST_SUBJTCT);
	}

	public void setTestSubject(String testSubject) {
		if (StringUtils.isNotBlank(testSubject)) {
			getCustoms().put(MAIL_TEST_SUBJTCT, testSubject);
		} else {
			getCustoms().remove(MAIL_TEST_SUBJTCT);
		}
	}

	public String getTestText() {
		return getCustoms().get(MAIL_TEST_TEXT);
	}

	public void setTestText(String testText) {
		if (StringUtils.isNotBlank(testText)) {
			getCustoms().put(MAIL_TEST_TEXT, testText);
		} else {
			getCustoms().remove(MAIL_TEST_TEXT);
		}
	}

	public Map<String, String> getCustoms() {
		if (customs == null) {
			customs = new HashMap<String, String>();
		}
		return customs;
	}

	public void setCustoms(Map<String, String> customs) {
		this.customs = customs;
	}

	public String getPrefix() {
		return PREFIX;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值