玉兰邮件模块不起作用
问题描述:
我试图发送电子邮件在木兰CMS 4.5.4使用邮件模块。我到目前为止的代码是:玉兰邮件模块不起作用
protected void sendEmail(CommentDTO comment){
if(comment!=null){
try{
MgnlMailFactory mailFactory = MailModule.getInstance().getFactory();
if(mailFactory!=null){
Map<String, Object> params = new HashMap<String, Object>();
MgnlEmail mail = mailFactory.getEmailFromTemplate("MyTemplate", params);
mail.setToList("[email protected]");
mail.setBody("HELLO");
mail.setFrom("[email protected]");
if(mail!=null){
MgnlMailHandler mmh = mailFactory.getEmailHandler();
if(mmh!=null){
mmh.prepareAndSendMail(mail);
}
}
}
}catch(Exception e){
}
}
}
我得到的日志是:
2013-02-22 16:52:30,357 INFO fo.magnolia.module.mail.handlers.SimpleMailHandler: Mail has been sent to: [2013-02-22 16:52:30,357 INFO fo.magnolia.module.mail.handlers.SimpleMailHandler: Mail has been sent to: [[email protected]]
但电子邮件永远不会到来......
此跟踪我得到前:
2013-02-22 16:52:24,212 WARN info.magnolia.cms.util.DeprecationUtil : A deprecated class or method was used: Use IoC!. Check the following trace: info.magnolia.module.mail.MailModule.getInstance(MailModule.java:80), info.magnolia.module.mail.MgnlMailFactory.getEmailHandler(MgnlMailFactory.java:69), the full stracktrace will be logged in debug mode in the info.magnolia.cms.util.DeprecationUtil category.
Eclipse将MailModule.getInstance()方法标记为不推荐使用,但我不知道必须放置什么。
有人可以帮助我吗?
谢谢!
答
好运行时,例如,我终于与此代码解决这个问题:
protected void sendEmail(CommentDTO comment){
if(comment!=null){
try{
MgnlMailFactory mailFactory = MailModule.getInstance().getFactory();
if(mailFactory!=null){
Map<String, Object> params = new HashMap<String, Object>();
params.put("articleName", comment.getArticleName());
params.put("id", comment.getId());
params.put("commentText", comment.getComment());
params.put("author", comment.getName());
MgnlEmail mail = mailFactory.getEmailFromTemplate("myTemplate", params);
mail.setBodyFromResourceFile();
if(mail!=null){
MgnlMailHandler mmh = mailFactory.getEmailHandler();
if(mmh!=null){
mmh.prepareAndSendMail(mail);
}
}
}
}catch(Exception e){
log.error("Error sending email: " +e.getMessage());
}
}
}
我想这使其作品是这一行:
mail.setBodyFromResourceFile();
,当然,SMTP服务器的正常配置。
答
由于没有引发异常,我想你错误地配置了你的SMTP服务器,或根本没有。如何做到这一点可以在这里阅读:http://documentation.magnolia-cms.com/modules/mail.html#ConfiguringSMTP
此外,确保:
- 您的邮件没有任何垃圾邮件过滤器的土地(也许您的邮箱外)
- 没有阻止防火墙(在localhost)