【问题标题】:Play Framework 2.2.0 Java EmailPlay Framework 2.2.0 Java 电子邮件
【发布时间】:2013-12-25 05:45:40
【问题描述】:

我正在使用带有 java 7.4 的 Play Framework 2.2.0。

我整个星期都在努力寻找可以用来从我的游戏应用程序发送电子邮件的电子邮件模块或包。我尝试了许多不同的包,每次都遇到包未找到异常、编译错误或包未找到异常。

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

commons-io-2.3

javax.mail

play-plugins-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());

...

第三次尝试:

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

谢谢大家。

【问题讨论】:

  • 我之前使用的是 sublime text 2 编辑器。所以,我通过设置最新版本的 eclipse 解决了这个问题。我运行 eclipse 命令进行播放并通过 Play 记录的步骤导入。这是一个类路径问题。但是,现在仍然没有发送电子邮件,也没有抛出错误......使用 SimpleEmail 对象我应该能够使用 localhost 发送电子邮件吗?

标签: java email playframework-2.2 build-dependencies


【解决方案1】:

您似乎有多个问题:

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

  3. (看起来你解决了?)你有一个:

    编译错误

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

    • 只要路径中有播放可执行文件,设置播放路径并没有什么特别之处。

【讨论】:

  • 我提供的代码不完整,因此我在此处提供的错误不会是准确的。
【解决方案2】:

感谢您的回复。我今天早上刚刚解决了!该错误是由于我的包未添加到我的路径引起的。我强烈建议使用强大的 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();
    }

【讨论】:

    【解决方案3】:

    试试这个:

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

    它对我来说就像一个魅力

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

    【讨论】:

      猜你喜欢
      • 2015-12-24
      • 1970-01-01
      • 2011-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-16
      • 2013-10-19
      • 1970-01-01
      相关资源
      最近更新 更多