我正在尝试使用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。知道是什么导致了这个错误吗?
故障排除步骤
下面的Java代码段完美无缺,
import java.util。; import javax.mail。; import javax.mail.internet。; import 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();
}
}
}
答案 0 :(得分:0)
如果您将邮件程序操作包装在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邮件程序保释“需要发件人地址”。果然,设置smtp.mock = true表明当mail.setFrom和friends填充在Future {...}范围之外时,fromAddress为null。
有趣的是,即使我将async val分解为本地方法并传入构造的邮件和正文:
private def async(mail: MailerAPI, body: String): Future[Unit] = Future{
mail.send(body)
}
尽管直接传递了一个构建的邮件程序,但在Future之外设置的邮件属性仍然会结束 - 令人惊讶,不是吗?