javaEmail发送Email邮件

发送邮件调用类,详情请见附件!

package com.siit.service;

import java.util.ArrayList;
import java.util.List;
import java.util.TimerTask;

import org.apache.log4j.Logger;

import com.siit.dao.TInvoiceSapMailDAO;
import com.siit.pojo.invoiceSapMail.TInvoiceSapMail;
import com.siit.util.AttributeNames;
import com.siit.util.ConfigInfoUtil;
import com.siit.util.sendEmail.MailSender;
import com.siit.util.sendEmail.MailSenderInfo;


/**
 * @TODO:[系统定时向发票经办人发送提醒邮件]
 */
public class SysInvoiceSapMailBean extends TimerTask{
	
	private static Logger logger = Logger.getLogger(SysInvoiceSapMailBean.class.getName());
	
	@SuppressWarnings("unchecked")
	public void run() 
	{
		logger.info("The system automatically send invoice agent reminded mail,Auto Begin-----");
		System.out.println("The system automatically send invoice agent reminded mail,Auto Begin-----");
		ConfigInfoUtil util = new ConfigInfoUtil();
		if(util.isFalgMd5("IsSendInvoiceSapMailInfoFlag")){  // 开关,是否发送邮件
			logger.info("【Start sending mail】");
			System.out.println("【Start sending mail】");
			/** 设置邮件发送服务器 **/
			MailSenderInfo mailInfo = new MailSenderInfo();  				// 这个类主要是设置邮件
			mailInfo.setMailServerHost(AttributeNames.MAIL_SERVER_HOST);  	// 发送邮件的服务器的IP
			mailInfo.setMailServerPort(AttributeNames.MAIL_SERVER_POST);    // 发送邮件的服务器的端口
			mailInfo.setValidate(AttributeNames.MAIL_SERVER_VALIDATE);  	// 是否需要身份验证
			mailInfo.setUserName(AttributeNames.MAIL_USERNAME); 			// 登陆邮件发送服务器的用户名
			mailInfo.setPassword(AttributeNames.MAIL_PASSWORD);				// 登陆邮件发送服务器的密码
			mailInfo.setFromAddress(AttributeNames.MAIL_FORM_ADDRESS); 		// 邮件发送者的地址
			
			TInvoiceSapMailDAO daoObj = new TInvoiceSapMailDAO();
			List mailList = daoObj.getInvoiceSapMailInfo();
			List invoiceIdList = new ArrayList(); 	// 存放发送数据的id
			TInvoiceSapMail vo = null;
			for (int i = 0; i < mailList.size(); i++) {
				vo = new TInvoiceSapMail();
				vo = (TInvoiceSapMail)mailList.get(i);
				if(vo.getEmail()!=null && !"".equals(vo.getEmail())){
					mailInfo.setToAddress(vo.getEmail().trim());   // 邮件接收者的地址
					//mailInfo.setToAddress("[email protected]");   // TEST
					//mailInfo.setToAddress("[email protected]");   // TEST
					mailInfo.setSubject("通过制证的发票单据号:"+checkNull(vo.getInvoiceNumber()));	// 设置邮箱标题
					StringBuffer buffer = new StringBuffer("");
					buffer.append("<html><head>");
					buffer.append("<style type='text/css'>" +
							".STYLE1 {font-size: 13px; font-weight: bold;  bgcolor='#FFFFFF'}" +
							".STYLE3 {color: #707070; font-size: 12px; }" +
							"body {margin-top: 0px;margin-bottom: 0px;}</style></head>");
					buffer.append("<body>");
					buffer.append("<table width='100%' border='0' cellpadding='0' cellspacing='1' bgcolor='#c9c9c9'>" +
						     "<tr>" +
						        "<td height='30' colspan='2' class='STYLE1'>邮件内容</td>" +
							"</tr>" +
							"<tr>" +
						        "<td height='22' bgcolor='#FFFFFF' class='STYLE1' align='left'>发票单据号</td>" +
								"<td height='22' bgcolor='#FFFFFF' class='STYLE3' align='left'>"+checkNull(vo.getInvoiceNumber())+"</td>" +
							"</tr>" +
							"<tr>" +
						        "<td height='22' bgcolor='#FFFFFF' class='STYLE1' align='left'>供应商名称</td>" +
								"<td height='22' bgcolor='#FFFFFF' class='STYLE3' align='left'>"+checkNull(vo.getSupplierName())+"</td>" +
							"</tr>" +
							"<tr>" +
						        "<td height='22' bgcolor='#FFFFFF' class='STYLE1' align='left'>供应商编码</td>" +
								"<td height='22' bgcolor='#FFFFFF' class='STYLE3' align='left'>"+checkNull(vo.getSupplierCode())+"</td>" +
							"</tr>" +
							"<tr>" +
						        "<td height='22' bgcolor='#FFFFFF' class='STYLE1' align='left'>发票金额</td>" +
								"<td height='22' bgcolor='#FFFFFF' class='STYLE3' align='left'>"+checkNull(vo.getInvoiceMoney())+"</td>" +
						     "</tr>" +
						      "<tr>" +
						        "<td height='22' bgcolor='#FFFFFF' class='STYLE1' align='left'>合同号</td>" +
								"<td height='22' bgcolor='#FFFFFF' class='STYLE3' align='left'>"+checkNull(vo.getContractCode())+"</td>" +
						     "</tr>" +
							 "<tr>" +
						        "<td height='22' bgcolor='#FFFFFF' class='STYLE1' align='left'>发票校验的主体</td>" +
								"<td height='22' bgcolor='#FFFFFF' class='STYLE3' align='left'>"+checkNull(vo.getCompanyNo())+"</td>" +
						     "</tr>" +
							 "<tr>" +
						        "<td height='22' bgcolor='#FFFFFF' class='STYLE1' align='left'>凭证号</td>" +
								"<td height='22' bgcolor='#FFFFFF' class='STYLE3' align='left'>"+checkNull(vo.getSapCode())+"</td>" +
						     "</tr>" +
							 "<tr>" +
						        "<td height='22' bgcolor='#FFFFFF' class='STYLE1' align='left'>制证的会计</td>" +
								"<td height='22' bgcolor='#FFFFFF' class='STYLE3' align='left'>"+checkNull(vo.getOperatorAccountUser())+"</td>" +
						     "</tr>" +
							 "<tr>" +
								"<td height='60' bgcolor='#FFFFFF' colspan='2' align='right' class='STYLE3'>【影像系统】发送的提醒邮件,请勿回复!</td>" +
						     "</tr>" +
						    "</table>");
					buffer.append("</body>");
					buffer.append("</html>");
					//以文本信息形式发送
					/*buffer.append("邮件内容:").append("\n");
					buffer.append("发票单据号:").append(vo.getInvoiceNumber()).append("\n");
					buffer.append("供应商名称:").append(vo.getSupplierName()).append("\n");
					buffer.append("供应商编码:").append(vo.getSupplierCode()).append("\n");
					buffer.append("发票金额:").append(vo.getInvoiceMoney()).append("\n");
					buffer.append("合同号:").append(vo.getContractCode()).append("\n");
					buffer.append("发票校验的主体:").append(vo.getCompanyNo()).append("\n");
					buffer.append("凭证号:").append(vo.getSapCode()).append("\n");
					buffer.append("制证的会计:").append(vo.getOperatorAccountUser()).append("\n");*/
					//mailInfo.setContent(buffer.toString());	 // 设置邮箱内容
					//boolean flag = MailSender.sendTextMail(mailInfo);	// 发送文体格式
					
					mailInfo.setContent(buffer.toString());	 // 设置邮箱内容
					boolean flag = MailSender.sendHtmlMail(mailInfo);	// 发送html格式
					
					logger.info("【Mail delivery is successful】Flag="+flag);
					System.out.println("【Mail delivery is successful】Flag:"+flag);
					if(flag){ //发送成功
						invoiceIdList.add(vo.getId());
					}
				}
			}
			logger.info("【End of the email】successfully:"+invoiceIdList.size());
			System.out.println("【End of the email】successfully:"+invoiceIdList.size());
			
			/**修改 “发票制证邮件提醒表” 中已发送信息的发送状态*/
			daoObj.updateSendStatus(invoiceIdList);
		}
	}
	
	/**
	* @TODO:[空对象处理]
	* @author:TanShenghua
	* @date:2013-8-21
	* @param obj
	* @return String
	*/
	public static String checkNull(Object obj) {
		if ((null == obj) || ("null".equals(obj)) || ("".equals(obj))) {
			return "";
		} else {
			return obj.toString();
		}
	}
	
	public static void main(String args[]){
		SysInvoiceSapMailBean beanObj = new SysInvoiceSapMailBean();
		beanObj.run();
	}
}

 

你可能感兴趣的:(javaEmal、发邮件)