/** * Return the name of the data object. If this DataHandler * was created with a DataSource, this method calls through * to the <code>DataSource.getName</code> method, otherwise it * returns <i>null</i>. * * @return the name of the object */ public String getName() { if (dataSource != null) return dataSource.getName(); else return null; }
/** * Sets the {@link DataSource}. Common {@link DataSource}s include {@link ByteArrayDataSource} and * {@link FileDataSource}. * * @param dataSource {@link DataSource} * @return this */ public <T extends DataSource> EmailAttachmentBuilder content(final T dataSource) { this.dataSource = dataSource; name(dataSource.getName()); return this; }
protected void attach(Email email, List<File> files, List<DataSource> dataSources) throws EmailException { if (!(email instanceof MultiPartEmail && attachmentsExist(files, dataSources))) { return; } MultiPartEmail mpEmail = (MultiPartEmail) email; for (File file : files) { mpEmail.attach(file); } for (DataSource ds : dataSources) { if (ds != null) { mpEmail.attach(ds, ds.getName(), null); } } }
/** * 构建邮件信息主体 * * @param charset 编码 * @return 邮件信息主体 * @throws MessagingException 消息异常 */ private Multipart buildContent(Charset charset) throws MessagingException { final Multipart mainPart = new MimeMultipart(); // 正文 final BodyPart body = new MimeBodyPart(); body.setContent(content, StrUtil.format("text/{}; charset={}", isHtml ? "html" : "plain", charset)); mainPart.addBodyPart(body); // 附件 if (ArrayUtil.isNotEmpty(this.attachments)) { BodyPart bodyPart; for (DataSource attachment : attachments) { bodyPart = new MimeBodyPart(); bodyPart.setDataHandler(new DataHandler(attachment)); bodyPart.setFileName(InternalMailUtil.encodeText(attachment.getName(), charset)); mainPart.addBodyPart(bodyPart); } } return mainPart; }
/** * 构建邮件信息主体 * * @param charset 编码 * @return 邮件信息主体 * @throws MessagingException 消息异常 */ private Multipart buildContent(Charset charset) throws MessagingException { final Multipart mainPart = new MimeMultipart(); // 正文 final BodyPart body = new MimeBodyPart(); body.setContent(content, StrUtil.format("text/{}; charset={}", isHtml ? "html" : "plain", charset)); mainPart.addBodyPart(body); // 附件 if (ArrayUtil.isNotEmpty(this.attachments)) { BodyPart bodyPart; for (DataSource attachment : attachments) { bodyPart = new MimeBodyPart(); bodyPart.setDataHandler(new DataHandler(attachment)); bodyPart.setFileName(InternalMailUtil.encodeText(attachment.getName(), charset)); mainPart.addBodyPart(bodyPart); } } return mainPart; }
/** * Return the name of the data object. If this DataHandler * was created with a DataSource, this method calls through * to the <code>DataSource.getName</code> method, otherwise it * returns <i>null</i>. * * @return the name of the object */ public String getName() { if (dataSource != null) return dataSource.getName(); else return null; }
@Override protected void render(Part mailPart, String fileName) { if (fileName == null) { fileName = dataSource.getName(); } render(mailPart, fileName, dataSource); } }
@Override protected void render(Part mailPart, String fileName) { if (fileName == null) { fileName = dataSource.getName(); } render(mailPart, fileName, dataSource); } }
/** * Find an attachment using its name. * * @param name the name of the attachment * @return the corresponding datasource or null if nothing was found */ public DataSource findAttachmentByName(final String name) { DataSource dataSource; for (int i = 0; i < getAttachmentList().size(); i++) { dataSource = getAttachmentList().get(i); if (name.equalsIgnoreCase(dataSource.getName())) { return dataSource; } } return null; }
/** * Creates the attachment part. * * @param attachment the attachment * @return the mime body part * @throws MessagingException the messaging exception */ private static MimeBodyPart createAttachmentPart(DataSource attachment) throws MessagingException { MimeBodyPart attachmentPart = new MimeBodyPart(); attachmentPart.setDataHandler(new DataHandler(attachment)); attachmentPart.setDisposition(Part.ATTACHMENT); attachmentPart.setFileName(attachment.getName()); return attachmentPart; }
/** * Determines the name of the data source if it is not already set. * * @param part the mail part * @param dataSource the data source * @return the name of the data source or {@code null} if no name can be determined * @throws MessagingException accessing the part failed * @throws UnsupportedEncodingException decoding the text failed */ protected String getDataSourceName(final Part part, final DataSource dataSource) throws MessagingException, UnsupportedEncodingException { String result = dataSource.getName(); if (result == null || result.length() == 0) { result = part.getFileName(); } if (result != null && result.length() > 0) { result = MimeUtility.decodeText(result); } else { result = null; } return result; }
String name = dataSource.getName(); if (EmailUtils.isEmpty(name))
MimeBodyPart attachmentPart = new MimeBodyPart(); attachmentPart.setDataHandler(new DataHandler(attachment)); attachmentPart.setFileName(attachment.getName()); multipart.addBodyPart(attachmentPart);
/** * Use the specified file with the specified Content-Type and * Content-Transfer-Encoding to provide the data for this part. * If contentType or encoding are null, appropriate values will * be chosen. * The simple file name is used as the file name for this * part and the data in the file is used as the data for this * part. The disposition of this part is set to * {@link Part#ATTACHMENT Part.ATTACHMENT}. * * @param file the File object to attach * @param contentType the Content-Type, or null * @param encoding the Content-Transfer-Encoding, or null * @exception IOException errors related to accessing the file * @exception MessagingException message related errors * @since JavaMail 1.5 */ public void attachFile(File file, String contentType, String encoding) throws IOException, MessagingException { DataSource fds = new EncodedFileDataSource(file, contentType, encoding); this.setDataHandler(new DataHandler(fds)); this.setFileName(fds.getName()); this.setDisposition(ATTACHMENT); }
/** * Use the specified file with the specified Content-Type and * Content-Transfer-Encoding to provide the data for this part. * If contentType or encoding are null, appropriate values will * be chosen. * The simple file name is used as the file name for this * part and the data in the file is used as the data for this * part. The disposition of this part is set to * {@link Part#ATTACHMENT Part.ATTACHMENT}. * * @param file the File object to attach * @param contentType the Content-Type, or null * @param encoding the Content-Transfer-Encoding, or null * @exception IOException errors related to accessing the file * @exception MessagingException message related errors * @since JavaMail 1.5 */ public void attachFile(File file, String contentType, String encoding) throws IOException, MessagingException { DataSource fds = new EncodedFileDataSource(file, contentType, encoding); this.setDataHandler(new DataHandler(fds)); this.setFileName(fds.getName()); this.setDisposition(ATTACHMENT); }
if (zip) { if (pluginConfiguration.getString("ZipExtension") != null) { response.setHeader("Content-Disposition", "inline; filename=\"" + dataSource.getName() + "." + pluginConfiguration.getString(SerializerPlugin.ZIP_EXTENSION) + "\""); } else { response.setHeader("Content-Disposition", "inline; filename=\"" + dataSource.getName() + ".zip" + "\""); String nameInZip = dataSource.getName() + "." + pluginConfiguration.getString(SerializerPlugin.EXTENSION); ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream); zipOutputStream.putNextEntry(new ZipEntry(nameInZip)); if (request.getParameter("mime") == null) { response.setContentType(pluginConfiguration.getString(SerializerPlugin.CONTENT_TYPE)); response.setHeader("Content-Disposition", "inline; filename=\"" + dataSource.getName() + "." + pluginConfiguration.getString(SerializerPlugin.EXTENSION) + "\""); } else { response.setContentType(request.getParameter("mime"));
/** * Get the name of the attached file. * * @return name of the attached file */ public String getFilename() { return dataSource.getName(); }
name = ds.getName();
public String getName() { load(); return dataSource.getName(); }