Play Framework 2.2.0 Java Email

时间:2013-12-07 23:56:44

标签: java email playframework-2.2 build-dependencies

我正在使用Play Framework 2.2.0和java 7.4。

我整个星期都在努力寻找可以用来从我的播放应用程序发送电子邮件的电子邮件模块或软件包。我已经尝试了很多不同的包,每次我得到一个包未找到异常,编译错误或包找不到异常。

我有很多 Build.scala Build.sbt 的变体。我还尝试了从“maven”中提取的非托管依赖项和托管依赖项。我相信它分别从我的/ lib目录中调用和拉取。我下载到my / lib目录的软件包是:

公地-IO-2.3

javax.mail

播放-插件-mailer_2.2.0

我的 Build.scala 的版本是:

Build.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());

...

第3次尝试:

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框架,以便它不告诉我这个:

**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)是否正确,是否应该是使用托管或非托管,最终是否可能!!??

全心全意。

3 个答案:

答案 0 :(得分:0)

看起来你有很多问题:

  1. 如何玩电子邮件。
    • 为此我建议查看播放验证码并查看它们是如何工作的。我目前正在使用该模块发送电子邮件。 play-authenticate
  2. 您可能在获取电子邮件日志时遇到问题。您是否尝试查看系统日志? / var / log / syslog(例如在Ubuntu / Debian上)。还有播放here

  3. 的日志配置选项
  4. (看起来你解决了吗?)你有一个:

    编译错误

    错误:找不到符号 在第116行的.. \ app \ controllers \ Parts.java中。

    • 只要你在路径中有播放可执行文件,设置播放路径就没什么特别了。

答案 1 :(得分:0)

感谢您的回复。我今天早上才解决了!该错误是由于我的包未添加到我的路径引起的。我强烈建议使用电源IDE进行类路径修改。我将它输入Eclipse并立即修改了我的对象参考。对于邮件本身来说,我的结果是:

...

import javax.mail.*;
import javax.mail.internet.*;

...

    try {
        String host = "smtp.gmail.com";
        String username = "program-email@gmail.com";
        String password = "password";
        InternetAddress[] addresses = {new InternetAddress("user@anymail.com"),
            new InternetAddress(bid.email),
            new InternetAddress("another-user@anymail.com")};
        Properties props = new Properties();

        // set any needed mail.smtps.* properties here
        Session session = Session.getInstance(props);
        MimeMessage message = new MimeMessage(session);
        message.setSubject("my subject placed here");
        message.setContent("my message placed here:\n\n"
                + part.toString(), "text/plain");
        message.setRecipients(Message.RecipientType.TO, addresses);

        // set the message content here
        Transport t = session.getTransport("smtps");
        try {
            t.connect(host, username, password);
            t.sendMessage(message, message.getAllRecipients());
        } finally {
            t.close();
        }          
    } catch (MessagingException me) {
        me.printStackTrace();
    }

答案 2 :(得分:0)

试试这个:

https://github.com/typesafehub/play-plugins/tree/master/mailer

它对我来说就像一个魅力

如果您有任何问题,请告诉我