?? showmail.java
字號:
package ch08.section10;
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=gb2312";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
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 {
// Get the inbox
Folder inbox = mailUser.getInbox();
// Get the messages from the inbox
Message[] msgs = inbox.getMessages();
// Get the requested message
Message m = msgs[msg];
// Show the date
mailInfo.setM_Sent(m.getSentDate());
// Show the from addresses.
Address a[] = m.getFrom();
mailInfo.setM_From(PubPro.formatAddresses(a));
// Show the to addresses
a = m.getRecipients(Message.RecipientType.TO);
mailInfo.setM_To(PubPro.formatAddresses(a));
// Show the subject
String s = m.getSubject();
if (s == null) {
s = "";
}
mailInfo.setM_Subject(s);
// Display the message
Object o = m.getContent();
String s_msg = "";
if (m.isMimeType("text/plain")) {
s_msg = (String) o;
}
else if (m.isMimeType("multipart/*")) {
// Multi-part message
Multipart mp = (Multipart) o;
// Loop through the parts
for (int j = 0; j < mp.getCount(); j++) {
Part part = mp.getBodyPart(j);
// Get the content type of this part
String contentType = part.getContentType();
if (contentType == null) {
out.println("Bad content type for part " + j);
continue;
}
ContentType ct = new ContentType(contentType);
// Plain text part
if (ct.match("text/plain")) {
s_msg += (String) part.getContent();
}
}
}
else {
// Unknown MIME type
out.println(m.getContentType());
}
mailInfo.setM_Message(s_msg);
request.setAttribute("_mailInfo", mailInfo);
ServletContext sc = getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher(
"/ch08/section10/MailInfo.jsp");
rd.forward(request, response);
}
catch (MessagingException ex) {
out.println("錯誤: " + ex.getMessage());
}
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
//Clean up resources
public void destroy() {
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -