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

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

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

MimeBodyPart.getContent介绍

[英]Return the content as a Java object. The type of the object returned is of course dependent on the content itself. For example, the native format of a text/plain content is usually a String object. The native format for a "multipart" content is always a Multipart subclass. For content types that are unknown to the DataHandler system, an input stream is returned as the content.

This implementation obtains the content from the DataHandler. That is, it invokes getDataHandler().getContent(); If the content is a Multipart or Message object and was created by parsing a stream, the object is cached and returned in subsequent calls so that modifications to the content will not be lost.
[中]将内容作为Java对象返回。返回的对象的类型当然取决于内容本身。例如,文本/纯文本内容的本机格式通常是字符串对象。“多部分”内容的本机格式始终是多部分子类。对于DataHandler系统未知的内容类型,将返回一个输入流作为内容。
此实现从DataHandler获取内容。也就是说,它调用getDataHandler()。getContent();如果内容是一个多部分或消息对象,并且是通过解析流创建的,那么该对象将被缓存并在后续调用中返回,这样对内容的修改就不会丢失。

代码示例

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

@Test
void testHtmlAndOneAttachment() throws MessagingException, IOException {
  Email email = Email.create()
    .from("inf0@jodd.org")
    .to("ig0r@gmail.com")
    .subject("test6")
    .textMessage("Hello!")
    .attachment(EmailAttachment.with().content(BYTES_11_15, APPLICATION_ZIP));
  Message message = createMessage(email);
  // wrapper
  final MimeMultipart multipart = (MimeMultipart) message.getContent();
  assertEquals(2, multipart.getCount());
  // inner content #1
  MimeBodyPart mimeBodyPart = (MimeBodyPart) multipart.getBodyPart(0);
  final MimeMultipart mimeMultipart = (MimeMultipart) mimeBodyPart.getContent();
  assertEquals(1, mimeMultipart.getCount());
  MimeBodyPart bodyPart = (MimeBodyPart) mimeMultipart.getBodyPart(0);
  assertEquals("Hello!", bodyPart.getContent());
}

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

SMIMEEnvelopedGenerator gen = new SMIMEEnvelopedGenerator();
JceKeyAgreeRecipientInfoGenerator rig = new JceKeyAgreeRecipientInfoGenerator(CMSAlgorithm.ECDH_SHA1KDF, senderPrivateKey, senderPublicKey, CMSAlgorithm.AES128_WRAP);
rig.setProvider("BC");
gen.addRecipientInfoGenerator(rig);

MimeBodyPart msg = new MimeBodyPart();
msg.setText("This is a secret message");

MimeBodyPart mp = gen.generate(msg, new JceCMSContentEncryptorBuilder(CMSAlgorithm.AES128_CBC).setProvider("BC").build());

Properties props = System.getProperties();
Session session = Session.getDefaultInstance(props, null);

String to = "bob@example.com";

Address fromUser = new InternetAddress("alice@example.com");
Address toUser = new InternetAddress(to);

MimeMessage body = new MimeMessage(session);
body.setFrom(fromUser);
body.setRecipient(Message.RecipientType.TO, toUser);
body.setSubject("example encrypted message");
body.setContent(mp.getContent(), mp.getContentType());
body.saveChanges();

body.writeTo(new FileOutputStream("/tmp/encrypted.msg"));

代码示例来源:origin: usnistgov/iheos-toolkit2

