这是我的发送邮件代码

<html>
 <head>
<title>JSP JavaMail Example </title>
 </head>

<body>

<%@ page import="java.util.*" %>
<%@ page import="javax.mail.*" %>
<%@ page import="javax.mail.internet.*" %>
<%@ page import="javax.activation.*" %>
<%@ page import="javax.mail.Authenticator" %>
<%@ page import="javax.mail.PasswordAuthentication" %>
<%@ page import="java.sql.*" %>
<%@ page import="com.google.appengine.api.rdbms.*" %>
<%@ page import="com.sun.mail.smtp.*" %>
<%@ page import="com.google.appengine.api.mail.*" %>

<%

class GMailAuthenticator extends Authenticator {
 String user;
 String pw;
 public GMailAuthenticator (String username, String password)
 {
    super();
    this.user = username;
    this.pw = password;
 }
public PasswordAuthentication getPasswordAuthentication()
{
   return new PasswordAuthentication(user, pw);
}
}

String host = "smtp.gmail.com";
String user= "[email protected]";
String pass= "xxxxxxxxx";
String to = request.getParameter("to");
String from = request.getParameter("from");
String subject = request.getParameter("subject");
String messageText = request.getParameter("body");
boolean sessionDebug = false;

String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

Properties props = System.getProperties();
props.put("mail.host", host);
props.put("mail.smtp.", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.fallback", "true");
props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
props.put("mail.debug", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.starttls.enable",true);
props.put("mail.smtp.starttls.required",true);

Session mailSession =Session.getInstance(props, new GMailAuthenticator(user, pass));

mailSession.setDebug(sessionDebug);

Message msg = new MimeMessage(mailSession);

msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setSentDate(new java.util.Date());
msg.setText(messageText);

Transport transport = mailSession.getTransport("smtp");
transport.connect(host,user,pass);

Transport.send(msg);

out.println("Mail was sent to " + to);
out.println(" from " + from);
out.println(" using host " + host + ".");
response.sendRedirect("verify.jsp");

String u= String.valueOf(session.getAttribute("un"));

Connection c = null;
PreparedStatement ps=null;
ResultSet rs=null;
Class.forName("com.mysql.jdbc.Driver");
DriverManager.registerDriver(new AppEngineDriver());
c =     DriverManager.getConnection("jdbc:google:rdbms://ctsteaching:ctsdatabase/ctsdb");


String query="update Registration set Status='Active' where Username=? ";
ps=c.prepareStatement(query);
ps.setString(1,u.trim());
ps.executeUpdate();

   %>
   </table>
  </body>
 </html>


将其部署到云中后,发送邮件时出现以下错误。

java.lang.NoClassDefFoundError: javax.net.SocketFactory is a restricted class. Please see the Google App Engine developer's guide for more details.
    at com.google.appengine.runtime.Request.process-02f037dc0f1c0aab(Request.java)
    at javax.net.SocketFactory.<clinit>(SocketFactory.java)
    at java.lang.reflect.Method.invoke(Method.java:43)
    at com.sun.mail.util.SocketFetcher.getSocketFactory(SocketFetcher.java:382)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:193)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1938)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:642)
    at javax.mail.Service.connect(Service.java:248)
    at javax.mail.Service.connect(Service.java:91)
    at org.apache.jsp.sendMail_jsp._jspService(sendMail_jsp.java:118)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:326)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
    at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:923)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
    at com.google.tracing.TraceContext$TraceContextRunnable.runInContext(TraceContext.java:449)
    at com.google.tracing.TraceContext$TraceContextRunnable$1.run(TraceContext.java:455)
    at com.google.tracing.TraceContext.runInContext(TraceContext.java:695)
    at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContextNoUnref(TraceContext.java:333)
    at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContext(TraceContext.java:325)
    at com.google.tracing.TraceContext$TraceContextRunnable.run(TraceContext.java:453)
    at java.lang.Thread.run(Thread.java:679)

最佳答案

javax.net.SocketFactory不在AE的白名单中,因此无法使用。
https://developers.google.com/appengine/docs/java/jrewhitelist

要发送邮件,请参阅
https://developers.google.com/appengine/docs/java/mail/usingjavamail

10-08 09:34