com.jpattern.service.mail.SendMailCommand Maven / Gradle / Ivy
package com.jpattern.service.mail;
import com.jpattern.core.IProvider;
import com.jpattern.core.IServicesName;
import com.jpattern.core.command.ACommand;
import com.jpattern.core.command.ICommandResult;
import com.jpattern.service.mail.message.FromRecipient;
import com.jpattern.service.mail.message.Recipient;
import com.jpattern.service.mail.message.TransportTextMessage;
import com.jpattern.shared.result.ErrorMessage;
/**
*
* @author Francesco Cina'
*
* 04/nov/2009
*/
public class SendMailCommand extends ACommand {
private static final String COMMAND_ACTION = "sendmail.failed";
private static final String COMMAND = "SendMailCommand";
private StringBuffer from;
private StringBuffer to;
private StringBuffer body;
private StringBuffer subject;
public SendMailCommand(StringBuffer from, StringBuffer to, StringBuffer subject, StringBuffer body) {
this.from = from;
this.to = to;
this.body = body;
this.subject = subject;
}
@Override
protected void rollback(ICommandResult result) {
}
@Override
protected void execute(ICommandResult result) {
try {
IMailService mailService = (IMailService) getProvider().getService( IServicesName.MAIL_SENDER_SERVICE );
IMailSender mailSender = mailService.mailSender();
MailRecipients recipients = new MailRecipients();
TransportMailMessage transportMailMessage = new TransportMailMessage();
createMailObjects(recipients, transportMailMessage);
// Aggiungo mittente e destinari al mailsender
mailSender.recipients(recipients);
// invio la mail
createMessages(result, mailSender.send(transportMailMessage));
} catch (Exception e) {
result.addErrorMessage(new ErrorMessage(COMMAND, e.getMessage()));
}
}
protected void createMessages(ICommandResult result, boolean valid) {
if (!valid) {
result.getErrorMessages().add(new ErrorMessage(COMMAND, COMMAND_ACTION));
}
}
private void createMailObjects(MailRecipients recipients, TransportMailMessage transportMailMessage) {
// Creo mittente
Recipient recipientFROM = new Recipient(from.toString());
FromRecipient fromrecipient = new FromRecipient(recipientFROM);
recipients.from(fromrecipient);
// Creo destinatario
recipients.addRecipient(new Recipient( to.toString() ));
// creo il subject
TransportTextMessage transportTextMessage = new TransportTextMessage(subject.toString());
// creo il body
TransportTextMessage transportBodyTextMessage = new TransportTextMessage(body.toString());
// creo la mail ed aggiungo subject e body
transportMailMessage.subject(transportTextMessage);
transportMailMessage.body(transportBodyTextMessage);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy