📄 sendmail.java
字号:
package com.doone.fj1w.fjmgr.sysmgr;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Properties;
import java.util.Random;
import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import com.doone.fj1w.common.MailUtil;
import com.doone.fj1w.fj1w.login.app.ConfigSubFacade;
import com.doone.iossp.FormBody;
import com.doone.util.ExtString;
import com.doone.util.FileLogger;
/**
* <p>
* Title: 注册模块
* </p>
*
* <p>
* Description: 发送邮件给注册用户
* </p>
*
* <p>
* Copyright: Copyright (c) 2005
* </p>
*
* <p>
* Company: new doone
* </p>
*
* @author :吴少林
* @version 1.0
* @modifyHistory version author modify_Date Comment 1.1 曾忠诚 2005-7-26
* 调用发送电子邮件公共类实现电子邮件发送。
*
*
*/
public class SendMail {
public static final String T1 = "1";
public static final String T2 = "2";
public static final String T3 = "3";
public static final String T4 = "4";
public static final String T5 = "5";
public static final String T6 = "6";
public static final String T7 = "7";
public static final String TT1 = "福建电信网上客户服务中心注册用户激活帐号通知书";
public static final String TT2 = "福建电信网上客户服务中心注册用户产品关联成功通知书";
public static final String TT3 = "福建电信网上客户服务中心注册用户撤销产品关联通知书";
public static final String TT4 = "福建电信网上客户服务中心注册用户取回密码通知书";
public static final String TT5 = "福建电信网上客户服务中心注册用户帐号注销通知书";
public static final String TT7 = "福建电信网上客户服务中心用户帐号自动注销通知书";
private FileLogger _logger = new FileLogger();
private static String active_url = "";
/** 获得域名. */
private static String DNS = "";
/** 邮件服务器主机. */
private static String POSTHOST = "";
/** 邮件服务器端口. */
private static String POSTPORT = "";
private static String LOCAL_ADDRESS = "";
public SendMail() {
try {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
InputStream is = cl.getResourceAsStream("Email.properties");
Properties dbProps = new Properties();
dbProps.load(is);
active_url = (String) dbProps.getProperty("URL");
DNS =(String)dbProps.getProperty ( "Dns" );
DNS = DNS.trim();
LOCAL_ADDRESS = (String)dbProps.getProperty("AD");
dbProps.clear();
is.close();
} catch (Exception ex) {
}
}
/**
* 获取配置邮件服务器主机IP.
*
* @return
*/
private static String getPostHost() {
try {
if (POSTHOST == null || POSTHOST.equals("")) {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
InputStream is = cl.getResourceAsStream("Email.properties");
Properties dbProps = new Properties();
dbProps.load(is);
POSTHOST = (String) dbProps.getProperty("host");
POSTHOST = POSTHOST.trim();
dbProps.clear();
is.close();
}
return POSTHOST;
} catch (Exception ex) {
FileLogger.getLogger().warn("在Email.properties文件没有host属性,请配制.");
}
return null;
}
/**
* 获取配置邮件服务器端口.
*
* @return
*/
private static String getPostPort() {
try {
if (POSTPORT == null || POSTPORT.equals("")) {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
InputStream is = cl.getResourceAsStream("Email.properties");
Properties dbProps = new Properties();
dbProps.load(is);
POSTPORT = (String) dbProps.getProperty("port");
POSTPORT = POSTPORT.trim();
dbProps.clear();
is.close();
}
return POSTPORT;
} catch (Exception ex) {
FileLogger.getLogger().warn("在Email.properties文件没有port属性,请配制.");
}
return null;
}
/**
*
* @param URL
* @param address
* @param topic
* @param accountId
* 用户帐号
* @param flag
* 标志写入
* @return
*/
public static boolean sendMail(String URL, String address, String topic,
String accountId, String flag) {
try {
if (ExtString.isEmpty(URL)){return false;}
SendMail mail = new SendMail();
String body = mail.MailBody(URL);
if (ExtString.isEmpty(body))
return false;
if (ExtString.isEmpty(address))
return false;
if (mail.sendMail(address, topic, body, "")) {
FormBody body2 =new FormBody();
body2.append("EMAIL",address);
body2.append("ACCOUNTID",accountId);
body2.append("TITLE",flag);
body2.append("CONTEXT",body);
ConfigSubFacade.setTrackEMail(body2);
return true;
}
} catch (RuntimeException ss) {
FileLogger.getLogger().warn(ss.getMessage(), ss);
}
return false;
}
/**
* 获取邮件发送的web根路径.
*
* @param request
* @return
*/
public static String getWebRootPath(HttpServletRequest request) {
StringBuffer buffer = new StringBuffer();
buffer.append("http://");
String host = getPostHost();
if (ExtString.isEmpty(host)) {
host = "134.128.67.51";
}
buffer.append(host);
buffer.append(":");
String port = getPostPort();
if (ExtString.isEmpty(port)) {
port = "80";
}
buffer.append(port);
if (request.getContextPath().length() == 0) {
buffer.append("/");
} else {
buffer.append("/");
buffer.append(request.getContextPath());
buffer.append("/");
}
return buffer.toString();
}
/**
* 没有根名
* @return
*/
public static String getWebRootPath() {
StringBuffer buffer = new StringBuffer();
buffer.append("http://");
String host = getPostHost();
if (ExtString.isEmpty(host)) {
host = "134.128.67.51";
}
buffer.append(host);
buffer.append(":");
String port = getPostPort();
if (ExtString.isEmpty(port)) {
port = "80";
}
buffer.append(port);
return buffer.toString();
}
public static String getDns(){
return DNS;
}
public static String getURL() {
return active_url;
}
public static String getLocal_Address(){
return LOCAL_ADDRESS;
}
/**//**
* 利用BASE64Encoder算法解密
*
* @param s
* String 需要解密的字符串
* @return String 解密后的字符串
* @throws Exception
*/
public static String eryEncoder(String cipter) {
String ret = null;
if (cipter == null || cipter.equals(""))
throw new RuntimeException("密码不能为空。");
BASE64Encoder base64 = new BASE64Encoder();
ret = base64.encode(cipter.getBytes());
return ret;
}
public static String eryDecoder(String cipter) {
if (cipter == null || cipter.equals(""))
throw new RuntimeException("密码不能为空。");
try {
BASE64Decoder base64 = new BASE64Decoder();
byte[] decode = base64.decodeBuffer(cipter);
return new String(decode);
} catch (java.io.IOException rd) {
}
return "";
}
public static String getRandomNumber(int iLength) {
try {
String[] m_srcStr = new String[] {
"abcdefghijklmnopqrstuvwxyz67890",
"ABCDEFGHIJKLMNOPQRSTUVWXYZ12345",
"ABCDEFGHIJKLMNOPklmnopqrstuvwx7" };
int len = 6;
if (iLength >= 3)
len = iLength;
Random m_rnd = new Random();
byte[] m_b = new byte[len];
m_rnd.nextBytes(m_b);
String m_pwdStr = "";
for (int i = 0; i < len; i++) {
int startIdx = Math.abs((int) m_b[i] % 31);
m_pwdStr += m_srcStr[i % 3].substring(startIdx, startIdx + 1);
}
return m_pwdStr;
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
/**
* 发送邮件
*
* @param toAddr
* String
* @param subject
* String
* @param body
* String
* @param mime
* String
*/
public boolean sendMail(String toAddr, String subject, String body,
String mime) {
MailUtil mail = new MailUtil();
mail.initSetting(toAddr, subject, body);
// mail.setAffixPath("C:/BssSysFLD"); //添加附件
try {
mail.smtp();
return true;
} catch (MessagingException ex) {
_logger.debug("邮件发送不成功!");
}
return false;
}
/** 读入一张网页邮件 */
public String MailBody(final String url) {
StringBuffer result = new StringBuffer();
try {
URL l_url = new URL(url);
URLConnection l_connection = l_url.openConnection();
l_connection.setRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)");
BufferedReader in = new BufferedReader(new InputStreamReader(
l_connection.getInputStream(), "GBK"));
String line = "";
while ((line = in.readLine()) != null) {
try {
line = Replace(line);
} catch (Exception ex) {
}
result.append(line);
}
} catch (UnsupportedEncodingException exs) {
_logger.warn("读取网页编码不成功,本解释器不支持这种各式" + exs.getMessage());
} catch (IOException ex) {
_logger.warn("读取URL失败,没有找到URL: " + url);
}
return result.toString();
}
public static String Replace(String source) {
// source = replace(source, "<", "<");
// source = replace(source, ">", ">");
// source = replace(source, "\t", " ");
// source = replace(source, "'", "'");
// source = replace(source, "\"", """);
// source = replace(source, "\r\n","\n");
// source = replace(source, "\n", "<br>");
// source = replace(source, " ", "");
// source = replace(source, "\\", "/");
// source = replace(source, "\"", "'");
//
return source;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -