java无法从Play framework发送电子邮件
我正在尝试使用Play框架发送电子邮件。在应用程序中。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。将用户设置为适当的值,即。server@businessdomain.com.知道是什么导致了这个错误吗
故障排除步骤
- 使用的电子邮件是真实的电子邮件,为了发布,它们是匿名的。同样地,使用真实的域名
- 通过本地邮件服务器(exim)测试,以及通过Gmail(smtp.google.com)和谷歌应用(aspmx.l.google.com)服务器直接发送。已使用邮件客户端验证了这些设置李>
下面的Java代码片段工作得非常好
导入java。util; 导入javax。邮寄; 导入javax。邮政互联网; 导入javax。激活
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(); } } }
# 1 楼答案
如果您在将来包装mailer操作(或使用Java等效工具),那么您可能会遇到一个变量范围问题:
这是有效的:
这不起作用:
我假设在未来的封闭期内,封闭范围是可用的;在本例中,肯定不是这样,因为底层Apache Commons mailer使用“From address required”进行bail。当然,设置smtp。mock=true表示在发送邮件时fromAddress为空。setFrom和朋友居住在未来{…}之外范围
有趣的是,即使我将async val分解为一个本地方法,并传入构造的mailer和body文本:
在未来之外设置的邮件属性仍然是空的,尽管直接传递了一个构造的邮件器,不是吗