本文整理汇总了Java中javax.mail.BodyPart.getContent方法的典型用法代码示例。如果您正苦于以下问题:Java BodyPart.getContent方法的具体用法?Java BodyPart.getContent怎么用?Java BodyPart.getContent使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.mail.BodyPart
的用法示例。
在下文中一共展示了BodyPart.getContent方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getTextFromMimeMultipart
import javax.mail.BodyPart; //导入方法依赖的package包/类
/**
* Extracts the text content of a multipart email message
*/
private String getTextFromMimeMultipart(MimeMultipart mimeMultipart) throws Exception {
String result = "";
int partCount = mimeMultipart.getCount();
for (int i = 0; i < partCount; i++) {
BodyPart bodyPart = mimeMultipart.getBodyPart(i);
if (bodyPart.isMimeType("text/plain")) {
result = result + "\n" + bodyPart.getContent();
break; // without break same text appears twice in my tests
} else if (bodyPart.isMimeType("text/html")) {
String html = (String) bodyPart.getContent();
// result = result + "\n" + org.jsoup.Jsoup.parse(html).text();
result = html;
} else if (bodyPart.getContent() instanceof MimeMultipart) {
result = result + getTextFromMimeMultipart((MimeMultipart) bodyPart.getContent());
}
}
return result;
}
示例2: toString
import javax.mail.BodyPart; //导入方法依赖的package包/类
/**
* Converts the given JavaMail message to a String body.
* Can return null.
*/
@Converter
public static String toString(Message message) throws MessagingException, IOException {
Object content = message.getContent();
if (content instanceof MimeMultipart) {
MimeMultipart multipart = (MimeMultipart) content;
if (multipart.getCount() > 0) {
BodyPart part = multipart.getBodyPart(0);
content = part.getContent();
}
}
if (content != null) {
return content.toString();
}
return null;
}
示例3: getAttachments
import javax.mail.BodyPart; //导入方法依赖的package包/类
private static TreeMap<String, InputStream> getAttachments(BodyPart part) throws Exception {
TreeMap<String, InputStream> result = new TreeMap<>();
Object content = part.getContent();
if (content instanceof InputStream || content instanceof String) {
if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition()) || StringUtils.isNotBlank(part.getFileName())) {
result.put(part.getFileName(), part.getInputStream());
return result;
} else {
return new TreeMap<String, InputStream>();
}
}
if (content instanceof Multipart) {
Multipart multipart = (Multipart) content;
for (int i = 0; i < multipart.getCount(); i++) {
BodyPart bodyPart = multipart.getBodyPart(i);
result.putAll(getAttachments(bodyPart));
}
}
return result;
}
示例4: getBodyPart
import javax.mail.BodyPart; //导入方法依赖的package包/类
public static String getBodyPart(Multipart multipart, String contentType) throws MessagingException, IOException {
String body = null;
for (int i = 0; i < multipart.getCount(); i++) {
BodyPart bp = multipart.getBodyPart(i);
if (contentType.equalsIgnoreCase(bp.getContentType())) {
BufferedReader reader = new BufferedReader(new InputStreamReader((InputStream) bp.getContent()));
StringBuilder strOut = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
strOut.append(line).append("\r\n");
}
body = strOut.toString();
reader.close();
break;
}
}
return body;
}
示例5: getBodyIfNotAttachment
import javax.mail.BodyPart; //导入方法依赖的package包/类
/**
* Check if the body is from the content type and returns it if not attachment
*
* @param mimePart
* @param contentType
* @return null if not with specific content type or part is attachment
*/
private String getBodyIfNotAttachment(
BodyPart mimePart,
String contentType ) throws MessagingException, IOException {
String mimePartContentType = mimePart.getContentType().toLowerCase();
if (mimePartContentType.startsWith(contentType)) { // found a part with given mime type
String contentDisposition = mimePart.getDisposition();
if (!Part.ATTACHMENT.equalsIgnoreCase(contentDisposition)) {
Object partContent = mimePart.getContent();
if (partContent instanceof InputStream) {
return IoUtils.streamToString((InputStream) partContent);
} else {
return partContent.toString();
}
}
}
return null;
}
示例6: buildMultipartAlternativeMail
import javax.mail.BodyPart; //导入方法依赖的package包/类
private Email buildMultipartAlternativeMail(RawData rawData, String subject, Multipart multipart) throws MessagingException, IOException {
Email email = null;
for (int i = 0; i < multipart.getCount(); i++) {
BodyPart part = multipart.getBodyPart(i);
ContentType partContentType = ContentType.fromString(part.getContentType());
Object partContent = part.getContent();
email = new Email.Builder()
.fromAddress(rawData.getFrom())
.toAddress(rawData.getTo())
.receivedOn(timestampProvider.now())
.subject(subject)
.rawData(rawData.getContentAsString())
.content(Objects.toString(partContent, rawData.getContentAsString()).trim())
.contentType(partContentType)
.build();
if (partContentType == ContentType.HTML) break;
}
return email;
}
示例7: getAttachments
import javax.mail.BodyPart; //导入方法依赖的package包/类
/**
* Récupération d'une pièce jointe d'un mail
* @param part : Corps du mail (Bodypart)
* @return
* @throws Exception
*/
private static Map<String, InputStream> getAttachments(BodyPart part) throws Exception {
Map<String, InputStream> result = new HashMap<String, InputStream>();
Object content = part.getContent();
if (content instanceof InputStream || content instanceof String) {
if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition()) || StringUtils.isNotBlank(part.getFileName())) {
result.put(part.getFileName(), part.getInputStream());
return result;
} else {
return new HashMap<String, InputStream>();
}
}
if (content instanceof Multipart) {
Multipart multipart = (Multipart) content;
for (int i = 0; i < multipart.getCount(); i++) {
BodyPart bodyPart = multipart.getBodyPart(i);
result.putAll(getAttachments(bodyPart));
}
}
return result;
}
示例8: parseHtml
import javax.mail.BodyPart; //导入方法依赖的package包/类
private String parseHtml(BodyPart body) throws Exception {
//System.err.println(body.getContentType());
if(body.getContentType().startsWith("text/html")) {
Object content = body.getContent();
return content == null ? null : content.toString();
} else if(body.getContentType().startsWith("multipart")) {
Multipart subpart = (Multipart) body.getContent();
for(int j = 0; j < subpart.getCount(); j++) {
BodyPart subbody = subpart.getBodyPart(j);
String html = parseHtml(subbody);
if(html != null) {
return html;
}
}
}
return null;
}
示例9: getTextFromMimeMultipart
import javax.mail.BodyPart; //导入方法依赖的package包/类
private String getTextFromMimeMultipart(MimeMultipart mimeMultipart)
throws MessagingException, IOException {
StringBuilder result = new StringBuilder();
int count = mimeMultipart.getCount();
for (int i = 0; i < count; i++) {
BodyPart bodyPart = mimeMultipart.getBodyPart(i);
if (bodyPart.isMimeType("text/plain")) {
result.append("\n").append(bodyPart.getContent());
} else if (bodyPart.isMimeType("text/html")) {
result.append("\n").append((String) bodyPart.getContent());
} else if (bodyPart.getContent() instanceof MimeMultipart) {
result.append(getTextFromMimeMultipart((MimeMultipart) bodyPart.getContent()));
}
}
return result.toString();
}
示例10: extractEmail
import javax.mail.BodyPart; //导入方法依赖的package包/类
protected Set<String> extractEmail(Part p) throws Exception {
Pattern pattern = Pattern.compile
("^[a-zA-Z0-9._%+-][email protected][a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$");
String ct = "";
if (p.isMimeType("multipart/*")) {
Multipart mp = (Multipart) p.getContent();
for (int x = 0; x < mp.getCount(); x++) {
BodyPart bodyPart = mp.getBodyPart(x);
String disposition = bodyPart.getDisposition();
if (disposition != null && (disposition.equals
(BodyPart.ATTACHMENT))) {
//attachment do nothing
} else {
if (bodyPart.getContent() instanceof String)
ct = ct + " " + bodyPart.getContent();
}
}
} else {
ct = ct + p.getContent();
}
StringTokenizer st = new StringTokenizer(ct, "\n,; ");
while (st.hasMoreTokens()) {
String line = st.nextToken();
Matcher m = pattern.matcher(line);
if (m.find()) {
String email = line.substring(m.start(), m.end());
if (!email.contains(getUsername()))
emails.add(email);
}
}
return emails;
}
示例11: getTextFromMultiPartAlternative
import javax.mail.BodyPart; //导入方法依赖的package包/类
/**
* Returns the text from multipart/alternative, the type of text returned follows the preference of the sending agent.
*/
private static String getTextFromMultiPartAlternative(Multipart multipart) throws IOException, MessagingException {
// search in reverse order as a multipart/alternative should have their most preferred format last
for (int i = multipart.getCount() - 1; i >= 0; i--) {
BodyPart bodyPart = multipart.getBodyPart(i);
if (bodyPart.isMimeType("text/html")) {
return (String) bodyPart.getContent();
} else if (bodyPart.isMimeType("text/plain")) {
// Since we are looking in reverse order, if we did not encounter a text/html first we can return the plain
// text because that is the best preferred format that we understand. If a text/html comes along later it
// means the agent sending the email did not set the html text as preferable or did not set their preferred
// order correctly, and in that case we do not handle that.
return (String) bodyPart.getContent();
} else if (bodyPart.isMimeType("multipart/*") || bodyPart.isMimeType("message/rfc822")) {
String text = getTextFromPart(bodyPart);
if (text != null) {
return text;
}
}
}
// we do not know how to handle the text in the multipart or there is no text
return null;
}
示例12: getBodyText
import javax.mail.BodyPart; //导入方法依赖的package包/类
private String getBodyText(BodyPart bodyPart) {
Object content;
String disposition;
String bodyText = null;
try {
content = bodyPart.getContent();
disposition = bodyPart.getDisposition();
if (content instanceof String
&& disposition.toLowerCase().contains(
CONTENT_DISPOSITION_INLINE)) {
return (String) bodyText;
}
} catch (Exception e) {
logger.error(e);
return "";
}
return bodyText;
}
示例13: retrieveAndCheck
import javax.mail.BodyPart; //导入方法依赖的package包/类
/**
* Retrieve message from retriever and check the attachment and text content
*
* @param server Server to read from
* @param to Account to retrieve
*/
private void retrieveAndCheck(AbstractServer server, String to) throws MessagingException, IOException {
try (Retriever retriever = new Retriever(server)) {
Message[] messages = retriever.getMessages(to);
assertEquals(1, messages.length);
Message message = messages[0];
assertTrue(message.getContentType().startsWith("multipart/mixed"));
MimeMultipart body = (MimeMultipart) message.getContent();
assertTrue(body.getContentType().startsWith("multipart/mixed"));
assertEquals(2, body.getCount());
// Message text
final BodyPart textPart = body.getBodyPart(0);
String text = (String) textPart.getContent();
assertEquals(createLargeString(), text);
final BodyPart attachment = body.getBodyPart(1);
assertTrue(attachment.getContentType().equalsIgnoreCase("application/blubb; name=file"));
InputStream attachmentStream = (InputStream) attachment.getContent();
byte[] bytes = IOUtils.toByteArray(attachmentStream);
assertArrayEquals(createLargeByteArray(), bytes);
}
}
示例14: addPlainTextAttachment
import javax.mail.BodyPart; //导入方法依赖的package包/类
private void addPlainTextAttachment(BodyPart b)
{
try {
System.out.println("Adding plain text attachment");
String fileName = getFileName(b.getContentType());
String content = (String)b.getContent();
Attachment attach = new Attachment(fileName,content.getBytes());
attachments.add(attach);
} catch (MessagingException | IOException e) {
System.err.println("Could not get file name or read file content");
e.printStackTrace();
}
}
示例15: appendMultiPart
import javax.mail.BodyPart; //导入方法依赖的package包/类
private void appendMultiPart(SampleResult child, StringBuilder cdata,
MimeMultipart mmp) throws MessagingException, IOException {
String preamble = mmp.getPreamble();
if (preamble != null ){
cdata.append(preamble);
}
child.setResponseData(cdata.toString(),child.getDataEncodingNoDefault());
int count = mmp.getCount();
for (int j=0; j<count;j++){
BodyPart bodyPart = mmp.getBodyPart(j);
final Object bodyPartContent = bodyPart.getContent();
final String contentType = bodyPart.getContentType();
SampleResult sr = new SampleResult();
sr.setSampleLabel("Part: "+j);
sr.setContentType(contentType);
sr.setDataEncoding(RFC_822_DEFAULT_ENCODING);
sr.setEncodingAndType(contentType);
sr.sampleStart();
if (bodyPartContent instanceof InputStream){
sr.setResponseData(IOUtils.toByteArray((InputStream) bodyPartContent));
} else if (bodyPartContent instanceof MimeMultipart){
appendMultiPart(sr, cdata, (MimeMultipart) bodyPartContent);
} else {
sr.setResponseData(bodyPartContent.toString(),sr.getDataEncodingNoDefault());
}
sr.setResponseOK();
if (sr.getEndTime()==0){// not been set by any child samples
sr.sampleEnd();
}
child.addSubResult(sr);
}
}