Powered By Blogger

Monday, February 14, 2011

Mail Issues

For any issues related to mail see MailActionExecuter,
it has ther code for sending mail to group as well as to user

protected void executeImpl(
final Action ruleAction,
final NodeRef actionedUponNodeRef)
{
// Create the mime mail message
MimeMessagePreparator mailPreparer = new MimeMessagePreparator()
{
@SuppressWarnings("unchecked")
public void prepare(MimeMessage mimeMessage) throws MessagingException
{
if (logger.isDebugEnabled())
{
logger.debug(ruleAction.getParameterValues());
}

MimeMessageHelper message = new MimeMessageHelper(mimeMessage);

// set header encoding if one has been supplied
if (headerEncoding != null && headerEncoding.length() != 0)
{
mimeMessage.setHeader("Content-Transfer-Encoding", headerEncoding);
}

// set recipient
String to = (String)ruleAction.getParameterValue(PARAM_TO);
if (to != null && to.length() != 0)
{
message.setTo(to);
}
else
{
// see if multiple recipients have been supplied - as a list of authorities
Serializable authoritiesValue = ruleAction.getParameterValue(PARAM_TO_MANY);
List authorities = null;
if (authoritiesValue != null)
{
if (authoritiesValue instanceof String)
{
authorities = new ArrayList(1);
authorities.add((String)authoritiesValue);
}
else
{
authorities = (List)authoritiesValue;
}
}

if (authorities != null && authorities.size() != 0)
{
List recipients = new ArrayList(authorities.size());
for (String authority : authorities)
{
AuthorityType authType = AuthorityType.getAuthorityType(authority);
if (authType.equals(AuthorityType.USER))
{
if (personService.personExists(authority) == true)
{
NodeRef person = personService.getPerson(authority);
String address = (String)nodeService.getProperty(person, ContentModel.PROP_EMAIL);
if (address != null && address.length() != 0 && validateAddress(address))
{
recipients.add(address);
}
}
}
else if (authType.equals(AuthorityType.GROUP))
{
// else notify all members of the group
Set users = authorityService.getContainedAuthorities(AuthorityType.USER, authority, false);
for (String userAuth : users)
{
if (personService.personExists(userAuth) == true)
{
NodeRef person = personService.getPerson(userAuth);
String address = (String)nodeService.getProperty(person, ContentModel.PROP_EMAIL);
if (address != null && address.length() != 0)
{
recipients.add(address);
}
}
}
}
}

message.setTo(recipients.toArray(new String[recipients.size()]));
}
else
{
// No recipiants have been specified
logger.error("No recipiant has been specified for the mail action");
}
}

// set subject line
message.setSubject((String)ruleAction.getParameterValue(PARAM_SUBJECT));

// See if an email template has been specified
String text = null;
NodeRef templateRef = (NodeRef)ruleAction.getParameterValue(PARAM_TEMPLATE);
if (templateRef != null)
{
// build the email template model
Map model = createEmailTemplateModel(actionedUponNodeRef);

// process the template against the model
text = templateService.processTemplate("freemarker", templateRef.toString(), model);
}

// set the text body of the message
if (text == null)
{
text = (String)ruleAction.getParameterValue(PARAM_TEXT);
}
message.setText(text);

// set the from address
NodeRef person = personService.getPerson(authService.getCurrentUserName());

String fromActualUser = null;
if (person != null)
{
fromActualUser = (String) nodeService.getProperty(person, ContentModel.PROP_EMAIL);
}
if( fromActualUser != null && fromActualUser.length() != 0)
{
message.setFrom(fromActualUser);
}
else
{
String from = (String)ruleAction.getParameterValue(PARAM_FROM);
if (from == null || from.length() == 0)
{
message.setFrom(fromAddress);
}
else
{
message.setFrom(from);
}
}
}
};

try
{
// Send the message unless we are in "testMode"
if(!testMode)
{
javaMailSender.send(mailPreparer);
}
else
{
try {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
mailPreparer.prepare(mimeMessage);
lastTestMessage = mimeMessage;
} catch(Exception e) {
System.err.println(e);
}
}
}
catch (MailException e)
{
String to = (String)ruleAction.getParameterValue(PARAM_TO);
if (to == null)
{
Object obj = ruleAction.getParameterValue(PARAM_TO_MANY);
if (obj != null)
{
to = obj.toString();
}
}

logger.error("Failed to send email to " + to, e);

throw new AlfrescoRuntimeException("Failed to send email to:" + to, e);
}
}

No comments:

Post a Comment