使用 GWT Servlet 中的 Java Sendmail 发送邮件部署到 tomcat



我有一个用java 1.7编译的eclipse项目,并被复制到tomcat 6。当我使用以下代码发送电子邮件时,出现以下错误:

at com.google.appengine.api.mail.stdimpl.GMTransport.sendMessage(GMTransport.java:236)
at javax.
maicom.google.apphosting.api.ApiProxy$CallNotFoundException: The API package 'mail' or call 'Send()' was not found.
    at com.google.apphosting.api.ApiProxy.makeSyncCall(ApiProxy.java:109)
    at com.google.apphosting.api.ApiProxy.makeSyncCall(ApiProxy.java:64)
    at com.google.appengine.api.mail.MailServiceImpl.doSend(MailServiceImpl.java:101)
    at com.google.appengine.api.mail.MailServiceImpl.send(MailServiceImpl.java:34)
l.Transport.send(Transport.java:95)
    at javax.mail.Transport.send(Transport.java:48)
...

代码为:

  try {
      Message msg = new MimeMessage(session);
    msg.setFrom(new InternetAddress(from, "hello from admin"));
    msg.addRecipient(Message.RecipientType.TO,
    new InternetAddress(to, “user”));
    msg.setSubject(subject);
    msg.setText(message);

      Transport.send(msg);

  } catch (AddressException e) {
    myPrint("Address exception: "+e);
      // ...
  } catch (MessagingException e) {
    myPrint("Messaging exception: "+e);
      // ...
  }

在创建和部署 war 文件之前,我已经将最新的 oracle 邮件.jar 和关联的 lib 文件夹放入 war/WEB-INF/lib 目录中。

尽管有一两个关于堆栈溢出的类似报告似乎都没有提供此问题的解决方案。

已解决

原因:Google 应用引擎已在较早阶段添加到项目中。Google 应用引擎类会隐藏 javax.mail 类,以便源中 javax.mail 类的导入不会在 IDE 中显示为未解析的引用。因此,我不知不觉地使用应用程序引擎版本进行编译。将应用引擎代码部署到自己的服务器会导致此错误,因为引用了错误的类库。

解决方案

  1. 在不添加 GWT 应用引擎功能的情况下重新生成项目。
  2. 创建库文件夹,导入所需的java邮件.jar文件并添加到构建路径: 对于 Eclipse IDE:右键单击文件并选择"添加到生成路径"。
  3. 重新编译并部署到自己的服务器。然后代码(如上所述)将工作。

最新更新