use of org.apache.commons.mail.MultiPartEmail in project openhab1-addons by openhab.
the class Mail method sendMail.
/**
* Sends an email with attachment(s) via SMTP
*
* @param to the email address of the recipient
* @param subject the subject of the email
* @param message the body of the email
* @param attachmentUrlList a list of URL strings of the contents to send as attachments
*
* @return <code>true</code>, if sending the email has been successful and
* <code>false</code> in all other cases.
*/
@ActionDoc(text = "Sends an email with attachment via SMTP")
public static boolean sendMail(@ParamDoc(name = "to") String to, @ParamDoc(name = "subject") String subject, @ParamDoc(name = "message") String message, @ParamDoc(name = "attachmentUrlList") List<String> attachmentUrlList) {
boolean success = false;
if (MailActionService.isProperlyConfigured) {
Email email = new SimpleEmail();
if (attachmentUrlList != null && !attachmentUrlList.isEmpty()) {
email = new MultiPartEmail();
for (String attachmentUrl : attachmentUrlList) {
// Create the attachment
try {
EmailAttachment attachment = new EmailAttachment();
attachment.setURL(new URL(attachmentUrl));
attachment.setDisposition(EmailAttachment.ATTACHMENT);
String fileName = attachmentUrl.replaceFirst(".*/([^/?]+).*", "$1");
attachment.setName(isNotBlank(fileName) ? fileName : "Attachment");
((MultiPartEmail) email).attach(attachment);
} catch (MalformedURLException e) {
logger.error("Invalid attachment url.", e);
} catch (EmailException e) {
logger.error("Error adding attachment to email.", e);
}
}
}
email.setHostName(hostname);
email.setSmtpPort(port);
email.setStartTLSEnabled(startTLSEnabled);
email.setSSLOnConnect(sslOnConnect);
if (isNotBlank(username)) {
if (popBeforeSmtp) {
email.setPopBeforeSmtp(true, hostname, username, password);
} else {
email.setAuthenticator(new DefaultAuthenticator(username, password));
}
}
try {
if (isNotBlank(charset)) {
email.setCharset(charset);
}
email.setFrom(from);
String[] toList = to.split(";");
for (String toAddress : toList) {
email.addTo(toAddress);
}
if (!isEmpty(subject)) {
email.setSubject(subject);
}
if (!isEmpty(message)) {
email.setMsg(message);
}
email.send();
logger.debug("Sent email to '{}' with subject '{}'.", to, subject);
success = true;
} catch (EmailException e) {
logger.error("Could not send e-mail to '" + to + "'.", e);
}
} else {
logger.error("Cannot send e-mail because of missing configuration settings. The current settings are: " + "Host: '{}', port '{}', from '{}', startTLSEnabled: {}, sslOnConnect: {}, username: '{}', password '{}'", new Object[] { hostname, String.valueOf(port), from, String.valueOf(startTLSEnabled), String.valueOf(sslOnConnect), username, password });
}
return success;
}
use of org.apache.commons.mail.MultiPartEmail in project weicoder by wdcode.
the class EmailApache method sendMultiPartEmail.
/**
* 发送带附件的邮件
* @param to 发送地址
* @param subject 邮件标题
* @param msg 邮件内容
* @param attach 附件
*/
protected final void sendMultiPartEmail(String[] to, String subject, String msg, String attach) {
// 实例化邮件操作类
MultiPartEmail email = new MultiPartEmail();
// 添加附件
setAttachment(email, attach);
// 发送Email
sendEmail(email, to, subject, msg);
}
use of org.apache.commons.mail.MultiPartEmail in project dwoss by gg-net.
the class DocumentSupporterOperation method mail.
/**
* This method send document to the e-Mail address that is in the customer set.
* <p/>
* @param document This is the Document that will be send.
* @throws UserInfoException if the sending of the Mail is not successful.
* @throws RuntimeException if problems exist in the JasperExporter
*/
@Override
public void mail(Document document, DocumentViewType jtype) throws UserInfoException, RuntimeException {
UiCustomer customer = customerService.asUiCustomer(document.getDossier().getCustomerId());
String customerMailAddress = customerService.asCustomerMetaData(document.getDossier().getCustomerId()).getEmail();
if (customerMailAddress == null) {
throw new UserInfoException("Kunde hat keine E-Mail Hinterlegt! Senden einer E-Mail ist nicht Möglich!");
}
String doctype = (jtype == DocumentViewType.DEFAULT ? document.getType().getName() : jtype.getName());
try (InputStream is = mandator.getMailTemplateLocation().toURL().openStream();
InputStreamReader templateReader = new InputStreamReader(is)) {
String text = new MailDocumentParameter(customer.toTitleNameLine(), doctype).eval(IOUtils.toString(templateReader));
MultiPartEmail email = mandator.prepareDirectMail();
email.setCharset("UTF-8");
email.addTo(customerMailAddress);
email.setSubject(document.getType().getName() + " | " + document.getDossier().getIdentifier());
email.setMsg(text + mandator.getDefaultMailSignature());
email.attach(new ByteArrayDataSource(JasperExportManager.exportReportToPdf(jasper(document, jtype)), "application/pdf"), "Dokument.pdf", "Das ist das Dokument zu Ihrem Aufrag als PDF.");
for (MandatorMailAttachment mma : mandator.getDefaultMailAttachment()) {
email.attach(mma.getAttachmentData().toURL(), mma.getAttachmentName(), mma.getAttachmentDescription());
}
email.send();
} catch (EmailException ex) {
L.error("Error on Mail sending", ex);
throw new UserInfoException("Das senden der Mail war nicht erfolgreich!\n" + ex.getMessage());
} catch (IOException | JRException e) {
throw new RuntimeException(e);
}
}
use of org.apache.commons.mail.MultiPartEmail in project dwoss by gg-net.
the class SalesListingProducerOperation method prepareAndSend.
/**
* Prepare and send filejackets to the specified email address.
* <p>
* @param fileJackets files to be send
*/
private void prepareAndSend(List<FileJacket> fileJackets) {
SubMonitor m = monitorFactory.newSubMonitor("Transfer");
m.message("sending Mail");
m.start();
try {
ListingMailConfiguration config = listingService.get().listingMailConfiguration();
MultiPartEmail email = mandator.prepareDirectMail();
email.setFrom(config.getFromAddress());
email.addTo(config.getToAddress());
email.setSubject(config.getSubject());
email.setMsg(config.toMessage());
for (FileJacket fj : fileJackets) {
email.attach(new javax.mail.util.ByteArrayDataSource(fj.getContent(), "application/xls"), fj.getHead() + fj.getSuffix(), "Die Händlerliste für die Marke ");
}
email.send();
m.finish();
} catch (EmailException e) {
throw new RuntimeException(e);
}
}
use of org.apache.commons.mail.MultiPartEmail in project ninja by ninjaframework.
the class CommonsMailHelperImplTest method testDoSetServerParameterStartTLSEnabled.
@Test
public void testDoSetServerParameterStartTLSEnabled() throws Exception {
Mail mail = MailImplTestHelper.getMailImplWithDemoContent();
MultiPartEmail multiPartEmail = commonsmailHelper.createMultiPartEmailWithContent(mail);
commonsmailHelper.doSetServerParameter(multiPartEmail, "mail.superserver.com", 33, true, true, true, "username", "password", true);
assertEquals(true, multiPartEmail.isStartTLSEnabled());
assertEquals(true, multiPartEmail.isStartTLSRequired());
}
Aggregations