⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 agilemailutils.java

📁 本软件使用Java语言提供的JavaMail包进行开发
💻 JAVA
字号:
package com.lanx.app.mail.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import com.lanx.app.mail.Cache;
import com.lanx.app.mail.CacheImpl;
import com.lanx.app.mail.entity.MailInfo;

public class AgileMailUtils {			
	private static Cache cache = CacheImpl.newInstance();

	/**
	 * 根据配置文件中item项的priority属性转换为线程的优先级
	 * 如果不能够根据它判断,那么缺省返回线程的normal级别
	 * 
	 * @param itemPriority item项的priority属性值
	 * 
	 * @return int 线程的优先级
	 * */
	public static int getPriority(String itemPriority) {
		if("1".equals(itemPriority)){
			return AgileMailConstants.Priority.MAX_PRIORITY;
		}else if("2".equals(itemPriority)){
			return AgileMailConstants.Priority.NORM_PRIORITY;
		}else if("3".equals(itemPriority)){
			return AgileMailConstants.Priority.MIN_PRIORITY;
		}else{
			return AgileMailConstants.Priority.NORM_PRIORITY;
		}			
	}
	
	/**
	 * 根据输入的带逗号,或分号;的字符串,分解为字符串数组
	 * 
	 * @param splitStr 带逗号,或分号;的字符串
	 * 
	 * @return String[] 字符串数组
	 * */
	public static String[] split(String splitStr){
		if(splitStr == null || "".equals(splitStr)){
			return null;
		}
		
		String[] split = null;
		if(splitStr.indexOf(",") != -1){
			split = splitStr.split(",");
		}else if(splitStr.indexOf(";") != -1){
			split = splitStr.split(";");
		}else{
			split = new String[1];
			split[0] = splitStr;
		}
		
		return split;
	}
	
	/**
	 * 根据邮件正文和html文本,构成邮件正文字符串
	 * 
	 * @param mailInfo MailInfo类实例
	 * 
	 * @return String 邮件正文字符串
	 * @throws IOException 
	 * */
	public static String composeContent(MailInfo mailInfo) throws IOException {
		StringBuilder builder = new StringBuilder();
		composeContent(builder,mailInfo.getMailContent());

		String htmlPath = mailInfo.getHtmlPath();
		
		//使用cache
		String html = "";			
		if(cache.containsKey(htmlPath)){
			html = (String)cache.get(htmlPath);
		}else {
			html = AgileMailUtils.composeHtml(htmlPath);
			cache.add(htmlPath, html);
		}
		builder.append(html);		
		
		return builder.toString();
	}
	
	/**
	 * 将邮件正文转化为StringBuilder实例
	 * 
	 * @param builder StringBuilder实例
	 * @param content 邮件正文的文本内容
	 * 
	 * @return void
	 * */
	private static void composeContent(StringBuilder builder,String content){
		if(content != null){
			builder.append(content).append("<br/>");
		}		
	}
	
	/**
	 * 将html文本读入String,构成字符串实例
	 * 
	 * @param htmlPath html文本的路径
	 * 
	 * @return String
	 * @throws IOException 
	 * */
	public static String composeHtml(String htmlPath) throws IOException {	
		StringBuilder builder = new StringBuilder();
		if(htmlPath == null || "".equals(htmlPath)){
			return "";
		}
		
        FileInputStream fis = new FileInputStream(new File(htmlPath));            	
    	InputStreamReader in = new InputStreamReader(fis,"GBK");
		BufferedReader reader = new BufferedReader(in);
		String line = "";
		while((line = reader.readLine()) != null){
			builder.append(line);
		}
		
		reader.close();
		in.close();
		fis.close();
		
		return builder.toString();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -