3.Email发送工具类
- package com.util;
- import Java.util.ArrayList;
- import java.util.List;
- import java.util.Properties;
- import javax.mail.Authenticator;
- import javax.mail.Message;
- import javax.mail.PasswordAuthentication;
- import javax.mail.Session;
- import javax.mail.Transport;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeMessage;
- import javax.mail.internet.MimeUtility;
- import javax.mail.internet.MimeMessage.RecipientType;
- /**
- * MailUtil
- * @author Administrator
- *
- */
- publicclass MailUtils {
- private String MAIL_NAME = "****";
- private String MAIL_PASSWORD = "******";
- private String mailHost = "smtp.163.com";
- private String MAIL_FROM = "******@163.com";
- private String MAIL_FROM_NAME = "****";
- public MailUtils(){
- }
- public MailUtils(String username,String password,String mailHost,String mailFrom,String mailFromName ){
- this.MAIL_NAME = username;
- this.MAIL_PASSWORD = password;
- this.mailHost = mailHost;
- this.MAIL_FROM = mailFrom;
- this.MAIL_FROM_NAME = mailFromName;
- }
- /**
- * 邮箱认证
- * @param userAuth
- * @param mailType
- * @return
- * @throws Exception
- */
- public Session mailAuth() throws Exception{
- Properties props = new Properties();
- props.setProperty("mail.smtp.auth", "true");
- props.setProperty("mail.transport.protocol", "true");
- props.setProperty("mail.host", mailHost);
- Session session = Session.getDefaultInstance(props,new Authenticator(){
- @Override
- protected PasswordAuthentication getPasswordAuthentication() {
- returnnew PasswordAuthentication(MAIL_NAME,MAIL_PASSWORD);
- }
- });
- return session;
- }
- /**
- * @param subject
- * @param content
- * @param mailTo
- * @throws Exception
- */
- publicvoid sendSimpleMail(String subject,String content,String mailTo) throws Exception{
- Session session = mailAuth();
- session.setDebug(true);
- Message msg = new MimeMessage(session);
- msg.setFrom(new InternetAddress(MimeUtility.encodeText(MAIL_FROM_NAME)+"<"+MAIL_FROM+">" ));
- msg.setSubject(subject);
- msg.setText(content);
- msg.addRecipient( RecipientType.TO, new InternetAddress(mailTo));
- Transport.send(msg);
- }
- publicvoid sendMutilHtmlMail(String subject,String content,List<String> mailto) throws Exception{
- if(mailto != null){
- for(int i = 0 ; i < mailto.size() ; i++){
- sendHtmlMail(subject, content,mailto.get(i));
- }
- }
- }
- }
4.生成日志工具类
- package com.util;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.OutputStreamWriter;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- publicclass LogUtil {
- privatestatic String FILE_PATH = "/usr/local/tomcatLook/";
- publicstaticvoid writerINF(String info){
- File file = null;
- File filedir = null;
- BufferedWriter writer = null;
- Date date = new Date();
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- SimpleDateFormat sdf2 = new SimpleDateFormat("MM-dd_HH:mm:ss");
- try {
- filedir = new File(FILE_PATH);
- if(!filedir.exists()){
- filedir.mkdirs();
- }
- file = new File(FILE_PATH+"domain."+sdf.format(date)+".log");
- if(file.exists()){
- file.createNewFile();
- }
- writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
- writer.write("["+sdf2.format(date)+"]-[INFO]:"+info+"\n");
- writer.flush();
- } catch (IOException e) {
- } finally{
- if(writer != null){
- try {
- writer.close();
- } catch (IOException e) {
- }
- }
- writer = null;
- file = null;
- filedir = null;
- date = null;
- sdf = null;
- sdf2 = null;
- }
- }
- publicstaticvoid writerE(Exception e){
- StringBuffer info = new StringBuffer();
- File file = null;
- File filedir = null;
- BufferedWriter writer = null;
- Date date = new Date();
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- SimpleDateFormat sdf2 = new SimpleDateFormat("MM-dd_HH:mm:ss");
- try {
- info.append("[").append(sdf2.format(date)).append("]-[Exception]:").append(e.getClass().getName()).append("\n\t").append(getExceptionString(e)).append("\n");
- filedir = new File(FILE_PATH);
- if(!filedir.exists()){
- filedir.mkdirs();
- }
- file = new File(FILE_PATH+"domain."+sdf.format(date)+".log");
- if(file.exists()){
- file.createNewFile();
- }
- writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
- writer.write(info.toString());
- writer.flush();
- } catch (IOException ioe) {
- } finally{
- if(writer != null){
- try {
- writer.close();
- } catch (IOException ioe2) {
- }
- }
- writer = null;
- file = null;
- filedir = null;
- date = null;
- sdf = null;
- }
- }
- /**
- * 返回处理后的异常信息
- * @param e 异常对象
- * @return 处理后的异常信息
- */
- privatestatic String getExceptionString(Exception e) {
- String errorString = e.toString() + "\r\n";
- StackTraceElement[] stackArr = e.getStackTrace();
- for (int i = 0; i < stackArr.length; i++) {
- errorString += "\tSTACK-->: " + stackArr[i].toString();
- if((i+1) != stackArr.length) errorString += "\r\n";
- }
- return errorString;
- }
- }
