本文整理匯總了Java中javax.mail.Part.getContentType方法的典型用法代碼示例。如果您正苦於以下問題:Java Part.getContentType方法的具體用法?Java Part.getContentType怎麽用?Java Part.getContentType使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.mail.Part
的用法示例。
在下文中一共展示了Part.getContentType方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: isContainAttach
import javax.mail.Part; //導入方法依賴的package包/類
/**
* 判斷此郵件是否包含附件
*/
public boolean isContainAttach(Part part) throws Exception {
boolean attachflag = false;
String contentType = part.getContentType();
if (part.isMimeType("multipart/*")) {
Multipart mp = (Multipart) part.getContent();
for (int i = 0; i < mp.getCount(); i++) {
BodyPart mpart = mp.getBodyPart(i);
String disposition = mpart.getDisposition();
if ((disposition != null)
&& ((disposition.equals(Part.ATTACHMENT)) || (disposition
.equals(Part.INLINE))))
attachflag = true;
else if (mpart.isMimeType("multipart/*")) {
attachflag = isContainAttach((Part) mpart);
} else {
String contype = mpart.getContentType();
if (contype.toLowerCase().indexOf("application") != -1)
attachflag = true;
if (contype.toLowerCase().indexOf("name") != -1)
attachflag = true;
}
}
} else if (part.isMimeType("message/rfc822")) {
attachflag = isContainAttach((Part) part.getContent());
}
return attachflag;
}
示例2: getContent
import javax.mail.Part; //導入方法依賴的package包/類
public static String getContent(Part message) throws MessagingException,
IOException {
if (message.getContent() instanceof String) {
return message.getContentType() + ": " + message.getContent() + " \n\t";
} else if (message.getContent() != null && message.getContent() instanceof Multipart) {
Multipart part = (Multipart) message.getContent();
String text = "";
for (int i = 0; i < part.getCount(); i++) {
BodyPart bodyPart = part.getBodyPart(i);
if (!Message.ATTACHMENT.equals(bodyPart.getDisposition())) {
text += getContent(bodyPart);
} else {
text += "attachment: \n" +
"\t\t name: " + (StringUtils.isEmpty(bodyPart.getFileName()) ? "none"
: bodyPart.getFileName()) + "\n" +
"\t\t disposition: " + bodyPart.getDisposition() + "\n" +
"\t\t description: " + (StringUtils.isEmpty(bodyPart.getDescription()) ? "none"
: bodyPart.getDescription()) + "\n\t";
}
}
return text;
}
if (message.getContent() != null && message.getContent() instanceof Part) {
if (!Message.ATTACHMENT.equals(message.getDisposition())) {
return getContent((Part) message.getContent());
} else {
return "attachment: \n" +
"\t\t name: " + (StringUtils.isEmpty(message.getFileName()) ? "none"
: message.getFileName()) + "\n" +
"\t\t disposition: " + message.getDisposition() + "\n" +
"\t\t description: " + (StringUtils.isEmpty(message.getDescription()) ? "none"
: message.getDescription()) + "\n\t";
}
}
return "";
}
示例3: SubethaEmailMessagePart
import javax.mail.Part; //導入方法依賴的package包/類
/**
* Object can be built on existing message part only.
*
* @param messagePart Message part.
*/
public SubethaEmailMessagePart(Part messagePart)
{
ParameterCheck.mandatory("messagePart", messagePart);
try
{
fileSize = messagePart.getSize();
fileName = messagePart.getFileName();
contentType = messagePart.getContentType();
Matcher matcher = encodingExtractor.matcher(contentType);
if (matcher.find())
{
encoding = matcher.group(1);
if (!Charset.isSupported(encoding))
{
throw new EmailMessageException(ERR_UNSUPPORTED_ENCODING, encoding);
}
}
try
{
contentInputStream = messagePart.getInputStream();
}
catch (Exception ex)
{
throw new EmailMessageException(ERR_FAILED_TO_READ_CONTENT_STREAM, ex.getMessage());
}
}
catch (MessagingException e)
{
throw new EmailMessageException(ERR_INCORRECT_MESSAGE_PART, e.getMessage());
}
}
示例4: setMailContent
import javax.mail.Part; //導入方法依賴的package包/類
/**
* 解析郵件,把得到的郵件內容保存到一個StringBuffer對象中,解析郵件 主要是根據MimeType類型的不同執行不同的操作,一步一步的解析
*/
public void setMailContent(Part part) throws Exception {
String contenttype = part.getContentType();
int nameindex = contenttype.indexOf("name");
boolean conname = false;
if (nameindex != -1)
conname = true;
// logger.info("CONTENTTYPE: " + contenttype);
if (part.isMimeType("text/plain") && !conname) {
bodytext.append((String) part.getContent());
} else if (part.isMimeType("text/html") && !conname) {
bodytext.append((String) part.getContent());
} else if (part.isMimeType("multipart/*")) {
Multipart multipart = (Multipart) part.getContent();
int counts = multipart.getCount();
for (int i = 0; i < counts; i++) {
setMailContent(multipart.getBodyPart(i));
}
} else if (part.isMimeType("message/rfc822")) {
setMailContent((Part) part.getContent());
} else {}
}
示例5: handleMimeMessages
import javax.mail.Part; //導入方法依賴的package包/類
protected String handleMimeMessages(Message message,Map<String,byte[]> attachmentMap)
throws Exception{
String emailBody = null;
Multipart multipart = (Multipart) message.getContent();
for (int j = 0, m = multipart.getCount(); j < m; j++) {
Part part = multipart.getBodyPart(j);
String disposition = part.getDisposition();
boolean isAttachment = false;
if (disposition != null && (disposition.equals(Part.ATTACHMENT)
|| (disposition.equals(Part.INLINE)))) {
isAttachment = true;
InputStream input = part.getInputStream();
String fileName = part.getFileName();
String contentType = part.getContentType();
ByteArrayOutputStream output = new ByteArrayOutputStream();
if (saveAttachment(input, output)) {
byte[] encodedByte = new Base64().encode(output.toByteArray());
if (encodedByte != null && fileName != null && contentType != null) {
attachmentMap.put(fileName+"~"+contentType, encodedByte);
}
}
}
if (!isAttachment && part.getContentType().startsWith("text/")){
emailBody = (String) part.getContent();
}
}
return emailBody;
}
示例6: getAttachments
import javax.mail.Part; //導入方法依賴的package包/類
/**
* Extracts the attachments from the mail.
*
* @param message
* The message.
* @return Collection of attachments as {@link AttachmentTO}.
* @throws IOException
* Exception.
* @throws MessagingException
* Exception.
*/
public static Collection<AttachmentTO> getAttachments(Message message)
throws MessagingException, IOException {
Collection<AttachmentTO> attachments = new ArrayList<AttachmentTO>();
Collection<Part> parts = getAllParts(message);
for (Part part : parts) {
String disposition = part.getDisposition();
String contentType = part.getContentType();
if (StringUtils.containsIgnoreCase(disposition, "inline")
|| StringUtils.containsIgnoreCase(disposition, "attachment")
|| StringUtils.containsIgnoreCase(contentType, "name=")) {
String fileName = part.getFileName();
Matcher matcher = FILENAME_PATTERN.matcher(part.getContentType());
if (matcher.matches()) {
fileName = matcher.group(1);
fileName = StringUtils.substringBeforeLast(fileName, ";");
}
if (StringUtils.isNotBlank(fileName)) {
fileName = fileName.replace("\"", "").replace("\\\"", "");
fileName = MimeUtility.decodeText(fileName);
if (fileName.endsWith("?=")) {
fileName = fileName.substring(0, fileName.length() - 2);
}
fileName = fileName.replace("?", "_");
AttachmentTO attachmentTO = new AttachmentStreamTO(part.getInputStream());
attachmentTO.setContentLength(part.getSize());
attachmentTO.setMetadata(new ContentMetadata());
attachmentTO.getMetadata().setFilename(fileName);
if (StringUtils.isNotBlank(contentType)) {
contentType = contentType.split(";")[0].toLowerCase();
}
attachmentTO.setStatus(AttachmentStatus.UPLOADED);
attachments.add(attachmentTO);
}
}
}
return attachments;
}
示例7: getMailPartAsText
import javax.mail.Part; //導入方法依賴的package包/類
public static String getMailPartAsText(final Part part) throws MessagingException, IOException {
String partString = "";
if (part.isMimeType("text/*")) {
partString = (String) part.getContent();
} else if (part.isMimeType("image/*")) {
final String contentType = part.getContentType();
final String disposition = part.getDisposition();
final String fileName = part.getFileName();
final StringBuilder imageTextString = new StringBuilder();
imageTextString.append("[Image]");
imageTextString.append("[Inline]");
imageTextString.append(contentType);
imageTextString.append(fileName);
partString = imageTextString.toString();
final StringBuilder imageInfoLogString = new StringBuilder();
imageInfoLogString.append("Image in body.. with name:: ");
imageInfoLogString.append(fileName);
imageInfoLogString.append(" disposition:: ");
imageInfoLogString.append(disposition);
imageInfoLogString.append("HTML file tag:: ");
imageInfoLogString.append(partString);
LOGGER.info(imageInfoLogString.toString());
} else if (part.isMimeType("multipart/*")) {
final Multipart multiPart = (Multipart) part.getContent();
for (int i = 0; i < multiPart.getCount(); i++) {
partString = getMailPartAsText(multiPart.getBodyPart(i));
}
}
return partString;
}
示例8: handlePart
import javax.mail.Part; //導入方法依賴的package包/類
public static void handlePart(Part part) throws MessagingException, IOException {
String disposition = part.getDisposition();
String contentType = part.getContentType();
if (disposition == null) { // When just body
System.out.println("Null: " + contentType);
// Check if plain
if (contentType.length() >= 10 && contentType.toLowerCase().substring(0, 10).equals("text/plain")) {
part.writeTo(System.out);
} else { // Don't think this will happen
System.out.println("Other body: " + contentType);
part.writeTo(System.out);
}
} else if (disposition.equalsIgnoreCase(Part.ATTACHMENT)) {
System.out.println("Attachment: " + part.getFileName() + " : " + contentType);
MailClient.saveFile(part.getFileName(), part.getInputStream());
} else if (disposition.equalsIgnoreCase(Part.INLINE)) {
System.out.println("Inline: " + part.getFileName() + " : " + contentType);
MailClient.saveFile(part.getFileName(), part.getInputStream());
} else { // Should never happen
System.out.println("Other: " + disposition);
}
}
示例9: getMimeType
import javax.mail.Part; //導入方法依賴的package包/類
public String getMimeType(Part part) throws Exception {
String mimeType = part.getContentType();
int semicolonIndex = mimeType.indexOf(';');
if (semicolonIndex!=-1) {
mimeType = mimeType
.substring(0, semicolonIndex);
}
mimeType = mimeType.trim();
mimeType = mimeType.toLowerCase();
return mimeType;
}
示例10: checkPartForTextType
import javax.mail.Part; //導入方法依賴的package包/類
/**
* Recursive find body parts and save them to HashMap
*/
private static void checkPartForTextType(HashMap<String, Collection<String>> bodies, Part part) throws IOException, MessagingException {
Object content = part.getContent();
if (content instanceof CharSequence) {
String ct = part.getContentType();
Collection<String> value = bodies.get(ct);
if (value != null) {
value.add(content.toString());
} else {
value = new LinkedList<>();
value.add(content.toString());
bodies.put(ct, value);
}
} else if (content instanceof Multipart) {
Multipart mp = (Multipart) content;
for(int i = 0; i < mp.getCount(); i++) {
checkPartForTextType(bodies, mp.getBodyPart(i));
}
}
}
示例11: getRelatedPart
import javax.mail.Part; //導入方法依賴的package包/類
/**
* 獲取消息附件中的文件.
* @param part
* Part 對象
* @param resourceList
* 文件的集合(每個 ResourceFileBean 對象中存放文件名和 InputStream 對象)
* @throws MessagingException
* the messaging exception
* @throws IOException
* Signals that an I/O exception has occurred.
*/
private void getRelatedPart(Part part, List<ResourceFileBean> resourceList) throws MessagingException, IOException {
//驗證 Part 對象的 MIME 類型是否與指定的類型匹配。
if (part.isMimeType("multipart/related")) {
Multipart mulContent = (Multipart) part.getContent();
for (int j = 0, m = mulContent.getCount(); j < m; j++) {
Part contentPart = mulContent.getBodyPart(j);
if (!contentPart.isMimeType("text/*")) {
String fileName = "Resource";
//此方法返回 Part 對象的內容類型。
String type = contentPart.getContentType();
if (type != null) {
type = type.substring(0, type.indexOf("/"));
}
fileName = fileName + "[" + type + "]";
if (contentPart.getHeader("Content-Location") != null
&& contentPart.getHeader("Content-Location").length > 0) {
fileName = contentPart.getHeader("Content-Location")[0];
}
InputStream is = contentPart.getInputStream();
resourceList.add(new ResourceFileBean(fileName, is));
}
}
} else {
Multipart mp = null;
Object content = part.getContent();
if (content instanceof Multipart) {
mp = (Multipart) content;
} else {
return;
}
for (int i = 0, n = mp.getCount(); i < n; i++) {
Part body = mp.getBodyPart(i);
getRelatedPart(body, resourceList);
}
}
}
示例12: getMailPartContentEncoding
import javax.mail.Part; //導入方法依賴的package包/類
private String getMailPartContentEncoding(Part part) throws MessagingException
{
String encoding = DEFAULT_ENCODING;
String contentType = part.getContentType();
int startIndex = contentType.indexOf(CHARSET);
if (startIndex > 0)
{
encoding = contentType.substring(startIndex + CHARSET.length() + 1).replaceAll("\"", "");
}
return encoding;
}
示例13: getMessageByContentTypes
import javax.mail.Part; //導入方法依賴的package包/類
protected Map<String, String> getMessageByContentTypes(Message message, String characterSet) throws Exception {
Map<String, String> messageMap = new HashMap<>();
if (message.isMimeType(TEXT_PLAIN)) {
messageMap.put(TEXT_PLAIN, MimeUtility.decodeText(message.getContent().toString()));
} else if (message.isMimeType(TEXT_HTML)) {
messageMap.put(TEXT_HTML, MimeUtility.decodeText(convertMessage(message.getContent().toString())));
} else if (message.isMimeType(MULTIPART_MIXED) || message.isMimeType(MULTIPART_RELATED)) {
messageMap.put(MULTIPART_MIXED, extractMultipartMixedMessage(message, characterSet));
} else {
Object obj = message.getContent();
Multipart mpart = (Multipart) obj;
for (int i = 0, n = mpart.getCount(); i < n; i++) {
Part part = mpart.getBodyPart(i);
if (decryptMessage && part.getContentType() != null &&
part.getContentType().equals(ENCRYPTED_CONTENT_TYPE)) {
part = decryptPart((MimeBodyPart)part);
}
String disposition = part.getDisposition();
String partContentType = part.getContentType().substring(0, part.getContentType().indexOf(";"));
if (disposition == null) {
if (part.getContent() instanceof MimeMultipart) {
// multipart with attachment
MimeMultipart mm = (MimeMultipart) part.getContent();
for (int j = 0; j < mm.getCount(); j++) {
if (mm.getBodyPart(j).getContent() instanceof String) {
BodyPart bodyPart = mm.getBodyPart(j);
if ((characterSet != null) && (characterSet.trim().length() > 0)) {
String contentType = bodyPart.getHeader(CONTENT_TYPE)[0];
contentType = contentType
.replace(contentType.substring(contentType.indexOf("=") + 1), characterSet);
bodyPart.setHeader(CONTENT_TYPE, contentType);
}
String partContentType1 = bodyPart
.getContentType().substring(0, bodyPart.getContentType().indexOf(";"));
messageMap.put(partContentType1,
MimeUtility.decodeText(bodyPart.getContent().toString()));
}
}
} else {
//multipart - w/o attachment
//if the user has specified a certain characterSet we decode his way
if ((characterSet != null) && (characterSet.trim().length() > 0)) {
InputStream istream = part.getInputStream();
ByteArrayInputStream bis = new ByteArrayInputStream(ASCIIUtility.getBytes(istream));
int count = bis.available();
byte[] bytes = new byte[count];
count = bis.read(bytes, 0, count);
messageMap.put(partContentType,
MimeUtility.decodeText(new String(bytes, 0, count, characterSet)));
} else {
messageMap.put(partContentType, MimeUtility.decodeText(part.getContent().toString()));
}
}
}
} //for
} //else
return messageMap;
}
示例14: getAttachedFileNames
import javax.mail.Part; //導入方法依賴的package包/類
protected String getAttachedFileNames(Part part) throws Exception {
String fileNames = "";
Object content = part.getContent();
if (!(content instanceof Multipart)) {
if (decryptMessage && part.getContentType() != null &&
part.getContentType().equals(ENCRYPTED_CONTENT_TYPE)) {
part = decryptPart((MimeBodyPart) part);
}
// non-Multipart MIME part ...
// is the file name set for this MIME part? (i.e. is it an attachment?)
if (part.getFileName() != null && !part.getFileName().equals("") && part.getInputStream() != null) {
String fileName = part.getFileName();
// is the file name encoded? (consider it is if it's in the =?charset?encoding?encoded text?= format)
if (fileName.indexOf('?') == -1) {
// not encoded (i.e. a simple file name not containing '?')-> just return the file name
return fileName;
}
// encoded file name -> remove any chars before the first "=?" and after the last "?="
return fileName.substring(fileName.indexOf("=?"), fileName.length() -
((new StringBuilder(fileName)).reverse()).indexOf("=?"));
}
} else {
// a Multipart type of MIME part
Multipart mpart = (Multipart) content;
// iterate through all the parts in this Multipart ...
for (int i = 0, n = mpart.getCount(); i < n; i++) {
if (!"".equals(fileNames)) {
fileNames += STR_COMMA;
}
// to the list of attachments built so far append the list of attachments in the current MIME part ...
fileNames += getAttachedFileNames(mpart.getBodyPart(i));
}
}
return fileNames;
}
示例15: processMultipart
import javax.mail.Part; //導入方法依賴的package包/類
public void processMultipart(Writer writer, Multipart mp, String name) throws MessagingException {
for (int j = 0; j < mp.getCount(); j++) {
try {
Part part = mp.getBodyPart(j);
Logger.debug("Disposition=" + part.getDisposition());
String contentType = part.getContentType();
Logger.debug("content-type=" + contentType);
Logger.debug("Object=" + part);
if (contentType.startsWith("text/plain")) {
writer.write((String) part.getContent());
} else if (contentType.startsWith("image/")) {
processImage(writer, part, name);
} else if (contentType.startsWith("multipart/")) {
processMultipart(writer, (Multipart) part.getContent(), name);
}
} catch (Exception e) {
Logger.warn("PostDaemon: Error reading message", e);
}
}
}