use of javax.activation.FileDataSource in project orientdb by orientechnologies.
the class OMailPlugin method addAttachment.
/**
* Adds a file as an attachment to the email's content
*
* @param multipart
* @param filePath
* @throws MessagingException
*/
private void addAttachment(final Multipart multipart, final String filePath) throws MessagingException {
MimeBodyPart attachPart = new MimeBodyPart();
DataSource source = new FileDataSource(filePath);
attachPart.setDataHandler(new DataHandler(source));
attachPart.setFileName(new File(filePath).getName());
multipart.addBodyPart(attachPart);
}
use of javax.activation.FileDataSource in project spring-framework by spring-projects.
the class MimeMessageHelper method addInline.
/**
* Add an inline element to the MimeMessage, taking the content from a
* {@code java.io.File}.
* <p>The content type will be determined by the name of the given
* content file. Do not use this for temporary files with arbitrary
* filenames (possibly ending in ".tmp" or the like)!
* <p><b>NOTE:</b> Invoke {@code addInline} <i>after</i> {@link #setText};
* else, mail readers might not be able to resolve inline references correctly.
* @param contentId the content ID to use. Will end up as "Content-ID" header
* in the body part, surrounded by angle brackets: e.g. "myId" -> "<myId>".
* Can be referenced in HTML source via src="cid:myId" expressions.
* @param file the File resource to take the content from
* @throws MessagingException in case of errors
* @see #setText
* @see #addInline(String, org.springframework.core.io.Resource)
* @see #addInline(String, javax.activation.DataSource)
*/
public void addInline(String contentId, File file) throws MessagingException {
Assert.notNull(file, "File must not be null");
FileDataSource dataSource = new FileDataSource(file);
dataSource.setFileTypeMap(getFileTypeMap());
addInline(contentId, dataSource);
}
use of javax.activation.FileDataSource in project spring-framework by spring-projects.
the class MimeMessageHelper method addAttachment.
/**
* Add an attachment to the MimeMessage, taking the content from a
* {@code java.io.File}.
* <p>The content type will be determined by the name of the given
* content file. Do not use this for temporary files with arbitrary
* filenames (possibly ending in ".tmp" or the like)!
* @param attachmentFilename the name of the attachment as it will
* appear in the mail
* @param file the File resource to take the content from
* @throws MessagingException in case of errors
* @see #addAttachment(String, org.springframework.core.io.InputStreamSource)
* @see #addAttachment(String, javax.activation.DataSource)
*/
public void addAttachment(String attachmentFilename, File file) throws MessagingException {
Assert.notNull(file, "File must not be null");
FileDataSource dataSource = new FileDataSource(file);
dataSource.setFileTypeMap(getFileTypeMap());
addAttachment(attachmentFilename, dataSource);
}
use of javax.activation.FileDataSource in project spring-framework by spring-projects.
the class Jaxb2MarshallerTests method marshalAttachments.
@Test
public void marshalAttachments() throws Exception {
marshaller = new Jaxb2Marshaller();
marshaller.setClassesToBeBound(BinaryObject.class);
marshaller.setMtomEnabled(true);
marshaller.afterPropertiesSet();
MimeContainer mimeContainer = mock(MimeContainer.class);
Resource logo = new ClassPathResource("spring-ws.png", getClass());
DataHandler dataHandler = new DataHandler(new FileDataSource(logo.getFile()));
given(mimeContainer.convertToXopPackage()).willReturn(true);
byte[] bytes = FileCopyUtils.copyToByteArray(logo.getInputStream());
BinaryObject object = new BinaryObject(bytes, dataHandler);
StringWriter writer = new StringWriter();
marshaller.marshal(object, new StreamResult(writer), mimeContainer);
assertTrue("No XML written", writer.toString().length() > 0);
verify(mimeContainer, times(3)).addAttachment(isA(String.class), isA(DataHandler.class));
}
use of javax.activation.FileDataSource in project opennms by OpenNMS.
the class JavaMailer2 method createFileAttachment.
/*
public Message buildMessage(String m_charSet, String m_encoding, String m_contentType) throws JavaMailerException {
try {
String encodedText = MimeUtility.encodeText(getMessageText(), m_charSet, m_encoding);
if (getFileName() == null) {
message.setContent(encodedText, m_contentType+"; charset="+m_charSet);
} else {
BodyPart bp = new MimeBodyPart();
bp.setContent(encodedText, m_contentType+"; charset="+m_charSet);
MimeMultipart mp = new MimeMultipart();
mp.addBodyPart(bp);
mp = new MimeMultipart();
mp.addBodyPart(createFileAttachment(new File(getFileName())));
message.setContent(mp);
}
message.setHeader("X-Mailer", getMailer());
message.setSentDate(new Date());
message.saveChanges();
return message;
} catch (AddressException e) {
log().error("Java Mailer Addressing exception: ", e);
throw new JavaMailerException("Java Mailer Addressing exception: ", e);
} catch (MessagingException e) {
log().error("Java Mailer messaging exception: ", e);
throw new JavaMailerException("Java Mailer messaging exception: ", e);
} catch (UnsupportedEncodingException e) {
log().error("Java Mailer messaging exception: ", e);
throw new JavaMailerException("Java Mailer encoding exception: ", e);
}
}
*/
/**
* Create a file attachment as a MimeBodyPart, checking to see if the file
* exists before we create the attachment.
*
* @param file file to attach
* @return attachment body part
* @throws javax.mail.MessagingException if we can't set the data handler or
* the file name on the MimeBodyPart
* @throws org.opennms.javamail.JavaMailerException if the file does not exist or is not
* readable
*/
public MimeBodyPart createFileAttachment(final File file) throws MessagingException, JavaMailerException {
if (!file.exists()) {
LOG.error("File attachment '{}' does not exist.", file.getAbsolutePath());
throw new JavaMailerException("File attachment '" + file.getAbsolutePath() + "' does not exist.");
}
if (!file.canRead()) {
LOG.error("File attachment '{}' is not readable.", file.getAbsolutePath());
throw new JavaMailerException("File attachment '" + file.getAbsolutePath() + "' is not readable.");
}
MimeBodyPart bodyPart = new MimeBodyPart();
FileDataSource fds = new FileDataSource(file);
bodyPart.setDataHandler(new DataHandler(fds));
bodyPart.setFileName(fds.getName());
return bodyPart;
}
Aggregations