call google api from servlet
I tried to call getGmail from my servlet command, but keep getting error and I don't know where went, can someone help?
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;
}
this is the error message
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(Gmail开发者_如何学PythonCommand.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)
java.lang.ClassNotFoundException: javax.mail.NoSuchProviderException
You need to have the JavaMail jar file in your classpath (in your case probably in WEB-INF/lib).
This is not included in the standard edition (only in the enterprise edition) of Java, but you can just add the jar file.
精彩评论