📄 testmail.jsp
字号:
<%@ page contentType="text/html;charset=GB2312"%>
<%
request.setCharacterEncoding("gb2312");
%>
<%@ page import="com.sinosoft.mail.UploadFileDataSource"%>
<%@ page import="java.util.*,javax.mail.*"%>
<%@ page import="javax.mail.internet.*"%>
<%@ page import="javax.activation.*"%>
<!--要发送附件必须引入该库-->
<%@ page import="java.net.*"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<!--要用到URL类-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>发送成功</title>
</head>
<body>
<%
try {
if (DiskFileUpload.isMultipartContent(request)) {
DiskFileUpload fileupload = new DiskFileUpload();
//设置允许用户上传文件大小,单位:字节
fileupload.setSizeMax(5*1024*1024);//5M
//设置最多允许在内存中存储的数据,单位:字节
fileupload.setSizeThreshold(1024*1024);
//设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录
//在进行文件上传的时候文件先存再内存中,然后才会存到server上,
// 但是如果内存放不下那么大的文件
//就必须用硬盘上的 一个临时文件夹来保存这个文件的部分,然后转存
//现在默认的文件存储的路径是
String tempPath = System.getProperty("user.dir");
String temp = tempPath+System.getProperty("file.separator")+"temp";
fileupload.setRepositoryPath(temp);
fileupload.setHeaderEncoding("gb2312");
FileItem item = null;
Map map = new HashMap();
List fileList = new ArrayList();
try {
List inputList = fileupload.parseRequest(request);
for(Iterator i = inputList.iterator();i.hasNext();) {
item = (FileItem) i.next();
if (item.isFormField()) {
map.put(item.getFieldName(), item.getString());
} else {
if(!item.getName().equals("")){
fileList.add(item);
}
}
}
} catch (FileUploadBase.FileSizeLimitExceededException fse) {
out.println("附件不可超过5M");
} catch (FileUploadBase.UnknownSizeException use) {
out.println("附件大小不可知");
} catch (org.apache.commons.fileupload.FileUploadException fue) {
out.println("上传错误");
} catch (Exception e) {
out.println(e.toString());
}
String tto = (String) map.get("to");
String tfrom = (String) map.get("from");
String tcc = (String) map.get("cc");
String tbcc = (String) map.get("bcc");
String ttitle = (String) map.get("title");
String emailtype = (String) map.get("emailtype");// 获取email类型
String tcontent = (String) map.get("content");
String smtp = (String) map.get("smtp");
String username = (String) map.get("username");
String password = (String) map.get("password");
//System.out.println(smtp + "|" + username + "|" + password + "|"
// + tfrom);
Properties props = new Properties();// 也可用Properties props =
// System.getProperties();
props.put("mail.smtp.host", smtp);// 存储发送邮件服务器的信息
props.put("mail.smtp.auth", "true");// 同时通过验证
Session s = Session.getInstance(props);// 根据属性新建一个邮件会话
s.setDebug(true);
MimeMessage message = new MimeMessage(s);
// 给消息对象设置发件人/收件人/主题/发信时间
InternetAddress from = new InternetAddress(tfrom);
message.setFrom(from);
message.setRecipients(Message.RecipientType.TO,
(Address[]) InternetAddress.parse(tto));
message.setRecipients(Message.RecipientType.CC,
(Address[]) InternetAddress.parse(tcc));
message.setRecipients(Message.RecipientType.BCC,
(Address[]) InternetAddress.parse(tbcc));
message.setSubject(ttitle);
message.setSentDate(new Date());
Multipart mm = new MimeMultipart();// 新建一个MimeMultipart对象用来存放多个BodyPart对象
// 设置信件文本内容
BodyPart mdp = new MimeBodyPart();// 新建一个存放信件内容的BodyPart对象
mdp.setContent(tcontent, emailtype + ";charset=gb2312");// 给BodyPart对象设置内容和格式/编码方式
mm.addBodyPart(mdp);// 将含有信件内容的BodyPart加入到MimeMultipart对象中
// 设置信件的附件(用本地上的文件作为附件)
for (Iterator i = fileList.iterator(); i.hasNext();) {
item = (FileItem)i.next();
mdp = new MimeBodyPart();
String fname = item.getName();// 提取文件名
int ddd = fname.lastIndexOf("\\");
fname = fname.substring(ddd);
fname = new String(fname.getBytes("gb2312"), "ISO8859-1");// 处理文件名是中文的情况
mdp.setFileName(fname);// 可以和原文件名不一致,但最好一样
DataSource datasource = new UploadFileDataSource(item);
mdp.setDataHandler(new DataHandler(datasource));
mm.addBodyPart(mdp);
}
message.setContent(mm);// 把mm作为消息对象的内容
message.saveChanges();
Transport transport = s.getTransport("smtp");
transport.connect(smtp, username, password);// 以smtp方式登录邮箱
transport.sendMessage(message, message.getAllRecipients());
transport.close();
%>
<div align="center">
<p>
<font color="#FF6600">发送成功!</font>
</p>
<p>
<br>
<a href="index.html">再发一封</a>
</p>
</div>
<%
}else{
%>
<div align="center">
<p>
<font color="#FF6600">请将form的enctype设置为:"multipart/form-data"!</font>
</p>
</div>
<%
}
} catch (MessagingException e) {
out.println(e.toString());
}
%>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -