我在Java 7.4中使用Play Framework 2.2.0。Play Framework 2.2.0 Java電子郵件
我一直試圖找到一個電子郵件模塊或包,我可以用它來從我的播放應用程序發送電子郵件。我已經嘗試了許多不同的包,每次我得到一個未發現包的異常,編譯錯誤或包未找到異常。
我有Build.scala和Build.sbt的許多變化。我也嘗試了非託管依賴和託管依賴從'maven'拉我相信它的調用和從我的/ lib目錄分別拉。我已經下載到我的/ lib目錄下的程序包有:
公地IO-2.3
javax.mail
播放插件,mailer_2.2.0
我我的Build.scala版本已被:
Build.scala
1日嘗試:
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"
)
}
第2次嘗試:
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);
...
第2次嘗試:
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();
}
...
我的問題是我怎麼設置我的遊戲框架,以便它不告訴我這個:
**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)是否正確,我是否應該使用託管或非託管,並最終是否這是可能的!?!?
謝謝大家。
我之前使用的是崇高的文本2編輯器。所以,我通過設置最新版本的eclipse來解決這個問題。我運行eclipse命令進行遊戲並通過Play的記錄步驟導入。這是一個類路徑問題。然而,現在電子郵件仍然沒有被髮送,也沒有錯誤被拋出......使用SimpleEmail對象,我應該能夠使用本地主機發送電子郵件? – mk7upurz87