/** * Creates new {@link FileDataSource} and then calls {@link #content(DataSource)} * * @param file {@link File} * @return this * @see #content(DataSource) */ public EmailAttachmentBuilder content(final File file) { return content(new FileDataSource(file)); }
/** * 设置文件类型附件 * * @param files 附件文件列表 * @return this */ public Mail setFiles(File... files) { DataSource[] attachments = new DataSource[files.length]; for (int i = 0; i < files.length; i++) { attachments[i] = new FileDataSource(files[i]); } return setAttachments(attachments); }
/** * 设置文件类型附件 * * @param files 附件文件列表 * @return this */ public Mail setFiles(File... files) { DataSource[] attachments = new DataSource[files.length]; for (int i = 0; i < files.length; i++) { attachments[i] = new FileDataSource(files[i]); } return setAttachments(attachments); }
/** * 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); }
/** * 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); }
public EmailMessage addAttachment(final String attachmentName, final File file) throws MessagingException { this._totalAttachmentSizeSoFar += file.length(); if (this._totalAttachmentSizeSoFar > _totalAttachmentMaxSizeInByte) { throw new MessageAttachmentExceededMaximumSizeException( "Adding attachment '" + attachmentName + "' will exceed the allowed maximum size of " + _totalAttachmentMaxSizeInByte); } final BodyPart attachmentPart = new MimeBodyPart(); final DataSource fileDataSource = new FileDataSource(file); attachmentPart.setDataHandler(new DataHandler(fileDataSource)); attachmentPart.setFileName(attachmentName); this._attachments.add(attachmentPart); return this; }
/** * 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); }
/** * 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); }
String file = "path of file to be attached"; String fileName = "attachmentName"; DataSource source = new FileDataSource(file); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(fileName);
DataSource source = new FileDataSource(filename); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(filename);
DataSource source = new FileDataSource(filename); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(filename);
@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)); }
private Object createMessageContent() throws IOException, MessagingException { MimeMultipart content = new MimeMultipart(); MimeBodyPart contentText = new MimeBodyPart(); contentText.setText( "Hello World!" ); content.addBodyPart( contentText ); MimeBodyPart contentFile = new MimeBodyPart(); File testFile = TestUtils.getInputFile( "GetPOP", "txt" ); FileDataSource fds = new FileDataSource( testFile.getAbsolutePath() ); contentFile.setDataHandler( new DataHandler( fds ) ); contentFile.setFileName( testFile.getName() ); content.addBodyPart( contentFile ); return content; }
/** * Creates {@link EmailAttachment}. * * @return {@link EmailAttachment}. * @throws MailException if issue with {@link DataSource}. */ public EmailAttachment<FileDataSource> buildFileDataSource(final String messageId, final File attachmentStorage) throws MailException { try { final FileDataSource fds; if (dataSource instanceof FileDataSource) { fds = (FileDataSource) dataSource; } else { final File file = new File(attachmentStorage, messageId); FileUtil.writeStream(file, dataSource.getInputStream()); fds = new FileDataSource(file); } checkDataSource(); return new EmailAttachment<>(name, contentId, isInline, fds).setEmbeddedMessage(targetMessage); } catch (final IOException ioexc) { throw new MailException(ioexc); } }
if (attachments != null && !attachments.isEmpty()) { for (final File attachment : attachments) { final DataSource source = new FileDataSource(attachment); final MimeBodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setDataHandler(new DataHandler(source));
@Test public void marshalAttachments() throws Exception { unmarshaller = new Jaxb2Marshaller(); unmarshaller.setClassesToBeBound(BinaryObject.class); unmarshaller.setMtomEnabled(true); unmarshaller.afterPropertiesSet(); MimeContainer mimeContainer = mock(MimeContainer.class); Resource logo = new ClassPathResource("spring-ws.png", getClass()); DataHandler dataHandler = new DataHandler(new FileDataSource(logo.getFile())); given(mimeContainer.isXopPackage()).willReturn(true); given(mimeContainer.getAttachment("<6b76528d-7a9c-4def-8e13-095ab89e9bb7@http://springframework.org/spring-ws>")).willReturn(dataHandler); given(mimeContainer.getAttachment("<99bd1592-0521-41a2-9688-a8bfb40192fb@http://springframework.org/spring-ws>")).willReturn(dataHandler); given(mimeContainer.getAttachment("696cfb9a-4d2d-402f-bb5c-59fa69e7f0b3@spring-ws.png")).willReturn(dataHandler); String content = "<binaryObject xmlns='http://springframework.org/spring-ws'>" + "<bytes>" + "<xop:Include href='cid:6b76528d-7a9c-4def-8e13-095ab89e9bb7@http://springframework.org/spring-ws' xmlns:xop='http://www.w3.org/2004/08/xop/include'/>" + "</bytes>" + "<dataHandler>" + "<xop:Include href='cid:99bd1592-0521-41a2-9688-a8bfb40192fb@http://springframework.org/spring-ws' xmlns:xop='http://www.w3.org/2004/08/xop/include'/>" + "</dataHandler>" + "<swaDataHandler>696cfb9a-4d2d-402f-bb5c-59fa69e7f0b3@spring-ws.png</swaDataHandler>" + "</binaryObject>"; StringReader reader = new StringReader(content); Object result = unmarshaller.unmarshal(new StreamSource(reader), mimeContainer); assertTrue("Result is not a BinaryObject", result instanceof BinaryObject); BinaryObject object = (BinaryObject) result; assertNotNull("bytes property not set", object.getBytes()); assertTrue("bytes property not set", object.getBytes().length > 0); assertNotNull("datahandler property not set", object.getSwaDataHandler()); }
FileDataSource fds = new FileDataSource( masterZipfile );
/** * Adds the attachment. * * @param attachment the attachment * @return the mail message */ public MailMessage addAttachment(File attachment) { FileDataSource ds = new FileDataSource(attachment); return addAttachment(ds); }
@Override protected void render(Part mailPart, String fileName) { DataSource ds = new FileDataSource(file); if (fileName == null) { fileName = file.getAbsolutePath(); } render(mailPart, fileName, ds); } }
@Override protected Future<Boolean> send(MailPlugin plugin) throws IOException, MessagingException { return plugin.send(Arrays.asList("dst@gmail.com"), "Testing Subject", "Dear Mail Crawler," + "\n\n No spam to my email, please!", new FileDataSource(f)); } @Override