⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 showmail.java

📁 JAVA编程百例书中各章节的所有例子的源代码,包括套接字编程
💻 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 + -