use of org.pentaho.platform.api.email.IEmailService in project pentaho-platform by pentaho.
the class EmailComponent method executeAction.
@Override
public boolean executeAction() {
EmailAction emailAction = (EmailAction) getActionDefinition();
String messagePlain = emailAction.getMessagePlain().getStringValue();
String messageHtml = emailAction.getMessageHtml().getStringValue();
String subject = emailAction.getSubject().getStringValue();
String to = emailAction.getTo().getStringValue();
String cc = emailAction.getCc().getStringValue();
String bcc = emailAction.getBcc().getStringValue();
String from = emailAction.getFrom().getStringValue(defaultFrom);
if (from.trim().length() == 0) {
from = defaultFrom;
}
if (ComponentBase.debug) {
// $NON-NLS-1$
debug(Messages.getInstance().getString("Email.DEBUG_TO_FROM", to, from));
// $NON-NLS-1$
debug(Messages.getInstance().getString("Email.DEBUG_CC_BCC", cc, bcc));
// $NON-NLS-1$
debug(Messages.getInstance().getString("Email.DEBUG_SUBJECT", subject));
// $NON-NLS-1$
debug(Messages.getInstance().getString("Email.DEBUG_PLAIN_MESSAGE", messagePlain));
// $NON-NLS-1$
debug(Messages.getInstance().getString("Email.DEBUG_HTML_MESSAGE", messageHtml));
}
if ((to == null) || (to.trim().length() == 0)) {
// Get the output stream that the feedback is going into
OutputStream feedbackStream = getFeedbackOutputStream();
if (feedbackStream != null) {
createFeedbackParameter("to", Messages.getInstance().getString("Email.USER_ENTER_EMAIL_ADDRESS"), "", "", // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
true);
// $NON-NLS-1$
setFeedbackMimeType("text/html");
return true;
} else {
return false;
}
}
if (subject == null) {
// $NON-NLS-1$
error(Messages.getInstance().getErrorString("Email.ERROR_0005_NULL_SUBJECT", getActionName()));
return false;
}
if ((messagePlain == null) && (messageHtml == null)) {
// $NON-NLS-1$
error(Messages.getInstance().getErrorString("Email.ERROR_0006_NULL_BODY", getActionName()));
return false;
}
if (getRuntimeContext().isPromptPending()) {
return true;
}
try {
Properties props = new Properties();
final IEmailService service = PentahoSystem.get(IEmailService.class, "IEmailService", PentahoSessionHolder.getSession());
props.put("mail.smtp.host", service.getEmailConfig().getSmtpHost());
props.put("mail.smtp.port", ObjectUtils.toString(service.getEmailConfig().getSmtpPort()));
props.put("mail.transport.protocol", service.getEmailConfig().getSmtpProtocol());
props.put("mail.smtp.starttls.enable", ObjectUtils.toString(service.getEmailConfig().isUseStartTls()));
props.put("mail.smtp.auth", ObjectUtils.toString(service.getEmailConfig().isAuthenticate()));
props.put("mail.smtp.ssl", ObjectUtils.toString(service.getEmailConfig().isUseSsl()));
props.put("mail.smtp.quitwait", ObjectUtils.toString(service.getEmailConfig().isSmtpQuitWait()));
props.put("mail.from.default", service.getEmailConfig().getDefaultFrom());
String fromName = service.getEmailConfig().getFromName();
if (StringUtils.isEmpty(fromName)) {
fromName = Messages.getInstance().getString("schedulerEmailFromName");
}
props.put("mail.from.name", fromName);
props.put("mail.debug", ObjectUtils.toString(service.getEmailConfig().isDebug()));
Session session;
if (service.getEmailConfig().isAuthenticate()) {
props.put("mail.userid", service.getEmailConfig().getUserId());
props.put("mail.password", decrypt(service.getEmailConfig().getPassword()));
Authenticator authenticator = new EmailAuthenticator();
session = Session.getInstance(props, authenticator);
} else {
session = Session.getInstance(props);
}
// debugging is on if either component (xaction) or email config debug is on
if (service.getEmailConfig().isDebug() || ComponentBase.debug) {
session.setDebug(true);
}
// construct the message
MimeMessage msg = new MimeMessage(session);
if (from != null) {
msg.setFrom(new InternetAddress(from));
} else {
// There should be no way to get here
// $NON-NLS-1$
error(Messages.getInstance().getString("Email.ERROR_0012_FROM_NOT_DEFINED"));
}
if ((to != null) && (to.trim().length() > 0)) {
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
}
if ((cc != null) && (cc.trim().length() > 0)) {
msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc, false));
}
if ((bcc != null) && (bcc.trim().length() > 0)) {
msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc, false));
}
if (subject != null) {
msg.setSubject(subject, LocaleHelper.getSystemEncoding());
}
EmailAttachment[] emailAttachments = emailAction.getAttachments();
if ((messagePlain != null) && (messageHtml == null) && (emailAttachments.length == 0)) {
msg.setText(messagePlain, LocaleHelper.getSystemEncoding());
} else if (emailAttachments.length == 0) {
if (messagePlain != null) {
// $NON-NLS-1$
msg.setContent(messagePlain, "text/plain; charset=" + LocaleHelper.getSystemEncoding());
}
if (messageHtml != null) {
// $NON-NLS-1$
msg.setContent(messageHtml, "text/html; charset=" + LocaleHelper.getSystemEncoding());
}
} else {
// need to create a multi-part message...
// create the Multipart and add its parts to it
Multipart multipart = new MimeMultipart();
// create and fill the first message part
if (messageHtml != null) {
// create and fill the first message part
MimeBodyPart htmlBodyPart = new MimeBodyPart();
// $NON-NLS-1$
htmlBodyPart.setContent(messageHtml, "text/html; charset=" + LocaleHelper.getSystemEncoding());
multipart.addBodyPart(htmlBodyPart);
}
if (messagePlain != null) {
MimeBodyPart textBodyPart = new MimeBodyPart();
// $NON-NLS-1$
textBodyPart.setContent(messagePlain, "text/plain; charset=" + LocaleHelper.getSystemEncoding());
multipart.addBodyPart(textBodyPart);
}
for (EmailAttachment element : emailAttachments) {
IPentahoStreamSource source = element.getContent();
if (source == null) {
// $NON-NLS-1$
error(Messages.getInstance().getErrorString("Email.ERROR_0015_ATTACHMENT_FAILED"));
return false;
}
DataSource dataSource = new ActivationHelper.PentahoStreamSourceWrapper(source);
String attachmentName = element.getName();
if (ComponentBase.debug) {
// $NON-NLS-1$
debug(Messages.getInstance().getString("Email.DEBUG_ADDING_ATTACHMENT", attachmentName));
}
// create the second message part
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
// attach the file to the message
attachmentBodyPart.setDataHandler(new DataHandler(dataSource));
attachmentBodyPart.setFileName(attachmentName);
if (ComponentBase.debug) {
// $NON-NLS-1$
debug(Messages.getInstance().getString("Email.DEBUG_ATTACHMENT_SOURCE", dataSource.getName()));
}
multipart.addBodyPart(attachmentBodyPart);
}
// add the Multipart to the message
msg.setContent(multipart);
}
// $NON-NLS-1$
msg.setHeader("X-Mailer", EmailComponent.MAILER);
msg.setSentDate(new Date());
Transport.send(msg);
if (ComponentBase.debug) {
// $NON-NLS-1$
debug(Messages.getInstance().getString("Email.DEBUG_EMAIL_SUCCESS"));
}
return true;
// TODO: persist the content set for a while...
} catch (SendFailedException e) {
// $NON-NLS-1$
error(Messages.getInstance().getErrorString("Email.ERROR_0011_SEND_FAILED", to), e);
/*
* Exception ne; MessagingException sfe = e; while ((ne = sfe.getNextException()) != null && ne instanceof
* MessagingException) { sfe = (MessagingException) ne;
* error(Messages.getInstance().getErrorString("Email.ERROR_0011_SEND_FAILED", sfe.toString()), sfe);
* //$NON-NLS-1$ }
*/
} catch (AuthenticationFailedException e) {
// $NON-NLS-1$
error(Messages.getInstance().getString("Email.ERROR_0014_AUTHENTICATION_FAILED", to), e);
} catch (Throwable e) {
// $NON-NLS-1$
error(Messages.getInstance().getErrorString("Email.ERROR_0011_SEND_FAILED", to), e);
}
return false;
}
use of org.pentaho.platform.api.email.IEmailService in project pentaho-platform by pentaho.
the class EmailComponent method validateSystemSettings.
@Override
protected boolean validateSystemSettings() {
final IEmailService service = PentahoSystem.get(IEmailService.class, "IEmailService", PentahoSessionHolder.getSession());
String mailhost = service.getEmailConfig().getSmtpHost();
boolean authenticate = service.getEmailConfig().isAuthenticate();
defaultFrom = service.getEmailConfig().getDefaultFrom();
String user = service.getEmailConfig().getUserId();
String password = service.getEmailConfig().getPassword();
// Check the email server settings...
if (mailhost.equals("") || (user.equals("") && authenticate) || defaultFrom.equals("")) {
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
// looks like the email stuff is not configured yet...
// see if we can provide feedback to the user...
boolean allowParameterUI = feedbackAllowed();
if (allowParameterUI) {
OutputStream feedbackStream = getFeedbackOutputStream();
StringBuffer messageBuffer = new StringBuffer();
PentahoSystem.get(IMessageFormatter.class, getSession()).formatErrorMessage("text/html", Messages.getInstance().getString("Email.USER_COULD_NOT_SEND_EMAIL"), Messages.getInstance().getString("Email.USER_SETTINGS_HELP"), // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
messageBuffer);
try {
feedbackStream.write(messageBuffer.toString().getBytes(LocaleHelper.getSystemEncoding()));
} catch (Exception e) {
// $NON-NLS-1$
error(Messages.getInstance().getErrorString("Base.ERROR_0003_INVALID_FEEDBACK_STREAM"), e);
return false;
}
return false;
} else {
// we are not allowed to provide feedback and cannot continue...
// $NON-NLS-1$
error(Messages.getInstance().getErrorString("Email.ERROR_0009_SERVER_SETTINGS_NOT_SET"));
return false;
}
}
boolean ok = (mailhost != null);
if (authenticate) {
ok &= (user != null) && (password != null);
}
ok &= defaultFrom != null;
return ok;
}
use of org.pentaho.platform.api.email.IEmailService in project pentaho-platform by pentaho.
the class Emailer method setup.
public boolean setup() {
try {
final IEmailService service = PentahoSystem.get(IEmailService.class, "IEmailService", PentahoSessionHolder.getSession());
props.put("mail.smtp.host", service.getEmailConfig().getSmtpHost());
props.put("mail.smtp.port", ObjectUtils.toString(service.getEmailConfig().getSmtpPort()));
props.put("mail.transport.protocol", service.getEmailConfig().getSmtpProtocol());
props.put("mail.smtp.starttls.enable", ObjectUtils.toString(service.getEmailConfig().isUseStartTls()));
props.put("mail.smtp.auth", ObjectUtils.toString(service.getEmailConfig().isAuthenticate()));
props.put("mail.smtp.ssl", ObjectUtils.toString(service.getEmailConfig().isUseSsl()));
props.put("mail.smtp.quitwait", ObjectUtils.toString(service.getEmailConfig().isSmtpQuitWait()));
props.put("mail.from.default", service.getEmailConfig().getDefaultFrom());
String fromName = service.getEmailConfig().getFromName();
if (StringUtils.isEmpty(fromName)) {
fromName = getEmailFromName();
}
props.put("mail.from.name", fromName);
props.put("mail.debug", ObjectUtils.toString(service.getEmailConfig().isDebug()));
if (service.getEmailConfig().isAuthenticate()) {
props.put("mail.userid", service.getEmailConfig().getUserId());
props.put("mail.password", service.getEmailConfig().getPassword());
setAuthenticator(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
String decrypted;
try {
Base64PasswordService ps = new Base64PasswordService();
String pass = service.getEmailConfig().getPassword();
if (pass.startsWith("ENC:")) {
decrypted = ps.decrypt(service.getEmailConfig().getPassword().substring(4, pass.length()));
} else {
decrypted = ps.decrypt(service.getEmailConfig().getPassword());
}
} catch (Exception e) {
decrypted = service.getEmailConfig().getPassword();
}
return new PasswordAuthentication(service.getEmailConfig().getUserId(), decrypted);
}
});
}
return true;
} catch (Exception e) {
// $NON-NLS-1$
logger.error("Email.ERROR_0013_CONFIG_FILE_INVALID", e);
}
return false;
}
Aggregations