📄 showmail.java
字号:
package ch08.section08;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ShowMail
extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
public void init() throws ServletException {
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
javax.servlet.http.HttpSession session = request.getSession();
MailUser mailUser = (MailUser) session.getAttribute("_mailUser");
String s_id = request.getParameter("id");
int msg = Integer.parseInt(s_id);
MailInfo mailInfo = new MailInfo();
try {
Folder inbox = mailUser.getInbox();
Message[] msgs = inbox.getMessages();
Message m = msgs[msg];
mailInfo.setM_Sent(m.getSentDate());
Address a[] = m.getFrom();
mailInfo.setM_From(PubPro.formatAddresses(a));
a = m.getRecipients(Message.RecipientType.TO);
mailInfo.setM_To(PubPro.formatAddresses(a));
String s = m.getSubject();
if (s == null) {
s = "";
}
mailInfo.setM_Subject(s);
Object o = m.getContent();
String s_msg = "";
if (m.isMimeType("text/plain")) {
s_msg = (String) o;
}
else if (m.isMimeType("multipart/*")) {
Multipart mp = (Multipart) o;
for (int j = 0; j < mp.getCount(); j++) {
Part part = mp.getBodyPart(j);
String contentType = part.getContentType();
if (contentType == null) {
out.println("Bad content type for part " + j);
continue;
}
ContentType ct = new ContentType(contentType);
if (ct.match("text/plain")) {
s_msg += (String) part.getContent();
}
else {
s = part.getFileName();
mailInfo.setM_AdjName(s);
}
}
}
else {
out.println(m.getContentType());
}
mailInfo.setM_Number(msg);
mailInfo.setM_Message(s_msg);
request.setAttribute("_mailInfo", mailInfo);
ServletContext sc = getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher(
"/ch08/section08/MailInfo.jsp");
rd.forward(request, response);
}
catch (MessagingException ex) {
out.println("错误: " + ex.getMessage());
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
public void destroy() {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -