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

📄 getmail.java

📁 JAVA编程百例书中各章节的所有例子的源代码,包括套接字编程
💻 JAVA
字号:
package ch08.section10;

import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class GetMail
    extends HttpServlet {
  private static final String CONTENT_TYPE = "text/html; charset=GBK";
  MailUser mailUser;
  MailInfo mf;
  //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();
//    out.println("<html>");
//    out.println("<head><title>GetMail</title></head>");
//    out.println("<body bgcolor=\"#ffffff\">");
//    out.println("<p>The servlet has received a " + request.getMethod() + ". This is the reply.</p>");
//    out.println("</body></html>");
    String transport = request.getParameter("transport");
    String transportHost = request.getParameter("storeHost");
    String store = request.getParameter("store");
//    String storeHost = request.getParameter(FORM_STOREHOST);
    String user = request.getParameter("user");
    String password = request.getParameter("password");
    mailUser = new MailUser();
    try {

      mailUser.login(store, transportHost,
                     transport, null,
                     user, password);
    }
    catch (MessagingException ex) {
      out.println("登录错误:" + ex.getMessage());
      return;
    }
    javax.servlet.http.HttpSession session = request.getSession();
    session.setAttribute("_mailUser", mailUser);
    Vector v_mf = getMail();
    request.setAttribute("_mails", v_mf);
    ServletContext sc = getServletContext();
    RequestDispatcher rd = sc.getRequestDispatcher(
        "/ch08/section10/ListMail.jsp");
    rd.forward(request, response);

//    out.println("ok");
//    showInbox(request, response, mailUser);
  }

  private Vector getMail() {
    Vector v_mf = new Vector();
    try {

      // Get the inbox
      Folder inbox = mailUser.getInbox();

      // Get the number of messages in the inbox
      int n = inbox.getMessageCount();

      // Get the messages from the inbox
      Message[] msgs = inbox.getMessages();

      // Loop through the inbox
      for (int i = 0; i < n; i++) {
        Message m = msgs[i];

        // Skip deleted messages
        if (m.isSet(Flags.Flag.DELETED)) {
          continue;
        }
        mf = new MailInfo();
        // Show the from address
        Address from[] = m.getFrom();
        Address addr = null;
        if ( (from != null) && (from.length > 0)) {
          addr = from[0];
        }

        mf.setM_From(PubPro.getAddress(addr));
        // Show the sent date
        mf.setM_Sent(m.getSentDate());
        // Show the subject
        mf.setM_Subject(m.getSubject());
        mf.setM_Number(i);
        v_mf.add(mf);
      }
    }
    catch (MessagingException ex) {
      System.out.println("错误: " + ex.getMessage());
    }
    finally {
      return v_mf;
    }
  }

  /**
   * Gets the display address
   * @param address The mail address
   * @return The display name
   */

  //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 + -