javax.mail.internet.MimeBodyPart.setFileName()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(13.7k)|赞(0)|评价(0)|浏览(253)

本文整理了Java中javax.mail.internet.MimeBodyPart.setFileName()方法的一些代码示例,展示了MimeBodyPart.setFileName()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。MimeBodyPart.setFileName()方法的具体详情如下:
包路径:javax.mail.internet.MimeBodyPart
类名称:MimeBodyPart
方法名:setFileName

MimeBodyPart.setFileName介绍

[英]Set the filename associated with this body part, if possible.

Sets the "filename" parameter of the "Content-Disposition" header field of this body part. For compatibility with older mailers, the "name" parameter of the "Content-Type" header is also set.

If the mail.mime.encodefilename System property is set to true, the MimeUtility#encodeText method will be used to encode the filename. While such encoding is not supported by the MIME spec, many mailers use this technique to support non-ASCII characters in filenames. The default value of this property is false.
[中]如果可能,设置与此身体部位关联的文件名。
设置此正文部分的“内容处置”标题字段的“文件名”参数。为了与旧邮件程序兼容,还设置了“内容类型”标题的“名称”参数。
如果mail.mime.encodefilename系统属性设置为true,则将使用MimeUtility#encodeText方法对文件名进行编码。虽然MIME规范不支持这种编码,但许多邮件程序使用这种技术来支持文件名中的非ASCII字符。此属性的默认值为false。

代码示例

代码示例来源:origin: stackoverflow.com

  1. if (arrayInputStream != null && arrayInputStream instanceof ByteArrayInputStream) {
  2. // create the second message part with the attachment from a OutputStrean
  3. MimeBodyPart attachment= new MimeBodyPart();
  4. ByteArrayDataSource ds = new ByteArrayDataSource(arrayInputStream, "application/pdf");
  5. attachment.setDataHandler(new DataHandler(ds));
  6. attachment.setFileName("Report.pdf");
  7. mimeMultipart.addBodyPart(attachment);
  8. }

代码示例来源:origin: stackoverflow.com

  1. Multipart multipart = new MimeMultipart("mixed");
  2. for (String str : attachment_PathList) {
  3. MimeBodyPart messageBodyPart = new MimeBodyPart();
  4. DataSource source = new FileDataSource(str);
  5. messageBodyPart.setDataHandler(new DataHandler(source));
  6. messageBodyPart.setFileName(source.getName());
  7. multipart.addBodyPart(messageBodyPart);
  8. }
  9. msg.setContent(multipart);
  10. Transport.send(msg);

代码示例来源:origin: camunda/camunda-bpm-platform

  1. /**
  2. * Use the specified file to provide the data for this part.
  3. * The simple file name is used as the file name for this
  4. * part and the data in the file is used as the data for this
  5. * part. The encoding will be chosen appropriately for the
  6. * file data. The disposition of this part is set to
  7. * {@link Part#ATTACHMENT Part.ATTACHMENT}.
  8. *
  9. * @param file the File object to attach
  10. * @exception IOException errors related to accessing the file
  11. * @exception MessagingException message related errors
  12. * @since JavaMail 1.4
  13. */
  14. public void attachFile(File file) throws IOException, MessagingException {
  15. FileDataSource fds = new FileDataSource(file);
  16. this.setDataHandler(new DataHandler(fds));
  17. this.setFileName(fds.getName());
  18. this.setDisposition(ATTACHMENT);
  19. }

代码示例来源:origin: openmrs/openmrs-core

  1. /**
  2. * Creates a MimeMultipart, so that we can have an attachment.
  3. *
  4. * @param message
  5. * @return
  6. */
  7. private MimeMultipart createMultipart(Message message) throws Exception {
  8. MimeMultipart toReturn = new MimeMultipart();
  9. MimeBodyPart textContent = new MimeBodyPart();
  10. textContent.setContent(message.getContent(), message.getContentType());
  11. MimeBodyPart attachment = new MimeBodyPart();
  12. attachment.setContent(message.getAttachment(), message.getAttachmentContentType());
  13. attachment.setFileName(message.getAttachmentFileName());
  14. toReturn.addBodyPart(textContent);
  15. toReturn.addBodyPart(attachment);
  16. return toReturn;
  17. }

