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

📄 sendmailopstep.java

📁 一个用java实现的发邮件的小程序
💻 JAVA
字号:
package com.icbc.inbs.operation;

import com.ibm.dse.base.*;
import com.icbc.inbs.service.*;

import com.icbc.inbs.base.*;
import java.text.*;
import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.StringTokenizer;
import com.ibm.dse.base.*;
import com.ibm.dse.cs.html.HtmlConstants;
import com.icbc.inbs.base.*;
public class SendMailOpStep extends OperationStep {
	private static int serial;
/**
 * SendMailOpStep constructor comment.
 */
public SendMailOpStep() {
	super();
}
/**
 * execute method comment.
 */
public int execute() throws Exception {

	try
	{
		String sendChannel =null;
		StringBuffer sb = new StringBuffer();
		try{
			sendChannel = (String)getParams().getValueAt("sendChannel");
		}catch(Exception e){
			sendChannel =  "Email";
		}
		
		SendMailService service = (SendMailService) this.getService("mailService");

		String content = "";

		String subject = (String) getParams().getValueAt("Subject");
		String formatName = (String) getParams().getValueAt("mailFormat");

		try
		{
			FormatElement formater = (FormatElement) getFormat(formatName);
			content = formater.format(getContext());
			//add to surpoort translation of multilanuage of file SunNing 2005-10-26
			StringTokenizer st = new StringTokenizer(content, "\r\n");
				
			for (; st.hasMoreTokens();) {
				String temp = st.nextToken();
				StringTokenizer st1 = new StringTokenizer(temp, ",",true);
				for (; st1.hasMoreTokens();) {
					String temp1 = st1.nextToken();
					if (temp1.trim().startsWith("icbc_download_convert")) {
					try{
						ResourceBundle rb =
							ResourceBundle.getBundle(
								"icbc",
								(Locale) Context
									.getCurrentContextForSession(
										(String) getValueAt(HtmlConstants
											.SESSIONID))
									.getElementAt(HtmlConstants.LOCALE)
									.getValue());
						temp1 = rb.getString(temp1.trim());
						byte[] b;
						try {
							b = temp1.getBytes("GBK");
						} catch (Exception e) {
							b = temp1.getBytes();
						}
						temp1=new String(b);
					}catch(Exception ee){
						temp1="";
					}
					//end of try-catch
					}
					sb.append(temp1);
				}
				sb.append("\r\n");
			}			
		} catch (Exception e)
		{
			setValueAt("TranErrorCode", "50066"); //数据格式化错误
			setValueAt("TranErrorDisplayMsg", TranFailException.getDisplayMessage("50066", (String)getValueAt("ieu")));
			setValueAt("TranErrorMsg", e.getMessage());
			setValueAt("TranErrorLocation", "SendMailOpStep.execute()");

		}
		//String fileName = saveFile(content);
		String fileName = saveFile(sb.toString());
		java.util.Vector files = new Vector(1);
		files.addElement(fileName);
		String addrListFile = getAddressListFile();
		if(addrListFile == null) 
			return -1;
		int retCode;
		String[] sendCh = new String[]{sendChannel};
		
		retCode = service.sendMail(sendCh,addrListFile,subject,"",files);

	

		if( retCode == 0)
			return 0;
		else
		{
			setValueAt("TranErrorCode","50067");	//邮件发送失败
			setValueAt("TranErrorDisplayMsg", TranFailException.getDisplayMessage("50067", (String)getValueAt("ieu")));
			return 1;		
		}
		
		
	} catch (TranFailException e){
		setValueAt("TranErrorCode","50069");	//写文件错误
		setValueAt("TranErrorDisplayMsg", TranFailException.getDisplayMessage("50069", (String)getValueAt("ieu")));
		setValueAt("TranErrorMsg", e.getMessage());
		setValueAt("TranErrorLocation", "SendMail.execute()");		
		return 1;
	} catch (Exception e)
	{
		setValueAt("TranErrorCode","50067");	//邮件发送失败
		setValueAt("TranErrorDisplayMsg", TranFailException.getDisplayMessage("50067", (String)getValueAt("ieu")));
		setValueAt("TranErrorMsg", e.getMessage());
		setValueAt("TranErrorLocation", "SendMail.execute()");	
		return 1;
	}
}
	/**
	 * 得到系统日期,以YYYYMMDD格式返回
	 * 返回值
	 * 正常:系统日期(YYYYMMDD格式)
	 * 异常:""
	 */

	private String getSysDate() {
		try{
			SimpleDateFormat dtFmt = new SimpleDateFormat("yyyyMMdd");
			Date dt = new Date();
			return(dtFmt.format(dt));
		}catch(Exception e){
			return "";	
		}		
	}
/**
 * Insert the method's description here.
 * Creation date: (2001-1-12 20:16:32)
 * @param content java.lang.String
 * @exception java.io.IOException The exception description.
 */
private String saveFile(String content) throws TranFailException {
	try
	{
		String fileName = "";
		String rootPath = "";
		fileName = (String) getParams().getValueAt("fileName");
		rootPath=(String)Settings.getSettings().getValueAt("ICBCSettings.fileRootPath");
		
		rootPath=rootPath + "mail" + System.getProperty("file.separator");
		int idx = fileName.indexOf(".");
		if (idx != -1)
		{
			String preName = fileName.substring(0, idx);
			String sub = fileName.substring(idx);
			fileName = preName + getSysDate() + sub;
		} else
		{
			fileName = fileName + getSysDate();
		}

		fileName = rootPath + fileName;
		java.io.FileWriter fw = new java.io.FileWriter(fileName);
		fw.write(content);
		fw.close();
		return fileName;
	} catch (Exception e)
	{
		throw new TranFailException(
			"22117", 
			"SendMailOpStep.saveFile(String)", 
			e.getMessage()); 

	}
}
/**
 * Insert the method's description here.
 * Creation date: (2001-1-12 20:16:32)
 * @param content java.lang.String
 * @exception java.io.IOException The exception description.
 */
private String getAddressListFile() throws TranFailException {
	String email = "";
	try
	{
		email = (String)getValueAt("email");
		//email = "sunn80@sina.com";
		
	} catch (Exception ee)
	{
	}
	if(email == null || email.trim().length() == 0){
		try{
		//email = (String)getValueAt("emai");
		}catch(Exception e){
			return null;
		}
	}
	try
	{
		String fileName = "mailAddr";
		String rootPath = null;
		rootPath=(String)Settings.getSettings().getValueAt("ICBCSettings.fileRootPath");

		rootPath=rootPath + "mail" + System.getProperty("file.separator");		
		int idx = fileName.indexOf(".");
		if (idx != -1)
		{
			String preName = fileName.substring(0, idx);
			String sub = fileName.substring(idx);
			fileName = preName + getSysDate() + sub;
		} else
		{
			fileName = fileName + getSysDate();
		}

		fileName = rootPath + fileName;
		java.io.FileWriter fw = new java.io.FileWriter(fileName);
		fw.write(email + ";");
		fw.close();
		return fileName;
	} catch (Exception e)
	{
		throw new TranFailException(
			"22117", 
			"SendMailOpStep.getAddressListFile()", 
			e.getMessage()); 

	}
}
}

⌨️ 快捷键说明

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