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); // 发送邮件
    	}
    
    }
    

      

  • 相关阅读:
    4、路由事件 RoutedEvent
    3、Grid、GridSplitter 网格分离器、SharedSizeGroup 共享尺寸组
    2、DockPanel
    1、布局容器Grid、StackPanel、GroupBox、DockPanel、WrapPanel
    15、Qt 样式表
    14、SpinBox与Horizontal Scroll Bar
    13、Qt界面布局
    12、label控件
    11、LineEdit与setCompleter自动补全
    Linux设备模型 (2)
  • 原文地址:https://www.cnblogs.com/guoziyi/p/8432331.html
Copyright © 2011-2022 走看看