上传文件页面sendAttachmentMail.jsp
<form id="form1" name="form1" method="post" action="<%=request.getContextPath()%>/servlet/sendAttachmentMail" enctype="multipart/form-data">
<table width="516" height="253" border="0" align="center">
<tr>
<td>收件人:</td>
<td><label>
<input type="text" name="to" id="to" />
</label></td>
</tr>
<tr>
<td>发件人:</td>
<td><label>
<input type="text" name="from" id="from" />
</label></td>
</tr>
<tr>
<td>主题:</td>
<td><label>
<input type="text" name="subject" id="subject" />
</label></td>
</tr>
<tr>
<td>选择附件:</td>
<td><label>
<input type="file" name="attachment" id="file" />
</label></td>
</tr>
<tr>
<td>内容:</td>
<td><label>
<textarea name="content" id="content" cols="45" rows="8"></textarea>
</label></td>
</tr>
<tr>
<td><label>
<input type="submit" name="button" id="button" value="提交" />
</label></td>
<td><label>
<input type="reset" name="button2" id="button2" value="重置" />
</label></td>
</tr>
</table>
</form>
web.xml中添加servlet信息
<servlet>
<servlet-name>SendAttachmentMailServlet</servlet-name>
<servlet-class>com.email.SendAttachmentMailServlet</servlet-class>
<init-param>
<param-name>savePath</param-name>
<param-value>uploads</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>SendAttachmentMailServlet</servlet-name>
<url-pattern>/servlet/sendAttachmentMail</url-pattern>
</servlet-mapping>
Servlet类SendAttachmentMailServlet.java中涉及到附件,所以有个上传文件的方法doAttachment,及初始化方法得到文件保存路径参数!!
/**
* 通过web邮箱服务器发送邮件
*
* @author lrq413
*
*/
public class SendAttachmentMailServlet extends HttpServlet {
private static final long serialVersionUID = -1408700020471642036L;
//定义上下文sc、文件保存路径savePath,表单内容parameters
private ServletContext sc;
private String savePath;
private Map<String, String> parameters = new HashMap<String, String>();
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
/**
* 处理表单信息,并发送邮件
*
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("doPost");
// 处理请求页面中文字符集
request.setCharacterEncoding("gbk");
response.setContentType("text/html; charset=gbk");
//得到表单中附件
File file = this.doAttachment(request);
//包含附件功能的邮件定义
MultiPartEmail email = new MultiPartEmail();
//设置邮件发送的服务器
email.setHostName("smtp.163.com");
//邮件发送验证用户名,密码
email.setAuthentication("lrq41234", "8259698");
//设置邮件接收字符集
email.setCharset("gbk");
try {
//设置邮件信息,从表单中取得邮箱发送人、接收人、主题、内容
email.addTo(parameters.get("to"));
email.setFrom(parameters.get("from"));
email.setSubject(parameters.get("subject"));
email.setMsg(parameters.get("content"));
if (file != null) {
//邮件添加附件,设置邮件附件的路径、类型、名称
EmailAttachment attachment = new EmailAttachment();
attachment.setPath(file.getPath());
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setName(file.getName());
email.attach(attachment);
}
//发送邮件
email.send();
request.setAttribute("sendmail.message", "success");
} catch (EmailException e) {
e.printStackTrace();
request.setAttribute("sendmail.message", "error");
}
//转发到结果页面
request.getRequestDispatcher("/sendResult.jsp").forward(request,
response);
}
/**
* 附件上传
*
* @param request
* @return File
* @throws ServletException
* @throws IOException
*/
public File doAttachment(HttpServletRequest request)
throws ServletException, IOException {
File file = null;
//定义文件上传相关对象实例
request.setCharacterEncoding("gbk");
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try {
//从请求表单中得到文件上传信息
List items = upload.parseRequest(request);
Iterator it = items.iterator();
while (it.hasNext()) {
FileItem item = (FileItem) it.next();
//将表单中信息存入parameters集合中,Map集合中存放表单名/值
if (item.isFormField()) {
parameters
.put(item.getFieldName(), item.getString("gbk"));
System.out.println("parameters = " + parameters);
} else {
//文件不为空,上传文件
if (item.getName() != null && !item.getName().equals("")) {
File tempFile = new File(item.getName());
file = new File(sc.getRealPath("/") + savePath,
tempFile.getName());
item.write(file);
}
}
}
} catch (Exception e) {
e.printStackTrace();
request.setAttribute("upload.message", "成功");
}
return file;
}
/**
* 初始化方法,
*/
public void init(ServletConfig config) {
//从配置文件中取得初始化参数
savePath = config.getInitParameter("savePath");
sc = config.getServletContext();
}
}
该实现方法是将附件上传到服务器中,再从服务器中将邮件并附件一起发出!!!最好是对方发送完邮件后,将附件信息删除!!
分享到:
相关推荐
用 apache commons email 发送带附件,HTML 格式的 邮件 格式例子 还包括commons-email的jar包,及其他相关资料
基于Java调用邮件系统所需要的jar包,commons-email-1.5.jar,javax.mail-1.4.5.jar,activation.jar
赠送jar包:commons-email-1.5.jar; 赠送原API文档:commons-email-1.5-javadoc.jar; 赠送源代码:commons-email-1.5-sources.jar; 赠送Maven依赖信息文件:commons-email-1.5.pom; 包含翻译后的API文档:...
commons-email-1.5-src.zip 借助commons-email简化发送邮件的过程
集成邮件发送所需要的四个jar包,activation.jar、additionnal.jar、commons-email-1.4.jar、mail.jar
1)将文件放入libs后,在gradle文件里加入implementation files('libs/commons-email-1.5.jar') 2)android收发邮件会要求引入此jar包
包含邮件发送的jar包,同时添加实现过程文章的连接,个人觉着操作简单,可以轻松实现邮件发送功能。啦啦啦啦啦啦啦啦啦
邮件开发包,email包,是开发web常用包
jar包大小:135KB commons-validator-1.3.1.jar jar包大小:93KB commons-transaction-1.2.jar jar包大小:141KB commons-scxml-0.6.jar jar包大小:254KB commons-primitives-1.0.jar jar包大小:60KB commons-pool-1.3....
NULL 博文链接:https://liangq.iteye.com/blog/1222307
commons-email-1.4.jarcommons-email-1.4.jarcommons-email-1.4.jar
开发工具 commons-fileupload-1.3.2开发工具 commons-fileupload-1.3.2开发工具 commons-fileupload-1.3.2开发工具 commons-fileupload-1.3.2开发工具 commons-fileupload-1.3.2开发工具 commons-fileupload-1.3.2...
Java发送邮件所需要的几个JAR包,打包发给大家,包含以下内容: commons-email-1.1.jar mail.jar additionnal.jar activation.jar
activation.jar commons-email-1.2.jar mail.jar免费下载
用commons-email-1.2.jar 发送邮件的三个jar 文件
commons-email-1.4
jsp上传组件包含commons-fileupload.jar和commons-io.jar,目前最流行的jsp上传组件
commons-email封装了邮件发送权限处理,AuthenticationFailedException错误可以解决了
commons-logging-1.0-javadoc.jar, commons-logging-1.0.1-javadoc.jar, commons-logging-1.0.1.jar, commons-logging-1.0.2-javadoc.jar, commons-logging-1.0.2.jar, commons-logging-1.0.3-javadoc.jar, commons-...
赠送jar包:commons-email-1.5.jar; 赠送原API文档:commons-email-1.5-javadoc.jar; 赠送源代码:commons-email-1.5-sources.jar; 赠送Maven依赖信息文件:commons-email-1.5.pom; 包含翻译后的API文档:...