问题
Im trying to send an email using Mail Module in Magnolia CMS 4.5.4. The code I have so far is:
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){
}
}
}
The log I get is:
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]
But the email never come...
Before this trace I get :
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 marks the method MailModule.getInstance() as deprecated but I have no idea what I must to put instead.
Somebody can help me?
Thanks!
回答1:
As there are no thrown Exceptions, I guess you wrongly configured your SMTP server or not at all. How to do this can be read here: http://documentation.magnolia-cms.com/modules/mail.html#ConfiguringSMTP
Also, assure that:
- Your mail didn't land in any spam filter (maybe outside your mailbox)
- There are no blocking firewalls (e.g. when running on localhost)
回答2:
Ok, I finally solve it with this code:
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());
}
}
}
I think what it make it works was this line:
mail.setBodyFromResourceFile();
And, of course, a good configuration of the SMTP server.
来源:https://stackoverflow.com/questions/15029418/magnolia-mail-module-doesnt-work