本文整理了Java中javax.mail.internet.MimeBodyPart.setFileName()
方法的一些代码示例,展示了MimeBodyPart.setFileName()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。MimeBodyPart.setFileName()
方法的具体详情如下:
包路径:javax.mail.internet.MimeBodyPart
类名称: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
if (arrayInputStream != null && arrayInputStream instanceof ByteArrayInputStream) {
// create the second message part with the attachment from a OutputStrean
MimeBodyPart attachment= new MimeBodyPart();
ByteArrayDataSource ds = new ByteArrayDataSource(arrayInputStream, "application/pdf");
attachment.setDataHandler(new DataHandler(ds));
attachment.setFileName("Report.pdf");
mimeMultipart.addBodyPart(attachment);
}
代码示例来源:origin: stackoverflow.com
Multipart multipart = new MimeMultipart("mixed");
for (String str : attachment_PathList) {
MimeBodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(str);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(source.getName());
multipart.addBodyPart(messageBodyPart);
}
msg.setContent(multipart);
Transport.send(msg);
代码示例来源:origin: camunda/camunda-bpm-platform
/**
* Use the specified file to provide the data for this part.
* 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 encoding will be chosen appropriately for the
* file data. The disposition of this part is set to
* {@link Part#ATTACHMENT Part.ATTACHMENT}.
*
* @param file the File object to attach
* @exception IOException errors related to accessing the file
* @exception MessagingException message related errors
* @since JavaMail 1.4
*/
public void attachFile(File file) throws IOException, MessagingException {
FileDataSource fds = new FileDataSource(file);
this.setDataHandler(new DataHandler(fds));
this.setFileName(fds.getName());
this.setDisposition(ATTACHMENT);
}
代码示例来源:origin: openmrs/openmrs-core
/**
* Creates a MimeMultipart, so that we can have an attachment.
*
* @param message
* @return
*/
private MimeMultipart createMultipart(Message message) throws Exception {
MimeMultipart toReturn = new MimeMultipart();
MimeBodyPart textContent = new MimeBodyPart();
textContent.setContent(message.getContent(), message.getContentType());
MimeBodyPart attachment = new MimeBodyPart();
attachment.setContent(message.getAttachment(), message.getAttachmentContentType());
attachment.setFileName(message.getAttachmentFileName());
toReturn.addBodyPart(textContent);
toReturn.addBodyPart(attachment);
return toReturn;
}
代码示例来源:origin: stackoverflow.com
MimeMultipart rootContainer = new MimeMultipart();
rootContainer.setSubType("related");
rootContainer.addBodyPart(alternativeMultiPartWithPlainTextAndHtml); // not in focus here
rootContainer.addBodyPart(createInlineImagePart(base64EncodedImageContentByteArray));
...
message.setContent(rootContainer);
message.setHeader("MIME-Version", "1.0");
message.setHeader("Content-Type", rootContainer.getContentType());
...
BodyPart createInlineImagePart(byte[] base64EncodedImageContentByteArray) throws MessagingException {
InternetHeaders headers = new InternetHeaders();
headers.addHeader("Content-Type", "image/jpeg");
headers.addHeader("Content-Transfer-Encoding", "base64");
MimeBodyPart imagePart = new MimeBodyPart(headers, base64EncodedImageContentByteArray);
imagePart.setDisposition(MimeBodyPart.INLINE);
imagePart.setContentID("<image>");
imagePart.setFileName("image.jpg");
return imagePart;
代码示例来源:origin: blynkkk/blynk-server
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);
}
代码示例来源:origin: com.sun.mail/javax.mail
/**
* Use the specified file to provide the data for this part.
* 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 encoding will be chosen appropriately for the
* file data. The disposition of this part is set to
* {@link Part#ATTACHMENT Part.ATTACHMENT}.
*
* @param file the File object to attach
* @exception IOException errors related to accessing the file
* @exception MessagingException message related errors
* @since JavaMail 1.4
*/
public void attachFile(File file) throws IOException, MessagingException {
FileDataSource fds = new FileDataSource(file);
this.setDataHandler(new DataHandler(fds));
this.setFileName(fds.getName());
this.setDisposition(ATTACHMENT);
}
代码示例来源:origin: stackoverflow.com
Multipart multipart = new MimeMultipart("mixed");
for (int alen = 0; attlen < attachments.length; attlen++)
{
MimeBodyPart messageAttachment = new MimeBodyPart();
fileName = ""+ attachments[attlen];
messageAttachment.attachFile(fileName);
messageAttachment.setFileName(attachment);
multipart.addBodyPart(messageAttachment);
}
代码示例来源:origin: camunda/camunda-bpm-platform
if (isEmpty(name)) { //Exceptional case.
name = toString(attachmentFormatters[i]);
parts[i].setFileName(name);
MimeMultipart multipart = new MimeMultipart();
String altType = getContentType(bodyFormat.getClass().getName());
setContent(body, buf, altType == null ? contentType : altType);
代码示例来源:origin: blynkkk/blynk-server
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);
}
}
代码示例来源:origin: camunda/camunda-bpm-platform
/**
* 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);
}
代码示例来源:origin: stackoverflow.com
Multipart multipart = new MimeMultipart();
MimeBodyPart html = new MimeBodyPart();
// Use actual html not "strstr"
html.setContent("<html><body><h1>Hi</h1></body></html>", "text/html");
multipart.addBodyPart(html);
// ...
// Joop Eggen suggestion to avoid spaces in the file name
String fileName = "Service_Change_Alert_"
+ new SimpleDateFormat("yyyy-MM-dd_HH:mm").format(date) + ".xlsx";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
updateDataBook.write(baos);
byte[] poiBytes = baos.toByteArray();
// Can be followed by the DataSource / DataHandler stuff if you really need it
MimeBodyPart attachment = new MimeBodyPart();
attachment.setFileName(filename);
attachment.setContent(poiBytes, "application/vnd.ms-excel");
//attachment.setDataHandler(dh);
attachment.setDisposition(MimeBodyPart.ATTACHMENT);
multipart.addBodyPart(attachment);
代码示例来源:origin: com.sun.mail/javax.mail
if (isEmpty(name)) { //Exceptional case.
name = toString(attachmentFormatters[i]);
parts[i].setFileName(name);
setContent(body, buf, altType == null ? contentType : altType);
if (body != msg) {
final MimeMultipart multipart = new MimeMultipart();
代码示例来源:origin: pentaho/pentaho-kettle
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 );
}
代码示例来源:origin: com.sun.mail/javax.mail
/**
* 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);
}
代码示例来源:origin: com.sun.mail/javax.mail
try {
MimeMultipart multipart = new MimeMultipart();
MimeBodyPart[] ambp = new MimeBodyPart[atn.length];
final MimeBodyPart body;
for (int i = 0; i < atn.length; ++i) {
ambp[i] = createBodyPart(i);
ambp[i].setFileName(atn[i]);
代码示例来源:origin: spring-projects/spring-framework
/**
* Add an attachment to the MimeMessage, taking the content from a
* {@code javax.activation.DataSource}.
* <p>Note that the InputStream returned by the DataSource implementation
* needs to be a <i>fresh one on each call</i>, as JavaMail will invoke
* {@code getInputStream()} multiple times.
* @param attachmentFilename the name of the attachment as it will
* appear in the mail (the content type will be determined by this)
* @param dataSource the {@code javax.activation.DataSource} to take
* the content from, determining the InputStream and the content type
* @throws MessagingException in case of errors
* @see #addAttachment(String, org.springframework.core.io.InputStreamSource)
* @see #addAttachment(String, java.io.File)
*/
public void addAttachment(String attachmentFilename, DataSource dataSource) throws MessagingException {
Assert.notNull(attachmentFilename, "Attachment filename must not be null");
Assert.notNull(dataSource, "DataSource must not be null");
try {
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setDisposition(MimeBodyPart.ATTACHMENT);
mimeBodyPart.setFileName(MimeUtility.encodeText(attachmentFilename));
mimeBodyPart.setDataHandler(new DataHandler(dataSource));
getRootMimeMultipart().addBodyPart(mimeBodyPart);
}
catch (UnsupportedEncodingException ex) {
throw new MessagingException("Failed to encode attachment filename", ex);
}
}
代码示例来源:origin: camunda/camunda-bpm-platform
Object ccl = getAndSetContextClassLoader(MAILHANDLER_LOADER);
try {
MimeMultipart multipart = new MimeMultipart();
MimeBodyPart[] ambp = new MimeBodyPart[atn.length];
final MimeBodyPart body;
for (int i = 0; i < atn.length; ++i) {
ambp[i] = createBodyPart(i);
ambp[i].setFileName(atn[i]);
代码示例来源:origin: blynkkk/blynk-server
@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);
}
代码示例来源:origin: traccar/traccar
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();
}
}
内容来源于网络,如有侵权,请联系作者删除!