String type[] = bp.getContentType().split("/");
if (type[0].equals("text") && type[1].equals("plain")) {
  map.put(name, bp.getContent());
} else {

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

@Test
void testTextHtml() throws MessagingException, IOException {
  final Email email = Email.create()
    .from(FROM_EXAMPLE_COM)
    .to(TO_EXAMPLE_COM)
    .subject(SUB)
    .textMessage(HELLO)
    .htmlMessage("<html><body><h1>Hey!</h1></body></html>");
  final Message message = createMessage(email);
  assertEquals(1, message.getFrom().length);
  assertEquals(FROM_EXAMPLE_COM, message.getFrom()[0].toString());
  assertEquals(1, message.getRecipients(RecipientType.TO).length);
  assertEquals(TO_EXAMPLE_COM, message.getRecipients(RecipientType.TO)[0].toString());
  assertEquals(SUB, message.getSubject());
  // wrapper
  final MimeMultipart multipart = (MimeMultipart) message.getContent();
  assertEquals(1, multipart.getCount());
  assertTrue(multipart.getContentType().contains("multipart/mixed"));
  // inner content
  final MimeBodyPart mimeBodyPart = (MimeBodyPart) multipart.getBodyPart(0);
  final MimeMultipart mimeMultipart = (MimeMultipart) mimeBodyPart.getContent();
  assertEquals(2, mimeMultipart.getCount());
  assertTrue(mimeMultipart.getContentType().contains("multipart/alternative"));
  MimeBodyPart bodyPart = (MimeBodyPart) mimeMultipart.getBodyPart(0);
  assertEquals(HELLO, bodyPart.getContent());
  assertTrue(bodyPart.getDataHandler().getContentType().contains(MimeTypes.MIME_TEXT_PLAIN));
  bodyPart = (MimeBodyPart) mimeMultipart.getBodyPart(1);
  assertEquals("<html><body><h1>Hey!</h1></body></html>", bodyPart.getContent());
  assertTrue(bodyPart.getDataHandler().getContentType().contains(MimeTypes.MIME_TEXT_HTML));
}

代码示例来源:origin: usnistgov/iheos-toolkit2

String type[] = bp.getContentType().split("/");
if (type[0].equals("text") && type[1].equals("plain")) {
  map.put(name, bp.getContent());
} else {

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

final MimeMultipart mimeMultipart = (MimeMultipart) mimeBodyPart.getContent();
assertEquals(2, mimeMultipart.getCount());
assertEquals(HELLO, bodyPart.getContent());
final MimeMultipart htmlMessage = (MimeMultipart) bodyPart.getContent();
assertTrue(htmlMessage.getContentType().contains("multipart/related"));
assertEquals(2, htmlMessage.getCount());
assertEquals("<html><body><h1>Hey!</h1><img src='cid:c.png'></body></html>", htmlMimeBodyPart.getContent());
assertTrue(htmlMimeBodyPart.getDataHandler().getContentType().contains(MimeTypes.MIME_TEXT_HTML));

代码示例来源:origin: pavansolapure/opencodez-samples

public static MimeMessage encryptMessage(MimeMessage message) throws Exception  {
  
  Security.addProvider(new BouncyCastleProvider());
  
  // create the generator for creating an smime/encrypted message
  SMIMEEnvelopedGenerator  gen = new SMIMEEnvelopedGenerator();
  
  X509Certificate recipientCert = getRecipientPublicCertificate(message);
  
  gen.addRecipientInfoGenerator(new JceKeyTransRecipientInfoGenerator(recipientCert).setProvider("BC"));
  
  MimeBodyPart msg = new MimeBodyPart();
  msg.setContent(message.getContent(), message.getContentType());
  
  MimeBodyPart mp = gen.generate(msg, new JceCMSContentEncryptorBuilder(CMSAlgorithm.RC2_CBC).setProvider("BC").build());
  message.setContent(mp.getContent(), mp.getContentType());
  message.saveChanges();
  
  return message;
}

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

MimeBodyPart mbp = (MimeBodyPart)part;
if (mbp.isMimeType("text/plain")) {
  body += mbp.getContent().toString();
} else if (mbp.isMimeType("TEXT/HTML")) {
  body += mbp.getContent().toString(); 
}else if(mbp.isMimeType("text/xml")) {
  body += mbp.getContent().toString(); 
}

代码示例来源:origin: usnistgov/iheos-toolkit2

String type[] = bp.getContentType().split("/");
if (type[0].equals("text") && type[1].equals("plain")) {
  map.put(name, bp.getContent());
} else {

代码示例来源:origin: usnistgov/iheos-toolkit2

public InputStream getContentInputStream(int bodyPart) throws java.io.IOException,javax.mail.MessagingException{
  if (!isMultipart) {
    return in;
  }
  MimeBodyPart bp = (MimeBodyPart) mp.getBodyPart(bodyPart);
  Object o = bp.getContent();
  if (o instanceof java.io.InputStream) {
    return (InputStream) o;
  } else {
    throw new java.io.IOException("getContentInputStreamCannot handle data type " + o.getClass().getName() + " in SOAPLite");
  }
}

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

lines.add("file: " + fileName);
} else {
  lines.add(S.string(bp.getContent()));

代码示例来源:origin: usnistgov/iheos-toolkit2

public String getContent(int bodyPart) throws java.io.IOException,javax.mail.MessagingException{
  if (!isMultipart) {
    return body;
  }
  MimeBodyPart bp = (MimeBodyPart) mp.getBodyPart(bodyPart);
  Object o = bp.getContent();
  if (o.getClass().getName().equals("java.lang.String")) {
    return (String) o;
  } else if (o.getClass().getName().equals("java.io.ByteArrayInputStream")) {
    return translate( (ByteArrayInputStream) o);
  } else {
    throw new java.io.IOException("Cannot handle data type " + o.getClass().getName() + " in SOAPLite");
  }
}

代码示例来源:origin: org.actframework/act

lines.add("file: " + fileName);
} else {
  lines.add(S.string(bp.getContent()));

代码示例来源:origin: net.sourceforge.jadex/jadex-platform

if(part.getContent() instanceof Multipart)
  collectParts((Multipart)part.getContent(), contents, attachments);
    contents.add((String)part.getContent());
    attachments.add(part.getContent());

代码示例来源:origin: phax/as2-lib

throw new GeneralSecurityException ("Content-Type indicates data isn't signed: " + aPart.getContentType ());
final MimeMultipart aMainPart = (MimeMultipart) aPart.getContent ();

代码示例来源:origin: com.mgmtp.jfunk/jfunk-core

private static void parseMultipart(final StrBuilder sb, final Multipart multipart) throws MessagingException, IOException {
    for (int i = 0; i < multipart.getCount(); i++) {
      BodyPart bodyPart = multipart.getBodyPart(i);
      String disposition = bodyPart.getDisposition();

      if (disposition == null && bodyPart instanceof MimeBodyPart) { // not an attachment
        MimeBodyPart mimeBodyPart = (MimeBodyPart) bodyPart;

        if (mimeBodyPart.getContent() instanceof Multipart) {
          parseMultipart(sb, (Multipart) mimeBodyPart.getContent());
        } else {
          String body = (String) mimeBodyPart.getContent();
          sb.appendln(body);
          sb.appendln("");
        }
      }
    }
  }
}

代码示例来源:origin: com.axway.ats.framework/ats-actionlibrary

new JceCMSContentEncryptorBuilder(encryption).setProvider(BouncyCastleProvider.PROVIDER_NAME)
                                         .build());
encryptedMessage.setContent(mp.getContent(), mp.getContentType());
Enumeration<?> mpEnum = mp.getAllHeaders();
while (mpEnum.hasMoreElements()) {

代码示例来源:origin: org.apache.james/james-server-mailets

Object content = part.getContent();
if (content instanceof String) {
  return (String) part.getContent();
InputStream is = (InputStream) part.getContent();
Scanner scanner = null;
try {

代码示例来源:origin: mguessan/davmail

MimeBodyPart bodyPart = (MimeBodyPart) multiPart.getBodyPart(i);
try {
  Object mimeBody = bodyPart.getContent();
  if (mimeBody instanceof MimeMultipart) {
    appendBodyStructure(buffer, (MimeMultipart) mimeBody);

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

String contentType = message.getContentType();
String messageContent = "";
if (contentType.contains("multipart")) {
  // content may contain attachments
  Multipart multiPart = (Multipart) message.getContent();
  int numberOfParts = multiPart.getCount();
  for (int partCount = 0; partCount < numberOfParts; partCount++) {
    MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(partCount);
    if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
      // this part is attachment
      String fileName = part.getFileName();
      attachFiles += fileName + ", ";
      part.saveFile(saveDirectory + File.separator + fileName);
    } else {
      // this part may be the message content
      messageContent = part.getContent().toString();
    }
  }

  if (attachFiles.length() > 1) {
    attachFiles = attachFiles.substring(0, attachFiles.length() - 2);
  }
} else if (contentType.contains("text/plain")
    || contentType.contains("text/html")) {
  Object content = message.getContent();
  if (content != null) {
    messageContent = content.toString();
  }
}

相关文章