@Override public void process(final InputStream stream) throws IOException { try { mimeFile.setDataHandler(new DataHandler(new ByteArrayDataSource(stream, "application/octet-stream"))); } catch (final Exception e) { throw new IOException(e); } } });
/** * Returns the number of bytes that can be read from this input * stream without blocking. * * @return the number of bytes that can be read from this input * stream without blocking. * @exception IOException if an I/O error occurs. */ public synchronized int available() throws IOException { ensureOpen(); return (count - pos) + in_available(); }
public InputStream toStream() { return new SharedByteArrayInputStream(buf, 0, count); } }
/** * Creates new {@link ByteArrayDataSource} and then calls {@link #content(DataSource)}. * * @param bytes array of bytes * @param contentType content type from {@link EmailAttachment}. * @return this * @see #content(DataSource) */ public EmailAttachmentBuilder content(final byte[] bytes, final String contentType) { return content(new ByteArrayDataSource(bytes, resolveContentType(contentType))); }
public InputStream toStream() { return new SharedByteArrayInputStream(buf, 0, count); } }
/** * Creates new {@link ByteArrayDataSource} and then calls {@link #content(DataSource)}. * * @param inputStream {@link InputStream} * @param contentType content type from {@link EmailAttachment}. * @return this * @throws IOException if {@link ByteArrayDataSource} cannot be created from {@link InputStream} * @see #content(DataSource) */ public EmailAttachmentBuilder content(final InputStream inputStream, final String contentType) throws IOException { return content(new ByteArrayDataSource(inputStream, resolveContentType(contentType))); }
private void addAttachedContent( String filename, String fileContent ) throws Exception { // create a data source MimeBodyPart mbp = new MimeBodyPart(); // get a data Handler to manipulate this file type; mbp.setDataHandler( new DataHandler( new ByteArrayDataSource( fileContent.getBytes(), "application/x-any" ) ) ); // include the file in the data source mbp.setFileName( filename ); // add the part with the file in the BodyPart(); data.parts.addBodyPart( mbp ); }
private void attachCSV(Multipart multipart, QrHolder[] attachmentData) throws Exception { StringBuilder sb = new StringBuilder(); for (QrHolder qrHolder : attachmentData) { sb.append(qrHolder.token) .append(",") .append(qrHolder.deviceId) .append(",") .append(qrHolder.dashId) .append("\n"); } MimeBodyPart attachmentsPart = new MimeBodyPart(); ByteArrayDataSource source = new ByteArrayDataSource(sb.toString(), "text/csv"); attachmentsPart.setDataHandler(new DataHandler(source)); attachmentsPart.setFileName("tokens.csv"); multipart.addBodyPart(attachmentsPart); }
private void attachQRs(Multipart multipart, QrHolder[] attachmentData) throws Exception { for (QrHolder qrHolder : attachmentData) { MimeBodyPart attachmentsPart = new MimeBodyPart(); ByteArrayDataSource source = new ByteArrayDataSource(qrHolder.data, "image/jpeg"); attachmentsPart.setDataHandler(new DataHandler(source)); attachmentsPart.setFileName(qrHolder.makeQRFilename()); multipart.addBodyPart(attachmentsPart); } }
@Signature public PHtmlEmail attach(Environment env, Memory content, String type, String name, String description) throws EmailException, MessagingException, IOException { InputStream is = Stream.getInputStream(env, content); try { htmlEmail.attach(new ByteArrayDataSource(is, type), name, description, EmailAttachment.ATTACHMENT); return this; } finally { Stream.closeStream(env, is); } }
private Response executeReport( long userId, boolean mail, ReportExecutor executor) throws SQLException, IOException { final ByteArrayOutputStream stream = new ByteArrayOutputStream(); if (mail) { new Thread(() -> { try { executor.execute(stream); MimeBodyPart attachment = new MimeBodyPart(); attachment.setFileName("report.xlsx"); attachment.setDataHandler(new DataHandler(new ByteArrayDataSource( stream.toByteArray(), "application/octet-stream"))); Context.getMailManager().sendMessage( userId, "Report", "The report is in the attachment.", attachment); } catch (SQLException | IOException | MessagingException e) { LOGGER.warn("Report failed", e); } }).start(); return Response.noContent().build(); } else { executor.execute(stream); return Response.ok(stream.toByteArray()) .header(HttpHeaders.CONTENT_DISPOSITION, CONTENT_DISPOSITION_VALUE_XLSX).build(); } }
@Override public void sendHtmlWithAttachment(String to, String subj, String body, QrHolder[] attachments) throws Exception { MimeMessage message = new MimeMessage(session); message.setFrom(from); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); message.setSubject(subj, "UTF-8"); Multipart multipart = new MimeMultipart(); MimeBodyPart bodyMessagePart = new MimeBodyPart(); bodyMessagePart.setContent(body, TEXT_HTML_CHARSET_UTF_8); multipart.addBodyPart(bodyMessagePart); for (QrHolder qrHolder : attachments) { MimeBodyPart attachmentsPart = new MimeBodyPart(); attachmentsPart.setDataHandler(new DataHandler(new ByteArrayDataSource(qrHolder.data, "image/jpeg"))); attachmentsPart.setFileName(qrHolder.makeQRFilename()); multipart.addBodyPart(attachmentsPart); } message.setContent(multipart); try (Transport transport = session.getTransport()) { transport.connect(host, username, password); transport.sendMessage(message, message.getAllRecipients()); } log.debug("Mail sent to {}. Subj: {}", to, subj); log.trace("Mail body: {}", body); }
/** * Creates {@link EmailAttachment}. * * @return {@link EmailAttachment}. * @throws MailException if issue with {@link DataSource}. */ public EmailAttachment<ByteArrayDataSource> buildByteArrayDataSource() throws MailException { try { final ByteArrayDataSource bads; if (dataSource instanceof ByteArrayDataSource) { bads = (ByteArrayDataSource) dataSource; } else { bads = new ByteArrayDataSource(dataSource.getInputStream(), dataSource.getContentType()); } checkDataSource(); return new EmailAttachment<>(name, contentId, isInline, bads).setEmbeddedMessage(targetMessage); } catch (final IOException ioexc) { throw new MailException(ioexc); } }
DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain")); message.setSender(new InternetAddress(user)); message.setSubject(subject);
entity.copyBytes(bytes, 0); ByteArrayDataSource ds = new ByteArrayDataSource(bytes, "multipart/mixed"); MimeMultipart mp = new MimeMultipart(ds); for(int i = 0; i < mp.getCount(); i++) {
ByteArrayDataSource ds = new ByteArrayDataSource(conn.getInputStream(), "multipart/mixed"); MimeMultipart mp = new MimeMultipart(ds);
ByteArrayDataSource ds = new ByteArrayDataSource(bytes, "multipart/mixed"); MimeMultipart mp = new MimeMultipart(ds); for(int i = 0; i < mp.getCount(); i++) { ByteArrayDataSource nestedDS = new ByteArrayDataSource(part.getInputStream(), "multipart/mixed"); MimeMultipart valueParts = new MimeMultipart(nestedDS);
@Override public void prepare(MimeMessage mimeMessage) throws Exception { EmailTarget emailUser = (EmailTarget) props.get(EmailPropertyType.USER.getType()); EmailInfo info = (EmailInfo) props.get(EmailPropertyType.INFO.getType()); boolean isMultipart = CollectionUtils.isNotEmpty(info.getAttachments()); MimeMessageHelper message = new MimeMessageHelper(mimeMessage, isMultipart, info.getEncoding()); message.setTo(emailUser.getEmailAddress()); message.setFrom(info.getFromAddress()); message.setSubject(info.getSubject()); if (emailUser.getBCCAddresses() != null && emailUser.getBCCAddresses().length > 0) { message.setBcc(emailUser.getBCCAddresses()); } if (emailUser.getCCAddresses() != null && emailUser.getCCAddresses().length > 0) { message.setCc(emailUser.getCCAddresses()); } String messageBody = info.getMessageBody(); if (messageBody == null) { messageBody = buildMessageBody(info, props); } message.setText(messageBody, true); for (Attachment attachment : info.getAttachments()) { ByteArrayDataSource dataSource = new ByteArrayDataSource(attachment.getData(), attachment.getMimeType()); message.addAttachment(attachment.getFilename(), dataSource); } } };
mimeText.setDataHandler(new DataHandler(new ByteArrayDataSource( Base64.encodeBase64(messageText.getBytes("UTF-8")), contentType + "; charset=\"utf-8\""))); final MimeBodyPart mimeFile = new MimeBodyPart(); public void process(final InputStream stream) throws IOException { try { mimeFile.setDataHandler(new DataHandler(new ByteArrayDataSource(stream, "application/octet-stream"))); } catch (final Exception e) { throw new IOException(e);
file.load(); ByteArrayDataSource bds = new ByteArrayDataSource(file.getContent(), file.getContentType());