我正在使用带有 java 7.4 的 Play Framework 2.2.0。
我整个星期都在努力寻找可以用来从我的游戏应用程序发送电子邮件的电子邮件模块或包。我尝试了许多不同的包,每次我得到包未找到异常、编译错误或包未找到异常。
我有很多Build.scala和Build.sbt的变体。我还尝试了从“maven”中提取的非托管依赖项和托管依赖项,我相信它分别被调用并从我的 /lib 目录中提取。我下载到我的 /lib 目录的包是:
commons-io-2.3
javax.mail
播放插件-mailer_2.2.0
我的Build.scala版本是:
构建.scala
第一次尝试:
import sbt._
import Keys._
object ApplicationBuild extends Build {
val appName = "Asset Manager"
val appVersion = "1.0"
val appDependencies = Seq(
"mysql" % "mysql-connector-java" % "5.1.27",
"org.scala-tools" %% "scala-stm" % "0.3",
"org.apache.derby" % "derby" % "10.4.1.3" % "test",
"org.apache.commons" % "commons-email" % "1.3.1",
"commons-io" % "commons-io" % "2.3"
)
}
第二次尝试:
import sbt._
import Keys._
object ApplicationBuild extends Build {
lazy val buildVersion = "2.2.0"
lazy val playVersion = "2.2.0"
val appName = "Asset Manager"
val appVersion = "1.0"
val appDependencies = Seq(
"mysql" % "mysql-connector-java" % "5.1.27",
"org.scala-tools" %% "scala-stm" % "0.3",
"org.apache.derby" % "derby" % "10.4.1.3" % "test",
"commons-io" % "commons-io" % "2.3"
)
libraryDependencies += "org.apache.commons" % "commons-email" % "1.3.1";
libraryDependencies += "com.typesafe" %% "play-plugins-util" % buildVersion;
}
我的电子邮件 Java 文件Parts.java:
第一次尝试
package controllers;
import play.libs.*;
import java.io.*;
import java.util.*;
import org.apache.commons.mail.*;
...
SimpleEmail email = new SimpleEmail();
email.setFrom(User.getByUsername(Session.get("username")).email);
email.addTo(app.configuration().getString("ownerEmail"));
email.addTo(part.email);
email.setSubject("Part Added: " + part.vendor + " - " + part.label);
email.setMsg("A Part has been added to the Asset Manager:\n\n"
+ part.toString());
Mail.send(email);
...
第二次尝试:
package controllers;
import play.libs.*;
import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import com.typesafe.plugin._;
...
MailerAPI mail = play.Play.application().plugin(MailerPlugin.class).email();
mail.setSubject("test");
mail.addRecipient("");
mail.addFrom("");
mail.sendHtml("A Part has been added to the Asset Manager:\n\n" + part.toString());
...
第三次尝试:
import java.io.*;
import java.util.*;
import java.util.*;
import javax.mail.internet.*;
import javax.activation.*;
...
String to = "...";
String from = "...";
String host = "localhost";
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", host);
// Get the default Session object.
Session session = Session.getDefaultInstance(properties);
try{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
message.setSubject("This is the Subject Line!");
message.setText("This is actual message");
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException me) {
me.printStackTrace();
}
...
我的问题是如何设置我的 Play Framework,这样它就不会告诉我这个:
**Compilation error**
error: cannot find symbol
In ..\app\controllers\Parts.java at line 116.
113 // mail.addFrom("");
114 // mail.sendHtml("A Part has been added to the Asset Manager:\n\n" + part.toString());
115
116 SimpleEmail email = new SimpleEmail();
117 email.setHostName("smtp.googlemail.com");
118 email.setSmtpPort(465);
119 email.setFrom("","");
120 // email.setFrom(User.getByUsername(Session.get("username")).email);
121 email.addTo("");
**注意:我清空了一些包含个人信息的字符串。
每当我尝试刷新我的页面时。我需要知道我将包的特定信息放在哪里,build.scala 的外观,命令顺序(../play clean;../play dependencies;../play run)是否正确,我是否应该使用托管或非托管,最终这是否可能!?!?
谢谢大家。