Magnolia邮件模块不起作用

时间:2013-02-22 17:01:35

标签: magnolia

我尝试使用Magnolia 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("whoever@whatever.co.uk");
                    mail.setBody("HELLO");
                    mail.setFrom("whoever@whatever.co.uk");
                    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: [whoever@whatever.co.uk]

但电子邮件永远不会来......

在此追踪之前,我得到:

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()标记为已弃用,但我不知道必须放置什么。

有人可以帮助我吗?

谢谢!

2 个答案:

答案 0 :(得分:0)

由于没有抛出异常,我猜您错误地配置了您的SMTP服务器或根本没有。如何执行此操作可以在此处阅读:http://documentation.magnolia-cms.com/modules/mail.html#ConfiguringSMTP

另外,请确保:

  • 您的邮件没有登陆任何垃圾邮件过滤器(可能在您的邮箱之外)
  • 没有阻止防火墙(例如,在localhost上运行时)

答案 1 :(得分:0)

好的,我终于用这段代码解决了这个问题:

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服务器配置。