当前位置: 代码迷 >> Java Web开发 >> JavaMail接收邮件的有关问题?——请高手指教解决
  详细解决方案

JavaMail接收邮件的有关问题?——请高手指教解决

热度:46   发布时间:2016-04-17 12:46:22.0
JavaMail接收邮件的问题?——请高手指教解决
/**
 *module:UEMailServerManagementAction
 *description:action
 */
/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package org.luonix.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.luonix.struts.form.UEMailServerManagementForm;

/** 
 * MyEclipse Struts
 * Creation date: 09-27-2007
 * 
 * XDoclet definition:
 * @struts.action path="/uEMailServerManagement" name="uEMailServerManagementForm" input="/form/uEMailServerManagement.jsp" scope="request" validate="true"
 * @struts.action-forward name="errors" path="/errors.jsp" redirect="true"
 */

//public ActionForward executeLogin(ActionMapping mapping, ActionForm form,
// HttpServletRequest request, HttpServletResponse response)用于登录邮件服务器,并接收//电子邮件
public class UEMailServerManagementAction 
extends org.apache.struts.actions.DispatchAction {
/*
* Generated Methods
*/

/** 
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward executeConnect(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
UEMailServerManagementForm uEMailServerManagementForm = (UEMailServerManagementForm) form;// TODO Auto-generated method stub

return null;
}
public ActionForward executeLogin(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
UEMailServerManagementForm uEMailServerManagementForm = (UEMailServerManagementForm) form;// TODO Auto-generated method stub
String host = "127.0.0.1";
//org.apache.struts.action.ActionForward forward = new 
//org.apache.struts.action.ActionForward();
//forward.setPath("/secondAction?method=mymethod");
String luonixid = uEMailServerManagementForm.getLuonixid();
String password = uEMailServerManagementForm.getPassword();
String protocol = "pop3";
java.util.Properties properties = java.lang.System.getProperties();
try{
javax.mail.Session session = 
javax.mail.Session.getInstance(properties, null);
session.setDebug(true);
javax.mail.Store store = null;
store = session.getStore(protocol);
store.connect(host, luonixid, password);
//store.close();
////
javax.mail.Folder folder = store.getDefaultFolder();
if(folder == null) throw new Exception("no default folder");
folder = folder.getFolder("INBOX");
if(folder == null) throw new Exception("NO POP3 INBOX"); 
////
//javax.mail.Folder folder = store.getFolder("INBOX");
System.out.println("邮箱打开了吗 " + folder.isOpen());
if(!folder.isOpen())
folder.open(javax.mail.Folder.READ_ONLY);
System.out.println("邮箱打开了吗 " + folder.isOpen());
//folder.notifyAll();
//folder.open(javax.mail.Folder.READ_ONLY);
System.out.println("folder.getMessageCount:" + 
folder.getMessageCount());
javax.mail.Message message[] = folder.getMessages();
int size = message.length;
if(size == 0){
System.out.println("你的收件夹里没有邮件");
javax.servlet.RequestDispatcher rd = 
request.getRequestDispatcher("errors.jsp");