本文整理汇总了Java中javax.mail.internet.MimeBodyPart.getContent方法的典型用法代码示例。如果您正苦于以下问题:Java MimeBodyPart.getContent方法的具体用法?Java MimeBodyPart.getContent怎么用?Java MimeBodyPart.getContent使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.mail.internet.MimeBodyPart
的用法示例。
在下文中一共展示了MimeBodyPart.getContent方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doTestTextAttachment
import javax.mail.internet.MimeBodyPart; //导入方法依赖的package包/类
private void doTestTextAttachment(boolean useFs) throws IOException, MessagingException {
emailerConfig.setFileStorageUsed(useFs);
testMailSender.clearBuffer();
String attachmentText = "Test Attachment Text";
EmailAttachment textAttach = EmailAttachment.createTextAttachment(attachmentText, "ISO-8859-1", "test.txt");
EmailInfo myInfo = new EmailInfo("[email protected]", "Test", null, "Test", textAttach);
emailer.sendEmailAsync(myInfo);
emailer.processQueuedEmails();
MimeMessage msg = testMailSender.fetchSentEmail();
MimeBodyPart firstAttachment = getFirstAttachment(msg);
// check content bytes
Object content = firstAttachment.getContent();
assertTrue(content instanceof InputStream);
byte[] data = IOUtils.toByteArray((InputStream) content);
assertEquals(attachmentText, new String(data, "ISO-8859-1"));
// disposition
assertEquals(Part.ATTACHMENT, firstAttachment.getDisposition());
// charset header
String contentType = firstAttachment.getContentType();
assertTrue(contentType.toLowerCase().contains("charset=iso-8859-1"));
}
示例2: doTestInlineImage
import javax.mail.internet.MimeBodyPart; //导入方法依赖的package包/类
private void doTestInlineImage(boolean useFs) throws IOException, MessagingException {
emailerConfig.setFileStorageUsed(useFs);
testMailSender.clearBuffer();
byte[] imageBytes = new byte[]{1, 2, 3, 4, 5};
String fileName = "logo.png";
EmailAttachment imageAttach = new EmailAttachment(imageBytes, fileName, "logo");
EmailInfo myInfo = new EmailInfo("[email protected]", "Test", null, "Test", imageAttach);
emailer.sendEmailAsync(myInfo);
emailer.processQueuedEmails();
MimeMessage msg = testMailSender.fetchSentEmail();
MimeBodyPart attachment = getInlineAttachment(msg);
// check content bytes
InputStream content = (InputStream) attachment.getContent();
byte[] data = IOUtils.toByteArray(content);
assertByteArrayEquals(imageBytes, data);
// disposition
assertEquals(Part.INLINE, attachment.getDisposition());
// mime type
String contentType = attachment.getContentType();
assertTrue(contentType.contains("image/png"));
}
示例3: doTestPdfAttachment
import javax.mail.internet.MimeBodyPart; //导入方法依赖的package包/类
private void doTestPdfAttachment(boolean useFs) throws IOException, MessagingException {
emailerConfig.setFileStorageUsed(useFs);
testMailSender.clearBuffer();
byte[] pdfBytes = new byte[]{1, 2, 3, 4, 6};
String fileName = "invoice.pdf";
EmailAttachment pdfAttach = new EmailAttachment(pdfBytes, fileName);
EmailInfo myInfo = new EmailInfo("[email protected]", "Test", null, "Test", pdfAttach);
emailer.sendEmailAsync(myInfo);
emailer.processQueuedEmails();
MimeMessage msg = testMailSender.fetchSentEmail();
MimeBodyPart attachment = getFirstAttachment(msg);
// check content bytes
InputStream content = (InputStream) attachment.getContent();
byte[] data = IOUtils.toByteArray(content);
assertByteArrayEquals(pdfBytes, data);
// disposition
assertEquals(Part.ATTACHMENT, attachment.getDisposition());
// mime type
String contentType = attachment.getContentType();
assertTrue(contentType.contains("application/pdf"));
}
示例4: testSignedEncryptedAS2Message
import javax.mail.internet.MimeBodyPart; //导入方法依赖的package包/类
@Test
public void testSignedEncryptedAS2Message() throws Exception {
InputStream ins = FIXTURE_LOADER.getResourceAsStream(MOCK_AS2_MSG);
ByteArrayInputStream bIns = new ByteArrayInputStream(IOHandler.readBytes(ins));
// Prepare Data
String mid = RANDOM.toString();
partnershipDVO.setIsOutboundEncryptRequired(true);
partnershipDVO.setIsOutboundSignRequired(true);
//Encrypt message
AS2Message as2Msg = TARGET.storeOutgoingMessage(
mid, //MessageID
"xml",
partnershipDVO,
new InputStreamDataSource(bIns, "xml", MOCK_AS2_MSG));
// Decrypt Message
SMIMEEnveloped crypted = new SMIMEEnveloped(as2Msg.getBodyPart());
RecipientId recId = new JceKeyTransRecipientId(partnershipDVO.getEncryptX509Certificate());
RecipientInformationStore recipientsInfo = crypted.getRecipientInfos();
RecipientInformation recipientInfo = recipientsInfo.get(recId);
KeyStoreManager keyMan = (KeyStoreManager)TARGET.getSystemModule().getComponent("keystore-manager");
JceKeyTransEnvelopedRecipient recipient = new JceKeyTransEnvelopedRecipient(keyMan.getPrivateKey());
recipient.setProvider(SECURITY_PROVIDER);
MimeBodyPart decrpted = SMIMEUtil.toMimeBodyPart(recipientInfo.getContent(recipient));
//Verify Signature
try{
SMIMESigned signed = new SMIMESigned((MimeMultipart)decrpted.getContent());
SignerInformationStore signers = signed.getSignerInfos();
Iterator signerInfos = signers.getSigners().iterator();
while (signerInfos.hasNext()) {
SignerInformation signerInfo = (SignerInformation)signerInfos.next();
SignerInformationVerifier verifier =
new BcRSASignerInfoVerifierBuilder(new DefaultCMSSignatureAlgorithmNameGenerator(),
new DefaultSignatureAlgorithmIdentifierFinder(),
new DefaultDigestAlgorithmIdentifierFinder(),
new BcDigestCalculatorProvider())
.build(new JcaX509CertificateHolder(partnershipDVO.getEffectiveVerifyCertificate()));
if (!signerInfo.verify(verifier)) {
Assert.fail("Signature Verfifcation Failed");
}
}
//Assert the filename value
MimeBodyPart signedPart = signed.getContent();
String filenameHdr = signedPart.getHeader("Content-Disposition")[0];
Assert.assertEquals("Lost Filename Header Information", MOCK_AS2_MSG, getFileName(filenameHdr));
// Verify MIC Value
ByteArrayOutputStream baos = new ByteArrayOutputStream();
signedPart.writeTo(baos);
byte[] content = (baos.toByteArray());
String mic = calculateMIC(content);
MessageDVO msgDVO = getStoredMessage(mid);
Assert.assertEquals("MIC Value is not valid.", mic, msgDVO.getMicValue());
}catch(Exception exp){
Assert.fail("Signature Verfifcation Failed");
}
Assert.assertTrue(true);
}
示例5: getBody
import javax.mail.internet.MimeBodyPart; //导入方法依赖的package包/类
private String getBody(MimeMessage msg) throws Exception {
MimeBodyPart textPart = getTextPart(msg);
return (String) textPart.getContent();
}