java - 无法从 Play 框架发送电子邮件

标签 java playframework jakarta-mail playframework-2.2

我正在尝试使用 Play Framework 发送电子邮件。在 applications.conf 中,当我设置 smtp.mock=true 时,它​​运行良好:

[info] application - HTML: Welcome to Play20StartApp. <br> Click on this link : http://localhost:9000/confirm/d77ea256-0d2e-4df5-b66e-e034159f8042 to confirm your email.<br><br>--<br>null
[debug] application - Mail sent - SMTP:smtp.google.com:465 SSL:yes user:username@gmail.com password:password

但是,当我评论 smtp.mock=true 并尝试发送真实的电子邮件时,我收到此错误:

[debug] application - Mail.sendMail: Mail will be sent to user1@gmail.com
[ERROR] [09/26/2013 03:46:05.014] [application-akka.actor.default-dispatcher-2] [TaskInvocation] From address required
org.apache.commons.mail.EmailException: From address required

我已将 smtp.user 设置为适当的值,即 server@businessdomain.com 。知道是什么导致了这个错误吗?

故障排除步骤

  1. 使用的电子邮件是真实的电子邮件,为了发布目的,它们是匿名的。同样,使用真实域名
  2. 已使用工作正常的本地邮件服务器 (exim) 以及通过 Gmail (smtp.google.com) 和 Google Apps (aspmx.l.google.com) 服务器直接发送进行了测试。这些设置已使用邮件客户端验证。
  3. 下面的 Java 代码片段可以完美运行,

    导入 java.util.; 导入 javax.mail.; 导入 javax.mail.internet.; 导入 javax.activation.;

    public class SendEmail
    {
        public static void main(String [] args)
        {
            String to = "recipient@mydomain.com";
            String from = "sender@mydomain.com";
            String host = "localhost";
    
            Properties properties = System.getProperties();
    
            // Setup mail server
            properties.setProperty("mail.smtp.host", host);
    
            // Get the default Session object.
            Session session = Session.getDefaultInstance(properties);
    
            try{
                MimeMessage message = new MimeMessage(session);
    
                // Set From: header field of the header.
                message.setFrom(new InternetAddress(from));
    
                message.addRecipient(Message.RecipientType.TO,
                        new InternetAddress(to));
                message.setSubject("Subject of email");
                message.setText("This is actual message");
    
                Transport.send(message);
                System.out.println("Sent message successfully....");
            }catch (MessagingException mex) {
                mex.printStackTrace();
            }
        }
    }
    

最佳答案

如果您将邮件程序操作包装在 Future(或 Play Java 等效项)中,那么您遇到的可能是变量作用域问题:

这个有效:

val mail = use[MailerPlugin].email
val async: Future[Unit] = Future{
  mail.setSubject(subject)
  mail.setRecipient(recipient)
  mail.setFrom(from)
  mail.send(body)
}
async.onFailure{case(e)=>
  Logger.error(s"mailer failed for $recipient due to: ${e.getMessage}")
}

有效:

val mail = use[MailerPlugin].email
mail.setSubject(subject)
mail.setRecipient(recipient)
mail.setFrom(from)
val async: Future[Unit] = Future{mail.send(body)}
async.onFailure{case(e)=>
  Logger.error(s"mailer failed for $recipient due to: ${e.getMessage}")
}

我假设封闭范围在 Future 闭包中可用;在这种情况下绝对不是因为底层 Apache Commons 邮件程序使用“需要发件人地址”来保释。果然,当 mail.setFrom 和 friends 在 Future{...} 范围之外填充时,设置 smtp.mock=true 表明 fromAddress 为 null。

有趣的是,即使我将 async val 分解为本地方法并传入构造的邮件程序和正文文本:

private def async(mail: MailerAPI, body: String): Future[Unit] = Future{
  mail.send(body)
}

尽管直接传递了一个构造的邮件程序,但在 Future 之外设置的邮件属性仍然以 null 告终——令人惊讶,不是吗?

关于java - 无法从 Play 框架发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19021980/

相关文章:

java - 玩!框架 [1.2.4] - 通过 wami-recorder 接受音频/xwav 内容类型

java - 从c到java,对象

java - 如何在 Google App Engine 中使用 CachedRowSet?

sql-server - 使用 Play 和 Slick 连接到 MSSQL(jtds)

java - 如何让 IntelliJ 识别 Play Framework *.scala.xml 模板

java - 如何在 Java 邮件中添加内联图像和附加文件

java - 如果收件箱中的邮件数量超过 int 范围会怎样?

tomcat - 使用交换服务器从 Struts 应用程序发送邮件时出现 "Unable to Relay"错误

java - 使用有限的内存查找丢失的号码

Java 泛型 方法声明中的 2 组 <K,V> 或 <T>