从servlet调用google api

时间:2010-12-13 05:25:40

标签: java google-api

我试图从我的servlet命令调用getGmail,但一直收到错误,我不知道去哪了,有人可以帮忙吗?

public class GmailCommand implements Command
{
    public GmailCommand() { }

    public String execute(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException
    {

        try 
        {
            GetGmail.run(null, null);
        } 
        catch (Exception e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "/welcome.jsp";
    }
}

public class GetGmail 
{
    //public static void main(String[] args) throws Exception {
    public static ArrayList<String[]> run(String username, String password)
        throws Exception 
    {
        ArrayList<String[]> result = null;
        System.out.println("GetGmail");
        Properties props = System.getProperties();
        props.setProperty("mail.store.protocol", "imaps");
        props.setProperty("mail.imap.port", "993");
        props.setProperty("mail.imap.connectiontimeout", "5000");
        props.setProperty("mail.imap.timeout", "5000");

        try 
        {
            Session session = Session.getDefaultInstance(props, null);
            Store store = session.getStore("imaps");
            //store.connect("imap.googlemail.com", username, password);
            //sendMessage(session);
            result = readMessage(store);
            store.close();
        } 
        catch (NoSuchProviderException e) 
        {
            e.printStackTrace();
            System.exit(1);
        } 
        catch (MessagingException e) 
        {
            e.printStackTrace();
            System.exit(2);
        }

        //  for(String[] ss:result){
        //      System.out.println(ss[0]+ss[1]+ss[2]+ss[3]);
        //  }

        return result;
    }

这是错误消息

SEVERE: Servlet.service() for servlet dispatcher threw exception
java.lang.ClassNotFoundException: javax.mail.NoSuchProviderException
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1386)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1232)
at com.web.command.GmailCommand.execute(GmailCommand.java:35)
at com.web.commandController.ControllerServlet.processRequest(ControllerServlet.java:46)
at com.web.commandController.ControllerServlet.doPost(ControllerServlet.java:40)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:873)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
at java.lang.Thread.run(Thread.java:619)

1 个答案:

答案 0 :(得分:3)

  

java.lang.ClassNotFoundException:javax.mail.NoSuchProviderException

您需要在类路径中包含JavaMail jar文件(在您的情况下可能在WEB-INF / lib中)。

这不包含在Java的标准版(仅限企业版)中,但您只需添加jar文件即可。