zoukankan      html  css  js  c++  java
  • MailUtils

    /**
     *包名:com.thinkgem.jeesite.test
     *描述:package com.thinkgem.jeesite.test;
     */
    package com.thinkgem.jeesite.test;
    
    import java.util.Date;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Properties;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    import javax.mail.internet.MimeMessage;
    
    import org.apache.commons.mail.HtmlEmail;
    import org.springframework.mail.javamail.JavaMailSenderImpl;
    import org.springframework.mail.javamail.MimeMessageHelper;
    
    import com.jjqkkkaa.posp.common.utils.DateUtils;
    
    /**
     * MailUtilsss.java
     * 版权所有(C) 2018 裕福控股有限公司
     * 创建:gll 
     * 时间:2018年2月8日
     * 描述:MailUtils
     */
    public class MailUtils {
    
    	private static final String from = "aaaaaaaaa@163.com";
    	private static final String fromName = "测试公司";
    	private static final String charSet = "utf-8";
    	private static final String username = "aaaaaaaa@163.com";
    	private static final String password = "123456";
    
    	private static Map<String, String> hostMap = new HashMap<String, String>();
    	static {
    		// 126
    		hostMap.put("smtp.126", "smtp.126.com");
    		// qq
    		hostMap.put("smtp.qq", "smtp.qq.com");
    
    		// 163
    		hostMap.put("smtp.163", "smtp.163.com");
    
    		// sina
    		hostMap.put("smtp.sina", "smtp.sina.com.cn");
    
    		// tom
    		hostMap.put("smtp.tom", "smtp.tom.com");
    
    		// 263
    		hostMap.put("smtp.263", "smtp.263.net");
    
    		// yahoo
    		hostMap.put("smtp.yahoo", "smtp.mail.yahoo.com");
    
    		// hotmail
    		hostMap.put("smtp.hotmail", "smtp.live.com");
    
    		// gmail
    		hostMap.put("smtp.gmail", "smtp.gmail.com");
    		hostMap.put("smtp.port.gmail", "465");
    	}
    
    	public static String getHost(String email) throws Exception {
    		Pattern pattern = Pattern.compile("\w+@(\w+)(\.\w+){1,2}");
    		Matcher matcher = pattern.matcher(email);
    		String key = "unSupportEmail";
    		if (matcher.find()) {
    			key = "smtp." + matcher.group(1);
    		}
    		if (hostMap.containsKey(key)) {
    			return hostMap.get(key);
    		} else {
    			throw new Exception("unSupportEmail");
    		}
    	}
    
    	public static int getSmtpPort(String email) throws Exception {
    		Pattern pattern = Pattern.compile("\w+@(\w+)(\.\w+){1,2}");
    		Matcher matcher = pattern.matcher(email);
    		String key = "unSupportEmail";
    		if (matcher.find()) {
    			key = "smtp.port." + matcher.group(1);
    		}
    		if (hostMap.containsKey(key)) {
    			return Integer.parseInt(hostMap.get(key));
    		} else {
    			return 25;
    		}
    	}
    
    
    	/**
    	 * 发送普通邮件
    	 * 
    	 * @param toMailAddr
    	 *            收信人地址
    	 * @param subject
    	 *            email主题
    	 * @param message
    	 *            发送email信息
    	 */
    	public static void sendCommonMail(String toMailAddr, String subject,
    			String message) {
    		HtmlEmail hemail = new HtmlEmail();
    		try {
    			hemail.setHostName(getHost(from));
    			hemail.setSmtpPort(getSmtpPort(from));
    			hemail.setCharset(charSet);
    			hemail.addTo(toMailAddr);
    			hemail.setFrom(from, fromName);
    			hemail.setAuthentication(username, password);
    			hemail.setSubject(subject);
    			hemail.setMsg(message);
    			hemail.send();
    			System.out.println("email send true!");
    		} catch (Exception e) {
    			e.printStackTrace();
    			System.out.println("email send error!");
    		}
    
    	}
    
    	public static void main(String[] args) {
    		 try {
    		 sendInlineMail();
    		 } catch (Exception e) {
    		 // TODO Auto-generated catch block
    		 e.printStackTrace();
    		 }
    	}
    
    	// 发送带内嵌文件的HTML格式邮件
    	public static void sendInlineMail() throws Exception {
    		String smtp = "smtp";
    		String host = "smtp.163.com";
    		String sslIs ="true";
    		String authIs = "true";
    		String userName = "aa@163.com";
    		String password = "aa#";
    
    		// spring提供的邮件实现类
    		JavaMailSenderImpl send = new JavaMailSenderImpl();
    		Properties prop = new Properties();
    		prop.setProperty("mail.transport.protocol", smtp); // 设置邮件发送协议
    		prop.setProperty("mail.host", host); // 邮件服务器地址
    		prop.setProperty("mail.smtps.ssl.enable", sslIs); // 邮件ssl验证
    		prop.setProperty("mail.smtp.auth", authIs); // 邮件服务身份验证
    
    		send.setUsername(userName); // 设置用户名
    		send.setPassword(password); // 设置密码
    		send.setJavaMailProperties(prop);
    
    		MimeMessage msg = send.createMimeMessage();
    		// 指定HTML编码,参数true表示为multipart
    		MimeMessageHelper helper = new MimeMessageHelper(msg, true, "UTF-8");
    		helper.setFrom(userName); // 发送者邮箱
    		helper.setTo("800@163.com"); // 接收者邮箱
    		// helper.setCc(CC_MAIL); // 抄送邮箱
    		// helper.setBcc(BCC_MAIl); // 密送邮箱
    		helper.setSentDate(new Date()); // 发送日期
    		helper.setSubject("工资条(" + DateUtils.getDate("yyyy-MM-dd HH:mm:ss")
    				+ ")");
    		String html = "<font size='5' color='red'>HTML格式测试成功!</font>";
    		helper.setText(html, true); // 邮件内容,参数true表示是html代码
    		send.send(msg); // 发送邮件
    	}
    
    }
    

      

  • 相关阅读:
    JVM调优-Java中的对象
    Spring MVC如何接收浏览器传递来的请求参数--request--形参--实体类封装
    Navicat for MySQL 64位破解版
    Idea详细配置
    数据库--悲观锁【收藏,待尝试】
    性能问题分析
    java 反射机制之 getDeclaredMethod()获取方法,然后invoke执行实例对应的方法
    Mysql show indexes 查看索引状态
    基于(Redis | Memcache)实现分布式互斥锁
    彻底解决每次打开visio都提示windows正在配置visio的问题
  • 原文地址:https://www.cnblogs.com/guoziyi/p/8432331.html
Copyright © 2011-2022 走看看