📄 sendmail.java
字号:
// Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi
// Source File Name: FJMailBean.java
package cn.js.fan.mail;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.activation.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.servlet.ServletContext;
import javax.servlet.http.*;
import org.apache.log4j.Logger;
import sun.misc.BASE64Encoder;
import com.redmoon.kit.util.FileUpload;
import com.redmoon.kit.util.FileInfo;
import cn.js.fan.util.StrUtil;
// Referenced classes of package fjmail:
// PopupAuthenticator
public class SendMail {
public String host;
public String mailFooter;
public String mailFooterHTML;
boolean sessionDebug;
Message msg;
Multipart multipart;
Session session;
PopupAuthenticator popAuthenticator;
String username;
String password;
String errinfo;
int port = 25;
String subtype = "related"; // Construct a MimeMultipart object of the given subtype. A unique boundary string is generated and this string is setup as the "boundary" parameter for the contentType field.
String tempAttachFilePath;
Logger logger = Logger.getLogger(SendMail.class.getName());
public SendMail() {
host = "fserver";
mailFooter = ""; // "\n\n\n===========此邮件由Bluewind发送========\n\n";
mailFooterHTML = ""; // "<br><br><br>===========此邮件由Bluewind发送========<br><br>";
}
public void setmailFooter(String s) throws Exception {
mailFooter = s;
}
public void setmailFooterHTML(String s) throws Exception {
mailFooterHTML = s;
}
/**
* 以HTML的方式发送
* @param smtpServer String
* @param smtpPort int
* @param senderName String
* @param to String
* @param subject String
* @param content String
* @return boolean
*/
public boolean send(String smtpServer, int smtpPort, String smtpUser, String smtpPwd, String to, String from, String subject,
String content) {
clear();
// String user = "guest1";
// String pwd = "HTTP://elite2005";
boolean re = false;
try {
initSession(smtpServer, smtpPort, smtpUser, smtpPwd);
// from = StrUtil.GBToUnicode(from);
// from += "<fgf163@pub.zj.jsinfo.net>";
initMsg(to, from, subject, content, true);
re = send();
}
catch (Exception e) {
logger.error("send(,,,,):" + e.getMessage());
}
return re;
}
public void initSession(String host, int port, String username,
String password, String subtype) {
this.port = port;
this.host = host;
this.username = username;
this.password = password;
this.subtype = subtype;
try {
Properties properties = System.getProperties();
properties.put("mail.host", host);
properties.put("mail.transport.protocol", "smtp");
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.port", "" + port);
PopupAuthenticator popupauthenticator = new PopupAuthenticator();
popupauthenticator.init(username, password);
session = Session.getInstance(properties, popupauthenticator);
session.setDebug(sessionDebug);
msg = new MimeMessage(session);
msg.setSentDate(new Date());
if (subtype.equals(""))
multipart = new MimeMultipart();
else
multipart = new MimeMultipart(subtype);
msg.setContent(multipart);
} catch (Exception e) {
errinfo += e.getMessage();
logger.error("setFrom: " + e.getMessage());
}
}
public void initSession(String host, String username, String password,
String subtype) {
initSession(host, 25, username, password, subtype);
}
public void initSession(String host, String username, String password) throws
Exception {
initSession(host, 25, username, password, "");
}
public void initSession(String host, int port, String username, String password) throws
Exception {
initSession(host, port, username, password, "");
}
public void initMsg(String to, String from, String subject, String body,
boolean flag) {
setSendTo(to);
setFrom(from);
setSubject(subject);
setBody(body, flag);
}
/**
* 初始化信息
* @param as
* @param s
* @param s1
* @param s2
* @param flag 是否用HTML的方式发信
* @throws java.lang.Exception
*/
public void initMsg(String to[], String from, String subject, String body,
boolean flag) throws Exception {
setSendTo(to);
setFrom(from);
setSubject(subject);
setBody(body, flag);
}
public void initMsg(String to, String subject, String body, boolean flag) throws
Exception {
setSendTo(to);
setSubject(subject);
setBody(body, flag);
}
public void setFrom(String from) {
try {
msg.setFrom(new InternetAddress(from));
} catch (Exception e) {
errinfo += e.getMessage();
logger.error("setFrom: " + e.getMessage());
}
}
void setSendTo(String to[]) {
for (int i = 0; i < to.length; i++)
setSendTo(to[i]);
}
void setSendTo(String to) {
try {
InternetAddress ainternetaddress[] = {
new InternetAddress(to)
};
msg.setRecipients(javax.mail.Message.RecipientType.TO,
ainternetaddress);
} catch (Exception e) {
errinfo += e.getMessage();
logger.error("setSendTo: " + e.getMessage());
}
}
void setCopyTo(String to[]) throws Exception {
for (int i = 0; to != null && i > to.length; i++)
setCopyTo(to[i]);
}
void setCopyTo(String to) throws Exception {
InternetAddress ainternetaddress[] = {
new InternetAddress(to)
};
msg.setRecipients(javax.mail.Message.RecipientType.CC, ainternetaddress);
}
//JavaMail中的邮件主题需要进行BASE64编码,格式形如:
//=?GB2312?B?xPq1xMPcwuvS0b6t1tjWw6Osx+u+ob/stcfCvKOssqLQ3rjEw9zC66Oh?=
//所以,直接使用msg.setSubject("中文主题"),或者msg.setSubject("中文主题".getBytes("8859_1"), "GB2312"))都一样会出现乱码。在设置邮件主题前需要将主题字串的字节编码为BASE64格式,并添加编码头
void setSubject(String subject) {
try {
BASE64Encoder base64encoder = new BASE64Encoder();
msg.setSubject("=?GB2312?B?" +
base64encoder.encode(subject.getBytes()) +
"?=");
} catch (Exception e) {
errinfo += e.getMessage();
logger.error("setSubject: " + e.getMessage());
}
}
/**
* 置正文类型
* @param s
* @param flag 如果为真,则表示用HTML的方式
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -