javax.mail.Multipart.addBodyPart()方法的使用及代码示例

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

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

Multipart.addBodyPart介绍

[英]Adds a Part to the multipart. The BodyPart is appended to the list of existing Parts.
[中]将部件添加到多部件。车身零件将附加到现有零件列表中。

代码示例

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

/**
 * Creates a Multipart from present parts.
 * @return multipart with all present parts
 * @throws MessagingException
 */
private Multipart createMultiPart() throws MessagingException {
  Multipart multipart = new MimeMultipart("alternative");
  if (textPart != null) {
    // add text first, to give priority to html
    multipart.addBodyPart((BodyPart) createTextMimePart());
  }
  if (htmlPart != null) {
    multipart.addBodyPart((BodyPart) createHtmlMimePart());
  }
  return multipart;
}

代码示例来源: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: 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: org.apache.james/james-server-jmap

@VisibleForTesting
void processTextBodyAsAttachment(MimeMessage mimeMessage) throws MessagingException {
  List<Header> contentHeaders = getContentHeadersFromMimeMessage(mimeMessage);
  removeAllContentHeaderFromMimeMessage(mimeMessage, contentHeaders);
  Multipart multipart = new MimeMultipart();
  multipart.addBodyPart(createMimeBodyPartWithContentHeadersFromMimeMessage(mimeMessage, contentHeaders));
  mimeMessage.setContent(multipart);
  mimeMessage.saveChanges();
}

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

InternetAddress dests[] = new InternetAddress[emails.length];
for (int i = 0; i < emails.length; i++) {
  dests[i] = new InternetAddress(emails[i].trim().toLowerCase());
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: stackoverflow.com

private static void addAttachment(Multipart multipart, String filename)
{
  DataSource source = new FileDataSource(filename);
  BodyPart messageBodyPart = new MimeBodyPart();        
  messageBodyPart.setDataHandler(new DataHandler(source));
  messageBodyPart.setFileName(filename);
  multipart.addBodyPart(messageBodyPart);
}

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

代码示例来源: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: 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: apache/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: looly/hutool

/**
 * 构建邮件信息主体
 * 
 * @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;
}

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

private void renderInlineResource(Multipart multipart, InlineResource inlineResource, Set<String> fileNames)
    throws MessagingException {
  assertNotNull(resourceLoader, "no resourceLoader");
  String resourceName = inlineResource.getResourceName();
  Resource resource = resourceLoader.getResource(resourceName);
  if (!resource.exists()) {
    throw new MailBuilderException("Could not find resource \"" + resourceName + "\"");
  }
  DataSource ds;
  try {
    ds = new URLDataSource(resource.getURL());
  } catch (IOException e) {
    ds = new ResourceDataSource(resource);
  }
  MimeBodyPart bodyPart = new MimeBodyPart();
  bodyPart.setDataHandler(new DataHandler(ds));
  bodyPart.setHeader(CONTENT_ID, "<" + inlineResource.getContentId() + ">");
  bodyPart.setFileName(inlineResource.getUniqueFilename(fileNames));
  bodyPart.setDisposition("inline");
  multipart.addBodyPart(bodyPart);
}

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

Multipart mp = new MimeMultipart();
MimeBodyPart mbp = new MimeBodyPart();
mbp.setContent(message, "text/plain; charset=ISO-8859-7");
mp.addBodyPart(mbp);

msg.setContent(mp);

代码示例来源: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" );
multipart.addBodyPart( textPart );
multipart.addBodyPart( htmlPart );
message.setContent( multipart );

相关文章