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

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

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

MimeBodyPart.setContent介绍

[英]A convenience method for setting this body part's content.

The content is wrapped in a DataHandler object. Note that a DataContentHandler class for the specified type should be available to the JavaMail implementation for this to work right. That is, to do setContent(foobar, "application/x-foobar"), a DataContentHandler for "application/x-foobar" should be installed. Refer to the Java Activation Framework for more information.
[中]设置此正文部分内容的简便方法。
内容包装在DataHandler对象中。请注意,指定类型的DataContentHandler类应可用于JavaMail实现,以使其正常工作。也就是说,要执行setContent(foobar, "application/x-foobar"),应该安装“application/x-foobar”的DataContentHandler。有关更多信息,请参阅Java激活框架。

代码示例

代码示例来源:origin: oblac/jodd

/**
 * Returns new {@link MimeBodyPart} with content set as msgMultipart.
 *
 * @param msgMultipart {@link MimeMultipart} to add to the new {@link MimeBodyPart}.
 * @return new {@link MimeBodyPart} with content set as msgMultipart.
 * @throws MessagingException if there is a failure.
 */
private MimeBodyPart getBaseBodyPart(final MimeMultipart msgMultipart) throws MessagingException {
  final MimeBodyPart bodyPart = new MimeBodyPart();
  bodyPart.setContent(msgMultipart);
  return bodyPart;
}

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

MimeBodyPart textPart = new MimeBodyPart();
textPart.setText(text, "utf-8");

MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(html, "text/html; charset=utf-8");

multiPart.addBodyPart(textPart); // <-- first
multiPart.addBodyPart(htmlPart); // <-- second
message.setContent(multiPart);

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

/**
 * Set the given plain text and HTML text as alternatives, offering
 * both options to the email client. Requires multipart mode.
 * <p><b>NOTE:</b> Invoke {@link #addInline} <i>after</i> {@code setText};
 * else, mail readers might not be able to resolve inline references correctly.
 * @param plainText the plain text for the message
 * @param htmlText the HTML text for the message
 * @throws MessagingException in case of errors
 */
public void setText(String plainText, String htmlText) throws MessagingException {
  Assert.notNull(plainText, "Plain text must not be null");
  Assert.notNull(htmlText, "HTML text must not be null");
  MimeMultipart messageBody = new MimeMultipart(MULTIPART_SUBTYPE_ALTERNATIVE);
  getMainPart().setContent(messageBody, CONTENT_TYPE_ALTERNATIVE);
  // Create the plain text part of the message.
  MimeBodyPart plainTextPart = new MimeBodyPart();
  setPlainTextToMimePart(plainTextPart, plainText);
  messageBody.addBodyPart(plainTextPart);
  // Create the HTML text part of the message.
  MimeBodyPart htmlTextPart = new MimeBodyPart();
  setHtmlTextToMimePart(htmlTextPart, htmlText);
  messageBody.addBodyPart(htmlTextPart);
}

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

@Override
public void sendHtmlWithAttachment(String to, String subj, String body,
                  QrHolder[] attachmentData) 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);
  attachQRs(multipart, attachmentData);
  attachCSV(multipart, attachmentData);
  message.setContent(multipart);
  Transport.send(message);
  log.trace("Mail to {} was sent. Subj : {}, body : {}", to, subj, body);
}

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

mimeMessage.setContent(rootMixedMultipart);
MimeMultipart nestedRelatedMultipart = new MimeMultipart(MULTIPART_SUBTYPE_RELATED);
MimeBodyPart relatedBodyPart = new MimeBodyPart();
relatedBodyPart.setContent(nestedRelatedMultipart);
rootMixedMultipart.addBodyPart(relatedBodyPart);
setMimeMultiparts(rootMixedMultipart, nestedRelatedMultipart);

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

final Message msg = new MimeMessage(session);
   msg.setFrom(new InternetAddress(senderAddress, senderDisplayName));
   msg.addRecipient(Message.RecipientType.TO,
       new InternetAddress(m.getRecipient(), m.getRecipientDisplayName()));
   msg.setSubject(m.getSubject());
   // Unformatted text version
   final MimeBodyPart textPart = new MimeBodyPart();
   textPart.setContent(m.getText(), "text/plain"); 
   // HTML version
   final MimeBodyPart htmlPart = new MimeBodyPart();
   htmlPart.setContent(m.getHtml(), "text/html");
   // Create the Multipart.  Add BodyParts to it.
   final Multipart mp = new MimeMultipart("alternative");
   mp.addBodyPart(textPart);
   mp.addBodyPart(htmlPart);
   // Set Multipart as the message's content
   msg.setContent(mp);
   LOGGER.log(Level.FINEST, "Sending email {0}", m);
   Transport.send(msg);

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

