【java基础】javaMail发送邮件设置发件人,重点设置中文昵称

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

package com.ztb.common.util.email;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Map;
import java.util.Properties;

public class EmailSendUtils {

	protected final static Logger logger = LoggerFactory.getLogger(EmailSendUtils.class);

	// 邮件发送类
	private static JavaMailSender mailSender;
	// 简单邮件信息类
	private static MimeMessage mimeMessage;
	// HTML邮件信息类
	private static MimeMessageHelper mimeMessageHelper;

	static {
		try {
			mailSender = new JavaMailSenderImpl();
			String host = PropertyUtils.getProperty("emailconfig.host");
			// String userName =
			// PropertyUtils.getProperty("emailconfig.username");
			String userName = "[email protected]";
			// String password =
			// PropertyUtils.getProperty("emailconfig.password");
			String password = "1111";
			String port = "1111";
			// 设置邮件服务主机
			((JavaMailSenderImpl) mailSender).setHost(host);
			((JavaMailSenderImpl) mailSender).setUsername(userName);
			((JavaMailSenderImpl) mailSender).setPassword(password);
			Properties pro = new Properties();
			pro.put("mail.smtp.ssl.enable", "true");
			pro.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
			pro.put("mail.smtp.auth", "true");
			pro.put("mail.smtp.port", port);
			pro.put("mail.smtp.socketFactory.port", port);
			pro.put("mail.smtp.socketFactory.fallback", "false");
			((JavaMailSenderImpl) mailSender).setJavaMailProperties(pro);
		} catch (Exception e) {
			logger.error("邮件服务器初始化错误", e);
			throw new RuntimeException("邮件服务器初始化错误!");
		}
	}

	/**
	 * @return : void
	 * @throws MessagingException
	 * @throws IOException
	 * @Description :使用模板发送邮件,模板里的变量用${XX}表示,EmailTemplate里的Map填充模板变量.
	 * @Creation Date : 2016年4月25日 下午3:02:10
	 */
	public static void sendWithTemplate(EmailTemplate emailTemplate) throws MessagingException, IOException {
		if (emailTemplate == null) {
			return;
		}
		mimeMessage = mailSender.createMimeMessage();
		mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
		mimeMessageHelper.setFrom(emailTemplate.getFrom());
		mimeMessageHelper.setSubject(emailTemplate.getSubject());
		String str = FileUtils.readFileToString(emailTemplate.getTemplateName());
		String text = mergeTemplateIntoString(emailTemplate.getModel(), str);
		mimeMessageHelper.setText(text, emailTemplate.getIsHtml());
		mimeMessageHelper.setTo(emailTemplate.getTo());
		mailSender.send(mimeMessage);
	}

	/**
	 * @return : void
	 * @throws MessagingException
	 * @throws IOException
	 * @Description :使用模板发送邮件,模板里的变量用${XX}表示,EmailTemplate里的Map填充模板变量.
	 */
	public static void sendWithContentHasTemp(EmailTemplate emailTemplate) throws MessagingException, IOException {
		if (emailTemplate == null) {
			return;
		}
		mimeMessage = mailSender.createMimeMessage();
		mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
		mimeMessageHelper.setFrom(emailTemplate.getFrom());
		mimeMessageHelper.setSubject(emailTemplate.getSubject());
		String str = (emailTemplate.getContent());
		String text = mergeTemplateIntoString(emailTemplate.getModel(), str);
		mimeMessageHelper.setText(text, emailTemplate.getIsHtml());
		mimeMessageHelper.setTo(emailTemplate.getTo());
		mailSender.send(mimeMessage);
	}

	/**
	 * @return : void
	 * @throws MessagingException
	 * @Description 发送普通HTMl邮件
	 */
	public static void sendWithHtml(EmailTemplate emailTemplate) throws MessagingException {
		if (emailTemplate == null) {
			return;
		}
		mimeMessage = mailSender.createMimeMessage();
		mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
		mimeMessageHelper.setFrom(emailTemplate.getFrom());
		mimeMessageHelper.setTo(emailTemplate.getTo());
		mimeMessageHelper.setSubject(emailTemplate.getSubject());
		mimeMessageHelper.setText(emailTemplate.getContent(), true);
		mailSender.send(mimeMessage);
	}

	/**
	 * @return : void
	 * @Description : 发送普通文本邮件
	 */
	public static void sendWithText(EmailTemplate emailTemplate) {
		if (emailTemplate == null) {
			return;
		}
		SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
		simpleMailMessage.setFrom(emailTemplate.getFrom());
		simpleMailMessage.setTo(emailTemplate.getTo());
		simpleMailMessage.setSubject(emailTemplate.getSubject());
		simpleMailMessage.setText(emailTemplate.getContent());
		simpleMailMessage.setSentDate(new Date());
		mailSender.send(simpleMailMessage);
	}
	/**
	 * 带有昵称的发送邮件
	 * @param emailTemplate
	 * @throws MessagingException
	 */
	public static void sendWithTextNick(EmailTemplate emailTemplate) throws MessagingException 
	{
		if (emailTemplate == null) {
			return;
		}
		//设置自定义发件人昵称    
        String nick="";    
        try {    
            nick=javax.mail.internet.MimeUtility.encodeText(emailTemplate.getModel().get("nick"));    
        } catch (UnsupportedEncodingException e) {    
            e.printStackTrace();    
        }     
		mimeMessage = mailSender.createMimeMessage();
		mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        mimeMessageHelper.setFrom(new InternetAddress(nick+" <"+emailTemplate.getFrom()+">")); 
		mimeMessageHelper.setTo(emailTemplate.getTo());
		mimeMessageHelper.setSubject(emailTemplate.getSubject());
		mimeMessageHelper.setText(emailTemplate.getContent(), true);
		mimeMessageHelper.setSentDate(new Date());
		mailSender.send(mimeMessage);
	}

	/**
	 * @return : String
	 * @Description : 根据变量替换字符串中的值
	 * @Creation Date : 2016年4月26日 上午9:57:16
	 */
	public static String mergeTemplateIntoString(Map map, String str) throws IOException {
		if (map == null || map.isEmpty()) {
			return str;
		}
		for (String key : map.keySet()) {
			String keypath = "${" + key + "}";
			if (str.contains(keypath)) {
				str = str.replace(keypath, String.valueOf(map.get(key)));
			}
		}
		return str;
	}

}

 

转载于:https://my.oschina.net/maojindaoGG/blog/1617256

你可能感兴趣的:(java)