代码示例来源:origin: stackoverflow.com

  1. MimeMultipart rootContainer = new MimeMultipart();
  2. rootContainer.setSubType("related");
  3. rootContainer.addBodyPart(alternativeMultiPartWithPlainTextAndHtml); // not in focus here
  4. rootContainer.addBodyPart(createInlineImagePart(base64EncodedImageContentByteArray));
  5. ...
  6. message.setContent(rootContainer);
  7. message.setHeader("MIME-Version", "1.0");
  8. message.setHeader("Content-Type", rootContainer.getContentType());
  9. ...
  10. BodyPart createInlineImagePart(byte[] base64EncodedImageContentByteArray) throws MessagingException {
  11. InternetHeaders headers = new InternetHeaders();
  12. headers.addHeader("Content-Type", "image/jpeg");
  13. headers.addHeader("Content-Transfer-Encoding", "base64");
  14. MimeBodyPart imagePart = new MimeBodyPart(headers, base64EncodedImageContentByteArray);
  15. imagePart.setDisposition(MimeBodyPart.INLINE);
  16. imagePart.setContentID("<image>");
  17. imagePart.setFileName("image.jpg");
  18. return imagePart;

代码示例来源:origin: blynkkk/blynk-server

  1. private void attachCSV(Multipart multipart, QrHolder[] attachmentData) throws Exception {
  2. StringBuilder sb = new StringBuilder();
  3. for (QrHolder qrHolder : attachmentData) {
  4. sb.append(qrHolder.token)
  5. .append(",")
  6. .append(qrHolder.deviceId)
  7. .append(",")
  8. .append(qrHolder.dashId)
  9. .append("\n");
  10. }
  11. MimeBodyPart attachmentsPart = new MimeBodyPart();
  12. ByteArrayDataSource source = new ByteArrayDataSource(sb.toString(), "text/csv");
  13. attachmentsPart.setDataHandler(new DataHandler(source));
  14. attachmentsPart.setFileName("tokens.csv");
  15. multipart.addBodyPart(attachmentsPart);
  16. }

代码示例来源:origin: com.sun.mail/javax.mail

  1. /**
  2. * Use the specified file to provide the data for this part.
  3. * The simple file name is used as the file name for this
  4. * part and the data in the file is used as the data for this
  5. * part. The encoding will be chosen appropriately for the
  6. * file data. The disposition of this part is set to
  7. * {@link Part#ATTACHMENT Part.ATTACHMENT}.
  8. *
  9. * @param file the File object to attach
  10. * @exception IOException errors related to accessing the file
  11. * @exception MessagingException message related errors
  12. * @since JavaMail 1.4
  13. */
  14. public void attachFile(File file) throws IOException, MessagingException {
  15. FileDataSource fds = new FileDataSource(file);
  16. this.setDataHandler(new DataHandler(fds));
  17. this.setFileName(fds.getName());
  18. this.setDisposition(ATTACHMENT);
  19. }

代码示例来源:origin: stackoverflow.com

  1. Multipart multipart = new MimeMultipart("mixed");
  2. for (int alen = 0; attlen < attachments.length; attlen++)
  3. {
  4. MimeBodyPart messageAttachment = new MimeBodyPart();
  5. fileName = ""+ attachments[attlen];
  6. messageAttachment.attachFile(fileName);
  7. messageAttachment.setFileName(attachment);
  8. multipart.addBodyPart(messageAttachment);
  9. }

代码示例来源:origin: camunda/camunda-bpm-platform

  1. if (isEmpty(name)) { //Exceptional case.
  2. name = toString(attachmentFormatters[i]);
  3. parts[i].setFileName(name);
  4. MimeMultipart multipart = new MimeMultipart();
  5. String altType = getContentType(bodyFormat.getClass().getName());
  6. setContent(body, buf, altType == null ? contentType : altType);

代码示例来源:origin: blynkkk/blynk-server

  1. private void attachQRs(Multipart multipart, QrHolder[] attachmentData) throws Exception {
  2. for (QrHolder qrHolder : attachmentData) {
  3. MimeBodyPart attachmentsPart = new MimeBodyPart();
  4. ByteArrayDataSource source = new ByteArrayDataSource(qrHolder.data, "image/jpeg");
  5. attachmentsPart.setDataHandler(new DataHandler(source));
  6. attachmentsPart.setFileName(qrHolder.makeQRFilename());
  7. multipart.addBodyPart(attachmentsPart);
  8. }
  9. }

代码示例来源:origin: camunda/camunda-bpm-platform

  1. /**
  2. * Use the specified file with the specified Content-Type and
  3. * Content-Transfer-Encoding to provide the data for this part.
  4. * If contentType or encoding are null, appropriate values will
  5. * be chosen.
  6. * The simple file name is used as the file name for this
  7. * part and the data in the file is used as the data for this
  8. * part. The disposition of this part is set to
  9. * {@link Part#ATTACHMENT Part.ATTACHMENT}.
  10. *
  11. * @param file the File object to attach
  12. * @param contentType the Content-Type, or null
  13. * @param encoding the Content-Transfer-Encoding, or null
  14. * @exception IOException errors related to accessing the file
  15. * @exception MessagingException message related errors
  16. * @since JavaMail 1.5
  17. */
  18. public void attachFile(File file, String contentType, String encoding)
  19. throws IOException, MessagingException {
  20. DataSource fds = new EncodedFileDataSource(file, contentType, encoding);
  21. this.setDataHandler(new DataHandler(fds));
  22. this.setFileName(fds.getName());
  23. this.setDisposition(ATTACHMENT);
  24. }

代码示例来源:origin: stackoverflow.com

  1. Multipart multipart = new MimeMultipart();
  2. MimeBodyPart html = new MimeBodyPart();
  3. // Use actual html not "strstr"
  4. html.setContent("<html><body><h1>Hi</h1></body></html>", "text/html");
  5. multipart.addBodyPart(html);
  6. // ...
  7. // Joop Eggen suggestion to avoid spaces in the file name
  8. String fileName = "Service_Change_Alert_"
  9. + new SimpleDateFormat("yyyy-MM-dd_HH:mm").format(date) + ".xlsx";
  10. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  11. updateDataBook.write(baos);
  12. byte[] poiBytes = baos.toByteArray();
  13. // Can be followed by the DataSource / DataHandler stuff if you really need it
  14. MimeBodyPart attachment = new MimeBodyPart();
  15. attachment.setFileName(filename);
  16. attachment.setContent(poiBytes, "application/vnd.ms-excel");
  17. //attachment.setDataHandler(dh);
  18. attachment.setDisposition(MimeBodyPart.ATTACHMENT);
  19. multipart.addBodyPart(attachment);

代码示例来源:origin: com.sun.mail/javax.mail

  1. if (isEmpty(name)) { //Exceptional case.
  2. name = toString(attachmentFormatters[i]);
  3. parts[i].setFileName(name);
  4. setContent(body, buf, altType == null ? contentType : altType);
  5. if (body != msg) {
  6. final MimeMultipart multipart = new MimeMultipart();

代码示例来源:origin: pentaho/pentaho-kettle

  1. private void addAttachedContent( String filename, String fileContent ) throws Exception {
  2. // create a data source
  3. MimeBodyPart mbp = new MimeBodyPart();
  4. // get a data Handler to manipulate this file type;
  5. mbp.setDataHandler( new DataHandler( new ByteArrayDataSource( fileContent.getBytes(), "application/x-any" ) ) );
  6. // include the file in the data source
  7. mbp.setFileName( filename );
  8. // add the part with the file in the BodyPart();
  9. data.parts.addBodyPart( mbp );
  10. }

代码示例来源:origin: com.sun.mail/javax.mail

  1. /**
  2. * Use the specified file with the specified Content-Type and
  3. * Content-Transfer-Encoding to provide the data for this part.
  4. * If contentType or encoding are null, appropriate values will
  5. * be chosen.
  6. * The simple file name is used as the file name for this
  7. * part and the data in the file is used as the data for this
  8. * part. The disposition of this part is set to
  9. * {@link Part#ATTACHMENT Part.ATTACHMENT}.
  10. *
  11. * @param file the File object to attach
  12. * @param contentType the Content-Type, or null
  13. * @param encoding the Content-Transfer-Encoding, or null
  14. * @exception IOException errors related to accessing the file
  15. * @exception MessagingException message related errors
  16. * @since JavaMail 1.5
  17. */
  18. public void attachFile(File file, String contentType, String encoding)
  19. throws IOException, MessagingException {
  20. DataSource fds = new EncodedFileDataSource(file, contentType, encoding);
  21. this.setDataHandler(new DataHandler(fds));
  22. this.setFileName(fds.getName());
  23. this.setDisposition(ATTACHMENT);
  24. }

代码示例来源:origin: com.sun.mail/javax.mail

  1. try {
  2. MimeMultipart multipart = new MimeMultipart();
  3. MimeBodyPart[] ambp = new MimeBodyPart[atn.length];
  4. final MimeBodyPart body;
  5. for (int i = 0; i < atn.length; ++i) {
  6. ambp[i] = createBodyPart(i);
  7. ambp[i].setFileName(atn[i]);

代码示例来源:origin: spring-projects/spring-framework

  1. /**
  2. * Add an attachment to the MimeMessage, taking the content from a
  3. * {@code javax.activation.DataSource}.
  4. * <p>Note that the InputStream returned by the DataSource implementation
  5. * needs to be a <i>fresh one on each call</i>, as JavaMail will invoke
  6. * {@code getInputStream()} multiple times.
  7. * @param attachmentFilename the name of the attachment as it will
  8. * appear in the mail (the content type will be determined by this)
  9. * @param dataSource the {@code javax.activation.DataSource} to take
  10. * the content from, determining the InputStream and the content type
  11. * @throws MessagingException in case of errors
  12. * @see #addAttachment(String, org.springframework.core.io.InputStreamSource)
  13. * @see #addAttachment(String, java.io.File)
  14. */
  15. public void addAttachment(String attachmentFilename, DataSource dataSource) throws MessagingException {
  16. Assert.notNull(attachmentFilename, "Attachment filename must not be null");
  17. Assert.notNull(dataSource, "DataSource must not be null");
  18. try {
  19. MimeBodyPart mimeBodyPart = new MimeBodyPart();
  20. mimeBodyPart.setDisposition(MimeBodyPart.ATTACHMENT);
  21. mimeBodyPart.setFileName(MimeUtility.encodeText(attachmentFilename));
  22. mimeBodyPart.setDataHandler(new DataHandler(dataSource));
  23. getRootMimeMultipart().addBodyPart(mimeBodyPart);
  24. }
  25. catch (UnsupportedEncodingException ex) {
  26. throw new MessagingException("Failed to encode attachment filename", ex);
  27. }
  28. }

代码示例来源:origin: camunda/camunda-bpm-platform

  1. Object ccl = getAndSetContextClassLoader(MAILHANDLER_LOADER);
  2. try {
  3. MimeMultipart multipart = new MimeMultipart();
  4. MimeBodyPart[] ambp = new MimeBodyPart[atn.length];
  5. final MimeBodyPart body;
  6. for (int i = 0; i < atn.length; ++i) {
  7. ambp[i] = createBodyPart(i);
  8. ambp[i].setFileName(atn[i]);

代码示例来源:origin: blynkkk/blynk-server

  1. @Override
  2. public void sendHtmlWithAttachment(String to, String subj, String body, QrHolder[] attachments) throws Exception {
  3. MimeMessage message = new MimeMessage(session);
  4. message.setFrom(from);
  5. message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
  6. message.setSubject(subj, "UTF-8");
  7. Multipart multipart = new MimeMultipart();
  8. MimeBodyPart bodyMessagePart = new MimeBodyPart();
  9. bodyMessagePart.setContent(body, TEXT_HTML_CHARSET_UTF_8);
  10. multipart.addBodyPart(bodyMessagePart);
  11. for (QrHolder qrHolder : attachments) {
  12. MimeBodyPart attachmentsPart = new MimeBodyPart();
  13. attachmentsPart.setDataHandler(new DataHandler(new ByteArrayDataSource(qrHolder.data, "image/jpeg")));
  14. attachmentsPart.setFileName(qrHolder.makeQRFilename());
  15. multipart.addBodyPart(attachmentsPart);
  16. }
  17. message.setContent(multipart);
  18. try (Transport transport = session.getTransport()) {
  19. transport.connect(host, username, password);
  20. transport.sendMessage(message, message.getAllRecipients());
  21. }
  22. log.debug("Mail sent to {}. Subj: {}", to, subj);
  23. log.trace("Mail body: {}", body);
  24. }

代码示例来源:origin: traccar/traccar

  1. private Response executeReport(
  2. long userId, boolean mail, ReportExecutor executor) throws SQLException, IOException {
  3. final ByteArrayOutputStream stream = new ByteArrayOutputStream();
  4. if (mail) {
  5. new Thread(() -> {
  6. try {
  7. executor.execute(stream);
  8. MimeBodyPart attachment = new MimeBodyPart();
  9. attachment.setFileName("report.xlsx");
  10. attachment.setDataHandler(new DataHandler(new ByteArrayDataSource(
  11. stream.toByteArray(), "application/octet-stream")));
  12. Context.getMailManager().sendMessage(
  13. userId, "Report", "The report is in the attachment.", attachment);
  14. } catch (SQLException | IOException | MessagingException e) {
  15. LOGGER.warn("Report failed", e);
  16. }
  17. }).start();
  18. return Response.noContent().build();
  19. } else {
  20. executor.execute(stream);
  21. return Response.ok(stream.toByteArray())
  22. .header(HttpHeaders.CONTENT_DISPOSITION, CONTENT_DISPOSITION_VALUE_XLSX).build();
  23. }
  24. }

相关文章