message.setSubject(subject, "UTF-8");
Multipart mp = new MimeMultipart();
MimeBodyPart mbp = new MimeBodyPart();
mbp.setContent(body, "text/html;charset=utf-8");
mp.addBodyPart(mbp);
message.setContent(mp);

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

part = new MimeBodyPart();
  part.setContent(s, layout.getContentType());
} else {
  try {
   headers.setHeader("Content-Type", layout.getContentType() + "; charset=UTF-8");
   headers.setHeader("Content-Transfer-Encoding", "quoted-printable");
   part = new MimeBodyPart(headers, os.toByteArray());
  } catch(Exception ex) {
    StringBuffer sbuf = new StringBuffer(s);
    part = new MimeBodyPart();
    part.setContent(sbuf.toString(), layout.getContentType());

代码示例来源: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: oblac/jodd

/**
 * @param emailMessage {@link EmailMessage} with data.
 * @param attachments  {@link List} of {@link EmailAttachment}s.
 * @return new {@link MimeBodyPart} with data from emailMessage and attachments.
 * @throws MessagingException if there is a failure.
 */
private MimeBodyPart getBodyPart(final EmailMessage emailMessage, final List<EmailAttachment<? extends DataSource>> attachments) throws MessagingException {
  final MimeBodyPart bodyPart = new MimeBodyPart();
  // detect embedded attachments
  final List<EmailAttachment<? extends DataSource>> embeddedAttachments = filterEmbeddedAttachments(attachments, emailMessage);
  if (embeddedAttachments.isEmpty()) {
    // no embedded attachments, just add message
    setContent(emailMessage, bodyPart);
  } else {
    attachments.removeAll(embeddedAttachments);
    // embedded attachments detected, join them as related
    final MimeMultipart relatedMultipart = new MimeMultipart(RELATED);
    final MimeBodyPart messageData = new MimeBodyPart();
    setContent(emailMessage, messageData);
    relatedMultipart.addBodyPart(messageData);
    addAnyAttachments(embeddedAttachments, relatedMultipart);
    bodyPart.setContent(relatedMultipart);
  }
  return bodyPart;
}

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

Multipart multipart = new MimeMultipart( "alternative" );
MimeBodyPart textPart = new MimeBodyPart();
textPart.setText( text, "utf-8" );
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent( html, "text/html; charset=utf-8" );

代码示例来源:origin: org.springframework/spring-context-support

/**
 * Set the given plain text and HTML text as alternatives, offering
 * both options to the email client. Requires multipart mode.
 * <p><b>NOTE:</b> Invoke {@link #addInline} <i>after</i> {@code setText};
 * else, mail readers might not be able to resolve inline references correctly.
 * @param plainText the plain text for the message
 * @param htmlText the HTML text for the message
 * @throws MessagingException in case of errors
 */
public void setText(String plainText, String htmlText) throws MessagingException {
  Assert.notNull(plainText, "Plain text must not be null");
  Assert.notNull(htmlText, "HTML text must not be null");
  MimeMultipart messageBody = new MimeMultipart(MULTIPART_SUBTYPE_ALTERNATIVE);
  getMainPart().setContent(messageBody, CONTENT_TYPE_ALTERNATIVE);
  // Create the plain text part of the message.
  MimeBodyPart plainTextPart = new MimeBodyPart();
  setPlainTextToMimePart(plainTextPart, plainText);
  messageBody.addBodyPart(plainTextPart);
  // Create the HTML text part of the message.
  MimeBodyPart htmlTextPart = new MimeBodyPart();
  setHtmlTextToMimePart(htmlTextPart, htmlText);
  messageBody.addBodyPart(htmlTextPart);
}

代码示例来源:origin: org.springframework/spring-context-support

mimeMessage.setContent(rootMixedMultipart);
MimeMultipart nestedRelatedMultipart = new MimeMultipart(MULTIPART_SUBTYPE_RELATED);
MimeBodyPart relatedBodyPart = new MimeBodyPart();
relatedBodyPart.setContent(nestedRelatedMultipart);
rootMixedMultipart.addBodyPart(relatedBodyPart);
setMimeMultiparts(rootMixedMultipart, nestedRelatedMultipart);

代码示例来源:origin: apache/usergrid

MimeBodyPart plainPart = new MimeBodyPart();
plainPart.setContent( plainText, "text/plain" );
plainPart.setHeader( MIME_VERSION, "1.0" );
plainPart.setHeader( CONTENT_TYPE, "text/plain; charset=iso-8859-1" );
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent( htmlText, "text/html" );
htmlPart.setHeader( MIME_VERSION, "1.0" );
htmlPart.setHeader( CONTENT_TYPE, "text/html; charset=iso-8859-1" );

代码示例来源:origin: igniterealtime/Openfire

MimeMultipart content = new MimeMultipart("alternative");
MimeBodyPart text = new MimeBodyPart();
text.setText(textBody, encoding);
text.setDisposition(Part.INLINE);
content.addBodyPart(text);
MimeBodyPart html = new MimeBodyPart();
html.setContent(htmlBody, "text/html; charset=UTF-8");
html.setDisposition(Part.INLINE);
html.setHeader("Content-Transfer-Encoding", "8bit");
MimeBodyPart bPart = new MimeBodyPart();
bPart.setText(textBody, encoding);
bPart.setDisposition(Part.INLINE);
MimeBodyPart bPart = new MimeBodyPart();
bPart.setContent(htmlBody, "text/html; charset=UTF-8");
bPart.setDisposition(Part.INLINE);
bPart.setHeader("Content-Transfer-Encoding", "8bit");

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

MimeBodyPart keyBody = new MimeBodyPart();
try {
  MimeBodyPart valueBody = new MimeBodyPart();
  try {
    valueBody.addHeader(CONTENT_TRANSFER_ENCODING, "binary");
    valueBody.addHeader(RestMessageHeaders.X_VOLD_VECTOR_CLOCK, eTag);
    valueBody.setContent(responseValue, "application/octet-stream");
    valueBody.addHeader(RestMessageHeaders.CONTENT_LENGTH,
              Integer.toString(responseValue.length));
  keyBody.setContent(multiPartValues);
  multiPartKeys.addBodyPart(keyBody);
} catch(MessagingException me) {

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

MimeBodyPart part1 = new MimeBodyPart(); // put the text in the
  part1.setContent( messageText.toString(), "text/html; " + "charset=" + meta.getEncoding() );
 } else {
  part1.setContent( messageText.toString(), "text/html; " + "charset=ISO-8859-1" );

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

MimeBodyPart body = new MimeBodyPart();
try {
  body.addHeader(CONTENT_TRANSFER_ENCODING, "binary");
  body.addHeader(RestMessageHeaders.X_VOLD_VECTOR_CLOCK, eTag);
  body.setContent(responseValue, "application/octet-stream");
  body.addHeader(RestMessageHeaders.CONTENT_LENGTH,
          Integer.toString(responseValue.length));

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

MimeBodyPart part1 = new MimeBodyPart(); // put the text in the
  part1.setContent( messageText.toString(), "text/html; " + "charset=" + getEncoding() );
 } else {
  part1.setContent( messageText.toString(), "text/html; " + "charset=ISO-8859-1" );
      MimeBodyPart files = new MimeBodyPart();
      URLDataSource fds = new URLDataSource( file.getURL() );
    MimeBodyPart files = new MimeBodyPart();
    FileDataSource fds = new FileDataSource( masterZipfile );

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

// Unformatted text version
   final MimeBodyPart textPart = new MimeBodyPart();
   textPart.setText("plain content");
   // HTML version
   final MimeBodyPart htmlPart = new MimeBodyPart();
   htmlPart.setContent("<b>html content</b>", "text/html");
   // Create the Multipart.  Add BodyParts to it.
   final Multipart mp = new MimeMultipart();
   mp.addBodyPart(textPart);
   mp.addBodyPart(htmlPart);
   // Set Multipart as the message's content
   msg.setContent(mp);

相关文章