本文整理汇总了Java中javax.mail.BodyPart.setDataHandler方法的典型用法代码示例。如果您正苦于以下问题:Java BodyPart.setDataHandler方法的具体用法?Java BodyPart.setDataHandler怎么用?Java BodyPart.setDataHandler使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.mail.BodyPart
的用法示例。
在下文中一共展示了BodyPart.setDataHandler方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: buildMessage
import javax.mail.BodyPart; //导入方法依赖的package包/类
private static Message buildMessage(Session session, String from, String recipients, String subject, String text, String filename) throws MessagingException, AddressException
{
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
message.setSubject(subject);
BodyPart messageTextPart = new MimeBodyPart();
messageTextPart.setText(text);
BodyPart messageAttachmentPart = new MimeBodyPart();
DataSource source = new FileDataSource(new File(filename));
messageAttachmentPart.setDataHandler(new DataHandler(source));
messageAttachmentPart.setFileName(filename);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageTextPart);
multipart.addBodyPart(messageAttachmentPart);
message.setContent(multipart);
return message;
}
示例2: populateContentOnBodyPart
import javax.mail.BodyPart; //导入方法依赖的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;
}
示例3: submitFaxJobValidTest
import javax.mail.BodyPart; //导入方法依赖的package包/类
/**
* Test
*
* @throws Exception
* Any exception
*/
@Test
public void submitFaxJobValidTest() throws Exception
{
Message message=new MimeMessage((Session)null);
message.setSubject("fax:123456789");
message.setFrom(new InternetAddress("[email protected]"));
File file=File.createTempFile("temp_",".txt");
file.deleteOnExit();
IOHelper.writeTextFile("abc",file);
DataSource source=new FileDataSource(file);
BodyPart messageFileAttachmentBodyPart=new MimeBodyPart();
messageFileAttachmentBodyPart.setDataHandler(new DataHandler(source));
messageFileAttachmentBodyPart.setFileName(file.getName());
Multipart multipart=new MimeMultipart();
multipart.addBodyPart(messageFileAttachmentBodyPart);
message.setContent(multipart);
FaxJob faxJob=this.faxBridge.submitFaxJob(message);
Assert.assertNotNull(faxJob);
Assert.assertNotNull(faxJob.getFile());
file.delete();
}
示例4: sendAttachMail
import javax.mail.BodyPart; //导入方法依赖的package包/类
public boolean sendAttachMail(MailSenderInfo mailInfo) {
MyAuthenticator authenticator = null;
Properties pro = mailInfo.getProperties();
if (mailInfo.isValidate()) {
authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
}
try {
Message mailMessage = new MimeMessage(Session.getInstance(pro, authenticator));
mailMessage.setFrom(new InternetAddress(mailInfo.getFromAddress()));
mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(mailInfo.getToAddress()));
mailMessage.setSubject(mailInfo.getSubject());
mailMessage.setSentDate(new Date());
Multipart multi = new MimeMultipart();
BodyPart textBodyPart = new MimeBodyPart();
textBodyPart.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
multi.addBodyPart(textBodyPart);
for (String path : mailInfo.getAttachFileNames()) {
DataSource fds = new FileDataSource(path);
BodyPart fileBodyPart = new MimeBodyPart();
fileBodyPart.setDataHandler(new DataHandler(fds));
fileBodyPart.setFileName(path.substring(path.lastIndexOf("/") + 1));
multi.addBodyPart(fileBodyPart);
}
mailMessage.setContent(multi);
mailMessage.saveChanges();
Transport.send(mailMessage);
return true;
} catch (MessagingException ex) {
ex.printStackTrace();
return false;
}
}
示例5: addAttachment
import javax.mail.BodyPart; //导入方法依赖的package包/类
@Override
protected void addAttachment(String name, DataHandler data) throws MessagingException {
BodyPart attachment = new MimeBodyPart();
attachment.setDataHandler(data);
attachment.setFileName(name);
attachment.setHeader("Content-ID", "<" + name + ">");
iBody.addBodyPart(attachment);
}
示例6: getBodyPart
import javax.mail.BodyPart; //导入方法依赖的package包/类
public BodyPart getBodyPart() {
try {
DataSource dataSource = new DataSource() {
@Override public String getContentType() { return contentType; }
@Override public InputStream getInputStream() { return new ByteArrayInputStream(body.toByteArray()); }
@Override public String getName() { return filenameOrNull; }
@Override public OutputStream getOutputStream() { throw new RuntimeException("unreachable"); }
};
BodyPart filePart = new MimeBodyPart();
filePart.setDataHandler(new DataHandler(dataSource));
if (filenameOrNull != null) {
filePart.setFileName(filenameOrNull);
filePart.setDisposition(Part.ATTACHMENT);
}
return filePart;
}
catch (MessagingException e) { throw new RuntimeException(e); }
}
示例7: addAttachment
import javax.mail.BodyPart; //导入方法依赖的package包/类
public EmailMessage addAttachment(String attachmentName, File file)
throws MessagingException {
_totalAttachmentSizeSoFar += file.length();
if (_totalAttachmentSizeSoFar > _totalAttachmentMaxSizeInByte) {
throw new MessageAttachmentExceededMaximumSizeException(
"Adding attachment '" + attachmentName
+ "' will exceed the allowed maximum size of "
+ _totalAttachmentMaxSizeInByte);
}
BodyPart attachmentPart = new MimeBodyPart();
DataSource fileDataSource = new FileDataSource(file);
attachmentPart.setDataHandler(new DataHandler(fileDataSource));
attachmentPart.setFileName(attachmentName);
_attachments.add(attachmentPart);
return this;
}
示例8: prepareMessage
import javax.mail.BodyPart; //导入方法依赖的package包/类
/**
* @param attachmentPaths
* Paths to attachment files.
* @throws Exception
* Exception.
* @return Message with attachments.
*/
private Message prepareMessage(String[] attachmentPaths) throws Exception {
Message message = new MimeMessage(Session.getDefaultInstance(System.getProperties()));
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("Test.");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
for (String file : attachmentPaths) {
BodyPart attachmentPart = new MimeBodyPart();
File attachment = new File(SRC_TEST_RESOURCES_MAILING_TEST + "/" + file);
DataSource source = new FileDataSource(attachment);
attachmentPart.setDataHandler(new DataHandler(source));
attachmentPart.setFileName(attachment.getName());
multipart.addBodyPart(attachmentPart);
}
message.setContent(multipart);
message.removeHeader("Content-Type");
message.setHeader("Content-Type", "multipart/mixed");
Assert.assertTrue(message.isMimeType("multipart/*"));
return message;
}
示例9: addAttachment
import javax.mail.BodyPart; //导入方法依赖的package包/类
private void addAttachment(String filename) throws Exception {
BodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
messageBodyPart.setHeader("Content-ID","<image>");
_multipart.addBodyPart(messageBodyPart);
}
示例10: getBodyPartFromDatasource
import javax.mail.BodyPart; //导入方法依赖的package包/类
/**
* Helper method which generates a {@link BodyPart} from an {@link AttachmentResource} (from its {@link DataSource}) and a disposition type
* ({@link Part#INLINE} or {@link Part#ATTACHMENT}). With this the attachment data can be converted into objects that fit in the email structure.
* <p>
* For every attachment and embedded image a header needs to be set.
*
* @param attachmentResource An object that describes the attachment and contains the actual content data.
* @param dispositionType The type of attachment, {@link Part#INLINE} or {@link Part#ATTACHMENT} .
*
* @return An object with the attachment data read for placement in the email structure.
* @throws MessagingException All BodyPart setters.
*/
private static BodyPart getBodyPartFromDatasource(final AttachmentResource attachmentResource, final String dispositionType)
throws MessagingException {
final BodyPart attachmentPart = new MimeBodyPart();
// setting headers isn't working nicely using the javax mail API, so let's do that manually
final String resourceName = determineResourceName(attachmentResource, false);
final String fileName = determineResourceName(attachmentResource, true);
attachmentPart.setDataHandler(new DataHandler(new NamedDataSource(fileName, attachmentResource.getDataSource())));
attachmentPart.setFileName(fileName);
final String contentType = attachmentResource.getDataSource().getContentType();
attachmentPart.setHeader("Content-Type", contentType + "; filename=" + fileName + "; name=" + resourceName);
attachmentPart.setHeader("Content-ID", format("<%s>", resourceName));
attachmentPart.setDisposition(dispositionType);
return attachmentPart;
}
示例11: appendIcsBody
import javax.mail.BodyPart; //导入方法依赖的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;
}
示例12: setUp
import javax.mail.BodyPart; //导入方法依赖的package包/类
/**
* Sets up the test objects.
*
* @throws Exception
* Any exception
*/
@Before
public void setUp() throws Exception
{
this.parser=new DefaultMailMessageParser();
this.parser.initialize(new HashMap<String,String>());
this.message=new MimeMessage((Session)null);
this.message.setSubject("fax:123456789");
this.message.setFrom(new InternetAddress("[email protected]"));
File file=File.createTempFile("temp_",".txt");
file.deleteOnExit();
this.fileName=file.getName();
IOHelper.writeTextFile("abc",file);
DataSource source=new FileDataSource(file);
BodyPart messageFileAttachmentBodyPart=new MimeBodyPart();
messageFileAttachmentBodyPart.setDataHandler(new DataHandler(source));
messageFileAttachmentBodyPart.setFileName(file.getName());
Multipart multipart=new MimeMultipart();
multipart.addBodyPart(messageFileAttachmentBodyPart);
this.message.setContent(multipart);
}
示例13: addFileAttachments
import javax.mail.BodyPart; //导入方法依赖的package包/类
/**
* Add file attachments to multi part document
*
* @param multipart
* @param attachments
* @throws MessagingException
*/
private static void addFileAttachments(MimeMultipart multipart, List<MailAttachmentPO> attachments)
throws MessagingException
{
for (MailAttachmentPO attachment : attachments)
{
if (null == attachment.getCid() || attachment.getCid().isEmpty())
{
DataSource byteDataSource = new ByteArrayDataSource(attachment.getContent(), attachment.getMimeType());
DataHandler attachementDataHandler = new DataHandler(byteDataSource);
BodyPart memoryBodyPart = new MimeBodyPart();
memoryBodyPart.setDataHandler(attachementDataHandler);
memoryBodyPart.setFileName(attachment.getName());
// Add part to multi-part
multipart.addBodyPart(memoryBodyPart);
}
}
}
示例14: testAttachments
import javax.mail.BodyPart; //导入方法依赖的package包/类
@Test
public void testAttachments() throws Exception{
Map<String, Object> settings = settings("/river-imap-attachments.json");
final Properties props = new Properties();
final String user = XContentMapValues.nodeStringValue(settings.get("user"), null);
final String password = XContentMapValues.nodeStringValue(settings.get("password"), null);
for (final Map.Entry<String, Object> entry : settings.entrySet()) {
if (entry != null && entry.getKey().startsWith("mail.")) {
props.setProperty(entry.getKey(), String.valueOf(entry.getValue()));
}
}
registerRiver("imap_river", "river-imap-attachments.json");
final Session session = Session.getInstance(props);
final Store store = session.getStore();
store.connect(user, password);
checkStoreForTestConnection(store);
final Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_WRITE);
final MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(EMAIL_TO));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(EMAIL_USER_ADDRESS));
message.setSubject(EMAIL_SUBJECT + "::attachment test");
message.setSentDate(new Date());
BodyPart bp = new MimeBodyPart();
bp.setText("Text");
Multipart mp = new MimeMultipart();
mp.addBodyPart(bp);
bp = new MimeBodyPart();
DataSource ds = new ByteArrayDataSource(this.getClass().getResourceAsStream("/httpclient-tutorial.pdf"), AttachmentMapperTest.APPLICATION_PDF);
bp.setDataHandler(new DataHandler(ds));
bp.setFileName("httpclient-tutorial.pdf");
mp.addBodyPart(bp);
message.setContent(mp);
inbox.appendMessages(new Message[]{message});
IMAPUtils.close(inbox);
IMAPUtils.close(store);
//let the river index
Thread.sleep(20*1000);
esSetup.client().admin().indices().refresh(new RefreshRequest()).actionGet();
SearchResponse searchResponse = esSetup.client().prepareSearch("imapriverdata").setTypes("mail").execute().actionGet();
Assert.assertEquals(1, searchResponse.getHits().totalHits());
//BASE64 content httpclient-tutorial.pdf
Assert.assertTrue(searchResponse.getHits().hits()[0].getSourceAsString().contains(AttachmentMapperTest.PDF_BASE64_DETECTION));
searchResponse = esSetup.client().prepareSearch("imapriverdata").addFields("*").setTypes("mail").setQuery(QueryBuilders.matchPhraseQuery("attachments.content.content", PDF_CONTENT_TO_SEARCH)).execute().actionGet();
Assert.assertEquals(1, searchResponse.getHits().totalHits());
Assert.assertEquals(1, searchResponse.getHits().hits()[0].field("attachments.content.content").getValues().size());
Assert.assertEquals("HttpClient Tutorial", searchResponse.getHits().hits()[0].field("attachments.content.title").getValue().toString());
Assert.assertEquals("application/pdf", searchResponse.getHits().hits()[0].field("attachments.content.content_type").getValue().toString());
Assert.assertTrue(searchResponse.getHits().hits()[0].field("attachments.content.content").getValue().toString().contains(PDF_CONTENT_TO_SEARCH));
}
示例15: addAttachment
import javax.mail.BodyPart; //导入方法依赖的package包/类
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.substring(filename.lastIndexOf("/") + 1));
multipart.addBodyPart(messageBodyPart);
BodyPart messageBodyPart2 = new MimeBodyPart();
messageBodyPart2.setText(subject);
multipart.addBodyPart(messageBodyPart2);
}