本文整理汇总了Java中javax.mail.util.ByteArrayDataSource类的典型用法代码示例。如果您正苦于以下问题:Java ByteArrayDataSource类的具体用法?Java ByteArrayDataSource怎么用?Java ByteArrayDataSource使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ByteArrayDataSource类属于javax.mail.util包,在下文中一共展示了ByteArrayDataSource类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: populateContentOnBodyPart
import javax.mail.util.ByteArrayDataSource; //导入依赖的package包/类
protected String populateContentOnBodyPart(BodyPart part, MailConfiguration configuration, Exchange exchange)
throws MessagingException, IOException {
String contentType = determineContentType(configuration, exchange);
if (contentType != null) {
LOG.trace("Using Content-Type {} for BodyPart: {}", contentType, part);
// always store content in a byte array data store to avoid various content type and charset issues
String data = exchange.getContext().getTypeConverter().tryConvertTo(String.class, exchange.getIn().getBody());
// use empty data if the body was null for some reason (otherwise there is a NPE)
data = data != null ? data : "";
DataSource ds = new ByteArrayDataSource(data, contentType);
part.setDataHandler(new DataHandler(ds));
// set the content type header afterwards
part.setHeader("Content-Type", contentType);
}
return contentType;
}
示例2: main
import javax.mail.util.ByteArrayDataSource; //导入依赖的package包/类
public static void main(final String[] args)
throws Exception {
final EmailPopulatingBuilder emailPopulatingBuilderNormal = EmailBuilder.startingBlank();
emailPopulatingBuilderNormal.from("lollypop", "[email protected]");
// don't forget to add your own address here ->
emailPopulatingBuilderNormal.to("C.Cane", YOUR_GMAIL_ADDRESS);
emailPopulatingBuilderNormal.withPlainText("We should meet up!");
emailPopulatingBuilderNormal.withHTMLText("<b>We should meet up!</b><img src='cid:thumbsup'>");
emailPopulatingBuilderNormal.withSubject("hey");
// add two text files in different ways and a black thumbs up embedded image ->
emailPopulatingBuilderNormal.withAttachment("dresscode.txt", new ByteArrayDataSource("Black Tie Optional", "text/plain"));
emailPopulatingBuilderNormal.withAttachment("location.txt", "On the moon!".getBytes(Charset.defaultCharset()), "text/plain");
String base64String = "iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAABeElEQVRYw2NgoAAYGxu3GxkZ7TY1NZVloDcAWq4MxH+B+D8Qv3FwcOCgtwM6oJaDMTAUXOhmuYqKCjvQ0pdoDrCnmwNMTEwakC0H4u8GBgYC9Ap6DSD+iewAoIPm0ctyLqBlp9F8/x+YE4zpYT8T0LL16JYD8U26+B7oyz4sloPwenpYno3DchCeROsUbwa05A8eB3wB4kqgIxOAuArIng7EW4H4EhC/B+JXQLwDaI4ryZaDSjeg5mt4LCcFXyIn1fdSyXJQVt1OtMWGhoai0OD8T0W8GohZifE1PxD/o7LlsPLiFNAKRrwOABWptLAcqc6QGDAHQEOAYaAc8BNotsJAOgAUAosG1AFA/AtUoY3YEFhKMAvS2AE7iC1+WaG1H6gY3gzE36hUFJ8mqzbU1dUVBBqQBzTgIDQRkWo5qCZdpaenJ0Zx1aytrc0DDB0foIG1oAYKqC0IZK8D4n1AfA6IzwPxXpCFoGoZVEUDaRGGUTAKRgEeAAA2eGJC+ETCiAAAAABJRU5ErkJggg==";
emailPopulatingBuilderNormal.withEmbeddedImage("thumbsup", parseBase64Binary(base64String), "image/png");
// let's try producing and then consuming a MimeMessage ->
Email emailNormal = emailPopulatingBuilderNormal.buildEmail();
final MimeMessage mimeMessage = EmailConverter.emailToMimeMessage(emailNormal);
final Email emailFromMimeMessage = EmailConverter.mimeMessageToEmail(mimeMessage);
// note: the following statements will produce 6 new emails!
sendMail(emailNormal);
sendMail(emailFromMimeMessage); // should produce the exact same result as emailPopulatingBuilderNormal!
}
示例3: readFrom
import javax.mail.util.ByteArrayDataSource; //导入依赖的package包/类
@Override
public SignedInput readFrom(Class<SignedInput> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> headers, InputStream entityStream) throws IOException, WebApplicationException {
Class<?> baseType = null;
Type baseGenericType = null;
if (genericType instanceof ParameterizedType) {
ParameterizedType param = (ParameterizedType) genericType;
baseGenericType = param.getActualTypeArguments()[0];
baseType = Types.getRawType(baseGenericType);
}
try {
ByteArrayDataSource ds = new ByteArrayDataSource(entityStream, mediaType.toString());
MimeMultipart mm = new MimeMultipart(ds);
SignedInputImpl input = new SignedInputImpl();
input.setType(baseType);
input.setGenericType(baseGenericType);
input.setAnnotations(annotations);
input.setBody(mm);
input.setProviders(providers);
return input;
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
示例4: createMimeMessageAttachments
import javax.mail.util.ByteArrayDataSource; //导入依赖的package包/类
/**
* Creates a list of {@link MimeMessage} attachments using the given incoming e-mail.
*
* @param email The parsed e-mail.
* @return A list containing the attachments, if any.
* @throws Exception If there is an error while constructing the list of attachments.
*/
private List<BodyPart> createMimeMessageAttachments(Email email) throws Exception {
List<BodyPart> attachments = new LinkedList<>();
for (Attachment attachment : email.getAttachments()) {
BodyPart attachmentBodyPart = new MimeBodyPart();
attachmentBodyPart.setFileName(attachment.getAttachmentName());
byte[] data = ByteStreams.toByteArray(attachment.getIs());
DataSource source = new ByteArrayDataSource(data, "application/octet-stream");
attachmentBodyPart.setDataHandler(new DataHandler(source));
attachments.add(attachmentBodyPart);
}
return attachments;
}
示例5: main
import javax.mail.util.ByteArrayDataSource; //导入依赖的package包/类
public static void main(final String[] args) throws Exception {
clearConfigProperties();
final Email emailNormal = new Email();
emailNormal.setFromAddress("lollypop", "[email protected]");
// don't forget to add your own address here ->
emailNormal.addRecipient("C.Cane", YOUR_GMAIL_ADDRESS, RecipientType.TO);
emailNormal.setText("We should meet up!");
emailNormal.setTextHTML("<b>We should meet up!</b><img src='cid:thumbsup'>");
emailNormal.setSubject("hey");
// add two text files in different ways and a black thumbs up embedded image ->
emailNormal.addAttachment("dresscode.txt", new ByteArrayDataSource("Black Tie Optional", "text/plain"));
emailNormal.addAttachment("location.txt", "On the moon!".getBytes(Charset.defaultCharset()), "text/plain");
String base64String = "iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAABeElEQVRYw2NgoAAYGxu3GxkZ7TY1NZVloDcAWq4MxH+B+D8Qv3FwcOCgtwM6oJaDMTAUXOhmuYqKCjvQ0pdoDrCnmwNMTEwakC0H4u8GBgYC9Ap6DSD+iewAoIPm0ctyLqBlp9F8/x+YE4zpYT8T0LL16JYD8U26+B7oyz4sloPwenpYno3DchCeROsUbwa05A8eB3wB4kqgIxOAuArIng7EW4H4EhC/B+JXQLwDaI4ryZaDSjeg5mt4LCcFXyIn1fdSyXJQVt1OtMWGhoai0OD8T0W8GohZifE1PxD/o7LlsPLiFNAKRrwOABWptLAcqc6QGDAHQEOAYaAc8BNotsJAOgAUAosG1AFA/AtUoY3YEFhKMAvS2AE7iC1+WaG1H6gY3gzE36hUFJ8mqzbU1dUVBBqQBzTgIDQRkWo5qCZdpaenJ0Zx1aytrc0DDB0foIG1oAYKqC0IZK8D4n1AfA6IzwPxXpCFoGoZVEUDaRGGUTAKRgEeAAA2eGJC+ETCiAAAAABJRU5ErkJggg==";
emailNormal.addEmbeddedImage("thumbsup", parseBase64Binary(base64String), "image/png");
// let's try producing and then consuming a MimeMessage ->
final MimeMessage mimeMessage = Mailer.produceMimeMessage(emailNormal);
final Email emailFromMimeMessage = new Email(mimeMessage);
// note: the following statements will produce 6 new emails!
sendMail(emailNormal);
sendMail(emailFromMimeMessage); // should produce the exact same result as emailNormal!
}
示例6: sendBugReport
import javax.mail.util.ByteArrayDataSource; //导入依赖的package包/类
public void sendBugReport(BugReport report) throws MailException, MessagingException {
MimeMessage message = this.mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setTo(bugReportEmail);
helper.setFrom(from);
helper.setSubject(report.getSubject());
helper.setText(report.getDescription());
if (report.getAttachments() != null) {
for (BugReportAttachment attachment : report.getAttachments()) {
// Decode base64 encoded data
byte[] data = Base64.getDecoder().decode(attachment.getData());
ByteArrayDataSource dataSource = new ByteArrayDataSource(data, attachment.getMimetype());
helper.addAttachment(attachment.getName(), dataSource);
}
}
this.mailSender.send(message);
}
示例7: testConsumer
import javax.mail.util.ByteArrayDataSource; //导入依赖的package包/类
@Test
public void testConsumer() throws Exception {
if (MtomTestHelper.isAwtHeadless(logger, null)) {
return;
}
context.createProducerTemplate().send("cxf:bean:consumerEndpoint", new Processor() {
public void process(Exchange exchange) throws Exception {
exchange.setPattern(ExchangePattern.InOut);
assertEquals("Get a wrong Content-Type header", "application/xop+xml", exchange.getIn().getHeader("Content-Type"));
List<Source> elements = new ArrayList<Source>();
elements.add(new DOMSource(StaxUtils.read(new StringReader(getRequestMessage())).getDocumentElement()));
CxfPayload<SoapHeader> body = new CxfPayload<SoapHeader>(new ArrayList<SoapHeader>(),
elements, null);
exchange.getIn().setBody(body);
exchange.getIn().addAttachment(MtomTestHelper.REQ_PHOTO_CID,
new DataHandler(new ByteArrayDataSource(MtomTestHelper.REQ_PHOTO_DATA, "application/octet-stream")));
exchange.getIn().addAttachment(MtomTestHelper.REQ_IMAGE_CID,
new DataHandler(new ByteArrayDataSource(MtomTestHelper.requestJpeg, "image/jpeg")));
}
});
}
示例8: process
import javax.mail.util.ByteArrayDataSource; //导入依赖的package包/类
@SuppressWarnings("unchecked")
public void process(Exchange exchange) throws Exception {
CxfPayload<SoapHeader> in = exchange.getIn().getBody(CxfPayload.class);
// verify request
Assert.assertEquals(1, in.getBody().size());
DataHandler dr = exchange.getIn().getAttachment(MtomTestHelper.REQ_PHOTO_CID);
Assert.assertEquals("application/octet-stream", dr.getContentType());
MtomTestHelper.assertEquals(MtomTestHelper.REQ_PHOTO_DATA, IOUtils.readBytesFromStream(dr.getInputStream()));
dr = exchange.getIn().getAttachment(MtomTestHelper.REQ_IMAGE_CID);
Assert.assertEquals("image/jpeg", dr.getContentType());
MtomTestHelper.assertEquals(MtomTestHelper.requestJpeg, IOUtils.readBytesFromStream(dr.getInputStream()));
// create response
List<Source> elements = new ArrayList<Source>();
elements.add(new DOMSource(StaxUtils.read(new StringReader(MtomTestHelper.MTOM_DISABLED_RESP_MESSAGE)).getDocumentElement()));
CxfPayload<SoapHeader> body = new CxfPayload<SoapHeader>(new ArrayList<SoapHeader>(),
elements, null);
exchange.getOut().setBody(body);
exchange.getOut().addAttachment(MtomTestHelper.RESP_PHOTO_CID,
new DataHandler(new ByteArrayDataSource(MtomTestHelper.RESP_PHOTO_DATA, "application/octet-stream")));
exchange.getOut().addAttachment(MtomTestHelper.RESP_IMAGE_CID,
new DataHandler(new ByteArrayDataSource(MtomTestHelper.responseJpeg, "image/jpeg")));
}
示例9: populateContentOnMimeMessage
import javax.mail.util.ByteArrayDataSource; //导入依赖的package包/类
protected String populateContentOnMimeMessage(MimeMessage part, MailConfiguration configuration, Exchange exchange)
throws MessagingException, IOException {
String contentType = determineContentType(configuration, exchange);
LOG.trace("Using Content-Type {} for MimeMessage: {}", contentType, part);
String body = exchange.getIn().getBody(String.class);
if (body == null) {
body = "";
}
// always store content in a byte array data store to avoid various content type and charset issues
DataSource ds = new ByteArrayDataSource(body, contentType);
part.setDataHandler(new DataHandler(ds));
// set the content type header afterwards
part.setHeader("Content-Type", contentType);
return contentType;
}
示例10: roundtripWithBinaryAttachments
import javax.mail.util.ByteArrayDataSource; //导入依赖的package包/类
@Test
public void roundtripWithBinaryAttachments() throws IOException {
String attContentType = "application/binary";
byte[] attText = {0, 1, 2, 3, 4, 5, 6, 7};
String attFileName = "Attachment File Name";
in.setBody("Body text");
DataSource ds = new ByteArrayDataSource(attText, attContentType);
in.addAttachment(attFileName, new DataHandler(ds));
Exchange result = template.send("direct:roundtrip", exchange);
Message out = result.getOut();
assertEquals("Body text", out.getBody(String.class));
assertTrue(out.hasAttachments());
assertEquals(1, out.getAttachmentNames().size());
assertThat(out.getAttachmentNames(), hasItem(attFileName));
DataHandler dh = out.getAttachment(attFileName);
assertNotNull(dh);
assertEquals(attContentType, dh.getContentType());
InputStream is = dh.getInputStream();
ByteArrayOutputStream os = new ByteArrayOutputStream();
IOHelper.copyAndCloseInput(is, os);
assertArrayEquals(attText, os.toByteArray());
}
示例11: roundtripWithBinaryAttachmentsAndBinaryContent
import javax.mail.util.ByteArrayDataSource; //导入依赖的package包/类
@Test
public void roundtripWithBinaryAttachmentsAndBinaryContent() throws IOException {
String attContentType = "application/binary";
byte[] attText = {0, 1, 2, 3, 4, 5, 6, 7};
String attFileName = "Attachment File Name";
in.setBody("Body text");
DataSource ds = new ByteArrayDataSource(attText, attContentType);
in.addAttachment(attFileName, new DataHandler(ds));
Exchange result = template.send("direct:roundtripbinarycontent", exchange);
Message out = result.getOut();
assertEquals("Body text", out.getBody(String.class));
assertTrue(out.hasAttachments());
assertEquals(1, out.getAttachmentNames().size());
assertThat(out.getAttachmentNames(), hasItem(attFileName));
DataHandler dh = out.getAttachment(attFileName);
assertNotNull(dh);
assertEquals(attContentType, dh.getContentType());
InputStream is = dh.getInputStream();
ByteArrayOutputStream os = new ByteArrayOutputStream();
IOHelper.copyAndCloseInput(is, os);
assertArrayEquals(attText, os.toByteArray());
}
示例12: addOneDocument
import javax.mail.util.ByteArrayDataSource; //导入依赖的package包/类
protected OMElement addOneDocument(OMElement request, String document, String documentId) throws IOException {
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace ns = fac.createOMNamespace("urn:ihe:iti:xds-b:2007" , null);
OMElement docElem = fac.createOMElement("Document", ns);
docElem.addAttribute("id", documentId, null);
// A string, turn it into an StreamSource
DataSource ds = new ByteArrayDataSource(document, "text/xml");
DataHandler handler = new DataHandler(ds);
OMText binaryData = fac.createOMText(handler, true);
docElem.addChild(binaryData);
Iterator iter = request.getChildrenWithLocalName("SubmitObjectsRequest");
OMElement submitObjectsRequest = null;
for (;iter.hasNext();) {
submitObjectsRequest = (OMElement)iter.next();
if (submitObjectsRequest != null)
break;
}
submitObjectsRequest.insertSiblingAfter(docElem);
return request;
}
示例13: testBuilderSimpleBuildWithStandardEmail
import javax.mail.util.ByteArrayDataSource; //导入依赖的package包/类
@Test
public void testBuilderSimpleBuildWithStandardEmail()
throws IOException {
ByteArrayDataSource namedAttachment = new ByteArrayDataSource("Black Tie Optional", "text/plain");
namedAttachment.setName("dresscode.txt"); // normally not needed, but otherwise the equals will fail
String base64String = "iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAABeElEQVRYw2NgoAAYGxu3GxkZ7TY1NZVloDcAWq4MxH+B+D8Qv3FwcOCgtwM6oJaDMTAUXOhmuYqKCjvQ0pdoDrCnmwNMTEwakC0H4u8GBgYC9Ap6DSD+iewAoIPm0ctyLqBlp9F8/x+YE4zpYT8T0LL16JYD8U26+B7oyz4sloPwenpYno3DchCeROsUbwa05A8eB3wB4kqgIxOAuArIng7EW4H4EhC/B+JXQLwDaI4ryZaDSjeg5mt4LCcFXyIn1fdSyXJQVt1OtMWGhoai0OD8T0W8GohZifE1PxD/o7LlsPLiFNAKRrwOABWptLAcqc6QGDAHQEOAYaAc8BNotsJAOgAUAosG1AFA/AtUoY3YEFhKMAvS2AE7iC1+WaG1H6gY3gzE36hUFJ8mqzbU1dUVBBqQBzTgIDQRkWo5qCZdpaenJ0Zx1aytrc0DDB0foIG1oAYKqC0IZK8D4n1AfA6IzwPxXpCFoGoZVEUDaRGGUTAKRgEeAAA2eGJC+ETCiAAAAABJRU5ErkJggg==";
final Email email = EmailBuilder.startingBlank()
.from("lollypop", "[email protected]")
.to("C.Cane", "[email protected]")
.withPlainText("We should meet up!")
.withHTMLText("<b>We should meet up!</b><img src='cid:thumbsup'>")
.withSubject("hey")
.withAttachment("dresscode.txt", namedAttachment)
.withAttachment("location.txt", "On the moon!".getBytes(Charset.defaultCharset()), "text/plain")
.withEmbeddedImage("thumbsup", parseBase64Binary(base64String), "image/png")
.buildEmail();
assertThat(EmailHelper.createDummyEmailBuilder(true, true, false).buildEmail()).isEqualTo(email);
}
开发者ID:bbottema,项目名称:simple-java-mail,代码行数:21,代码来源:EmailPopulatingBuilderUsingDefaultsFromPropertyFileTest.java
示例14: testBuilderSimpleBuildWithStandardEmail_PlusOptionals
import javax.mail.util.ByteArrayDataSource; //导入依赖的package包/类
@Test
public void testBuilderSimpleBuildWithStandardEmail_PlusOptionals()
throws IOException {
ByteArrayDataSource namedAttachment = new ByteArrayDataSource("Black Tie Optional", "text/plain");
namedAttachment.setName("dresscode.txt"); // normally not needed, but otherwise the equals will fail
String base64String = "iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAABeElEQVRYw2NgoAAYGxu3GxkZ7TY1NZVloDcAWq4MxH+B+D8Qv3FwcOCgtwM6oJaDMTAUXOhmuYqKCjvQ0pdoDrCnmwNMTEwakC0H4u8GBgYC9Ap6DSD+iewAoIPm0ctyLqBlp9F8/x+YE4zpYT8T0LL16JYD8U26+B7oyz4sloPwenpYno3DchCeROsUbwa05A8eB3wB4kqgIxOAuArIng7EW4H4EhC/B+JXQLwDaI4ryZaDSjeg5mt4LCcFXyIn1fdSyXJQVt1OtMWGhoai0OD8T0W8GohZifE1PxD/o7LlsPLiFNAKRrwOABWptLAcqc6QGDAHQEOAYaAc8BNotsJAOgAUAosG1AFA/AtUoY3YEFhKMAvS2AE7iC1+WaG1H6gY3gzE36hUFJ8mqzbU1dUVBBqQBzTgIDQRkWo5qCZdpaenJ0Zx1aytrc0DDB0foIG1oAYKqC0IZK8D4n1AfA6IzwPxXpCFoGoZVEUDaRGGUTAKRgEeAAA2eGJC+ETCiAAAAABJRU5ErkJggg==";
final Email email = EmailBuilder.startingBlank()
.from("lollypop", "[email protected]")
.withReplyTo("lollypop-reply", "[email protected]")
.withBounceTo("lollypop-bounce", "[email protected]")
.to("C.Cane", "[email protected]")
.withPlainText("We should meet up!")
.withHTMLText("<b>We should meet up!</b><img src='cid:thumbsup'>")
.withSubject("hey")
.withAttachment("dresscode.txt", namedAttachment)
.withAttachment("location.txt", "On the moon!".getBytes(Charset.defaultCharset()), "text/plain")
.withEmbeddedImage("thumbsup", parseBase64Binary(base64String), "image/png")
.withDispositionNotificationTo("[email protected]")
.withReturnReceiptTo("Complex Email", "[email protected]")
.withHeader("dummyHeader", "dummyHeaderValue")
.buildEmail();
assertThat(EmailHelper.createDummyEmailBuilder(true, false, true).buildEmail()).isEqualTo(email);
}
开发者ID:bbottema,项目名称:simple-java-mail,代码行数:26,代码来源:EmailPopulatingBuilderUsingDefaultsFromPropertyFileTest.java
示例15: appendIcsBody
import javax.mail.util.ByteArrayDataSource; //导入依赖的package包/类
protected MimeMessage appendIcsBody(MimeMessage msg, MailMessage m) throws Exception {
log.debug("setMessageBody for iCal message");
// -- Create a new message --
Multipart multipart = new MimeMultipart();
Multipart multiBody = new MimeMultipart("alternative");
BodyPart html = new MimeBodyPart();
html.setDataHandler(new DataHandler(new ByteArrayDataSource(m.getBody(), "text/html; charset=UTF-8")));
multiBody.addBodyPart(html);
BodyPart iCalContent = new MimeBodyPart();
iCalContent.addHeader("content-class", "urn:content-classes:calendarmessage");
iCalContent.setDataHandler(new DataHandler(new ByteArrayDataSource(new ByteArrayInputStream(m.getIcs()),
"text/calendar; charset=UTF-8; method=REQUEST")));
multiBody.addBodyPart(iCalContent);
BodyPart body = new MimeBodyPart();
body.setContent(multiBody);
multipart.addBodyPart(body);
BodyPart iCalAttachment = new MimeBodyPart();
iCalAttachment.setDataHandler(new DataHandler(new ByteArrayDataSource(new ByteArrayInputStream(m.getIcs()),
"application/ics")));
iCalAttachment.removeHeader("Content-Transfer-Encoding");
iCalAttachment.addHeader("Content-Transfer-Encoding", "base64");
iCalAttachment.removeHeader("Content-Type");
iCalAttachment.addHeader("Content-Type", "application/ics");
iCalAttachment.setFileName("invite.ics");
multipart.addBodyPart(iCalAttachment);
msg.setContent(multipart);
return msg;
}