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

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

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

MimeBodyPart.<init>介绍

[英]An empty MimeBodyPart object is created. This body part maybe filled in by a client constructing a multipart message.
[中]将创建一个空的MimeBodyPart对象。这个主体部分可能由构建多部分消息的客户端填写。

代码示例

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

msg.addRecipients(MimeMessage.RecipientType.TO, toEmail.stream().map(e -> {
  try {
    return new InternetAddress(e);
  } catch (AddressException e1) {
    throw Throwables.propagate(e1);
  Multipart mp = new MimeMultipart();
  MimeBodyPart htmlPart = new MimeBodyPart();
  htmlPart.setContent(richText.get(), "text/html");
  mp.addBodyPart(htmlPart);
  parts.forEach(part -> {
    try {
      mp.addBodyPart(part);
    } catch (MessagingException e) {
      throw Throwables.propagate(e);

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

break;
case MULTIPART_MODE_MIXED:
  MimeMultipart mixedMultipart = new MimeMultipart(MULTIPART_SUBTYPE_MIXED);
  mimeMessage.setContent(mixedMultipart);
  setMimeMultiparts(mixedMultipart, mixedMultipart);
  break;
case MULTIPART_MODE_RELATED:
  MimeMultipart relatedMultipart = new MimeMultipart(MULTIPART_SUBTYPE_RELATED);
  mimeMessage.setContent(relatedMultipart);
  setMimeMultiparts(relatedMultipart, relatedMultipart);
  break;
case MULTIPART_MODE_MIXED_RELATED:
  MimeMultipart rootMixedMultipart = new MimeMultipart(MULTIPART_SUBTYPE_MIXED);
  mimeMessage.setContent(rootMixedMultipart);
  MimeMultipart nestedRelatedMultipart = new MimeMultipart(MULTIPART_SUBTYPE_RELATED);
  MimeBodyPart relatedBodyPart = new MimeBodyPart();
  relatedBodyPart.setContent(nestedRelatedMultipart);
  rootMixedMultipart.addBodyPart(relatedBodyPart);

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

/**
 * Add an inline element 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.
 * <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" -> "&lt;myId&gt;".
 * Can be referenced in HTML source via src="cid:myId" expressions.
 * @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 #addInline(String, java.io.File)
 * @see #addInline(String, org.springframework.core.io.Resource)
 */
public void addInline(String contentId, DataSource dataSource) throws MessagingException {
  Assert.notNull(contentId, "Content ID must not be null");
  Assert.notNull(dataSource, "DataSource must not be null");
  MimeBodyPart mimeBodyPart = new MimeBodyPart();
  mimeBodyPart.setDisposition(MimeBodyPart.INLINE);
  // We're using setHeader here to remain compatible with JavaMail 1.2,
  // rather than JavaMail 1.3's setContentID.
  mimeBodyPart.setHeader(HEADER_CONTENT_ID, "<" + contentId + ">");
  mimeBodyPart.setDataHandler(new DataHandler(dataSource));
  getMimeMultipart().addBodyPart(mimeBodyPart);
}

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

final Message message = sender.createMessage();
final InternetAddress from = new InternetAddress(this._fromAddress, false);
message.setFrom(from);
for (final String toAddr : this._toAddress) {
 message.addRecipient(Message.RecipientType.TO, new InternetAddress(
   toAddr, false));
   this._enableAttachementEmbedment ? new MimeMultipart("related")
     : new MimeMultipart();
 final BodyPart messageBodyPart = new MimeBodyPart();
 messageBodyPart.setContent(this._body.toString(), this._mimeType);
 multipart.addBodyPart(messageBodyPart);

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

private Multipart _multipart; 
_multipart = new MimeMultipart(); 

public void addAttachment(String filename,String subject) throws Exception { 
  BodyPart messageBodyPart = new MimeBodyPart(); 
  DataSource source = new FileDataSource(filename); 
  messageBodyPart.setDataHandler(new DataHandler(source)); 
  messageBodyPart.setFileName(filename); 
  _multipart.addBodyPart(messageBodyPart);

  BodyPart messageBodyPart2 = new MimeBodyPart(); 
  messageBodyPart2.setText(subject); 

  _multipart.addBodyPart(messageBodyPart2); 
} 


message.setContent(_multipart);

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

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;
}

代码示例来源:origin: webx/citrus

/** 渲染邮件内容。 */
public void render(Part mailPart) throws MessagingException {
  Multipart multipart = getMultipart();
  if (contents.isEmpty()) {
    throw new MessagingException("Empty multipart");
  }
  for (MailContent content : contents) {
    MimeBodyPart bodyPart = new MimeBodyPart();
    content.render(bodyPart);
    multipart.addBodyPart(bodyPart);
  }
  mailPart.setContent(multipart);
}

代码示例来源: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: stackoverflow.com

InternetAddress dests[] = new InternetAddress[emails.length];
for (int i = 0; i < emails.length; i++) {
  dests[i] = new InternetAddress(emails[i].trim().toLowerCase());
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);
message.setSentDate(new java.util.Date());

代码示例来源:origin: hortonworks/streamline

/**
 * Construct a {@link Message} from the map of message field values
 */
private Message getEmailMessage(Map<String, String> fields) throws MessagingException {
  Message msg = new MimeMessage(emailSession);
  msg.setFrom(new InternetAddress(fields.get(FIELD_FROM.key)));
  InternetAddress[] address = {new InternetAddress(fields.get(FIELD_TO.key))};
  msg.setRecipients(Message.RecipientType.TO, address);
  msg.setSubject(fields.get(FIELD_SUBJECT.key));
  msg.setSentDate(new Date());
  Multipart content = new MimeMultipart();
  MimeBodyPart mimeBodyPart = new MimeBodyPart();
  mimeBodyPart.setContent(fields.get(FIELD_BODY.key), fields.get(FIELD_CONTENT_TYPE.key));
  content.addBodyPart(mimeBodyPart);
  msg.setContent(content);
  return msg;
}

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

break;
case MULTIPART_MODE_MIXED:
  MimeMultipart mixedMultipart = new MimeMultipart(MULTIPART_SUBTYPE_MIXED);
  mimeMessage.setContent(mixedMultipart);
  setMimeMultiparts(mixedMultipart, mixedMultipart);
  break;
case MULTIPART_MODE_RELATED:
  MimeMultipart relatedMultipart = new MimeMultipart(MULTIPART_SUBTYPE_RELATED);
  mimeMessage.setContent(relatedMultipart);
  setMimeMultiparts(relatedMultipart, relatedMultipart);
  break;
case MULTIPART_MODE_MIXED_RELATED:
  MimeMultipart rootMixedMultipart = new MimeMultipart(MULTIPART_SUBTYPE_MIXED);
  mimeMessage.setContent(rootMixedMultipart);
  MimeMultipart nestedRelatedMultipart = new MimeMultipart(MULTIPART_SUBTYPE_RELATED);
  MimeBodyPart relatedBodyPart = new MimeBodyPart();
  relatedBodyPart.setContent(nestedRelatedMultipart);
  rootMixedMultipart.addBodyPart(relatedBodyPart);

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

/**
 * Add an inline element 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.
 * <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" -> "&lt;myId&gt;".
 * Can be referenced in HTML source via src="cid:myId" expressions.
 * @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 #addInline(String, java.io.File)
 * @see #addInline(String, org.springframework.core.io.Resource)
 */
public void addInline(String contentId, DataSource dataSource) throws MessagingException {
  Assert.notNull(contentId, "Content ID must not be null");
  Assert.notNull(dataSource, "DataSource must not be null");
  MimeBodyPart mimeBodyPart = new MimeBodyPart();
  mimeBodyPart.setDisposition(MimeBodyPart.INLINE);
  // We're using setHeader here to remain compatible with JavaMail 1.2,
  // rather than JavaMail 1.3's setContentID.
  mimeBodyPart.setHeader(HEADER_CONTENT_ID, "<" + contentId + ">");
  mimeBodyPart.setDataHandler(new DataHandler(dataSource));
  getMimeMultipart().addBodyPart(mimeBodyPart);
}

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

email_address = senderName + '<' + email_address + '>';
 msg.setFrom( new InternetAddress( email_address ) );
} else {
 throw new MessagingException( BaseMessages.getString( PKG, "Mail.Error.ReplyEmailNotFilled" ) );
  address[i] = new InternetAddress( reply_Address_List[i] );
InternetAddress[] address = new InternetAddress[destinations.length];
for ( int i = 0; i < destinations.length; i++ ) {
 address[i] = new InternetAddress( destinations[i] );
 messageText.append( Const.CR );
data.parts = new MimeMultipart();
MimeBodyPart part1 = new MimeBodyPart(); // put the text in the

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

part = new MimeBodyPart();
  part.setContent(s, layout.getContentType());
} else {
   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());
Multipart mp = new MimeMultipart();
mp.addBodyPart(part);
msg.setContent(mp);

代码示例来源: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;
}

相关文章