本文整理匯總了Java中javax.mail.Multipart類的典型用法代碼示例。如果您正苦於以下問題:Java Multipart類的具體用法?Java Multipart怎麽用?Java Multipart使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
Multipart類屬於javax.mail包,在下文中一共展示了Multipart類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getMessagePart
import javax.mail.Multipart; //導入依賴的package包/類
private static Multipart getMessagePart() throws MessagingException, IOException {
Multipart multipart = new MimeMultipart();
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(getVal("msg.Body"));
multipart.addBodyPart(messageBodyPart);
if (getBoolVal("attach.reports")) {
LOG.info("Attaching Reports as zip");
multipart.addBodyPart(getReportsBodyPart());
} else {
if (getBoolVal("attach.standaloneHtml")) {
multipart.addBodyPart(getStandaloneHtmlBodyPart());
}
if (getBoolVal("attach.console")) {
multipart.addBodyPart(getConsoleBodyPart());
}
if (getBoolVal("attach.screenshots")) {
multipart.addBodyPart(getScreenShotsBodyPart());
}
}
messageBodyPart.setContent(getVal("msg.Body")
.concat("\n\n\n")
.concat(MailComponent.getHTMLBody()), "text/html");
return multipart;
}
示例2: getMessageContent
import javax.mail.Multipart; //導入依賴的package包/類
/**
* Get the content of a mail message.
*
* @param message
* the mail message
* @return the content of the mail message
*/
private String getMessageContent(Message message) throws MessagingException {
try {
Object content = message.getContent();
if (content instanceof Multipart) {
StringBuffer messageContent = new StringBuffer();
Multipart multipart = (Multipart) content;
for (int i = 0; i < multipart.getCount(); i++) {
Part part = multipart.getBodyPart(i);
if (part.isMimeType("text/plain")) {
messageContent.append(part.getContent().toString());
}
}
return messageContent.toString();
}
return content.toString();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
示例3: testSendMultipartEmail
import javax.mail.Multipart; //導入依賴的package包/類
@Test
public void testSendMultipartEmail() throws Exception {
mailService.sendEmail("[email protected]", "testSubject", "testContent", true, false);
verify(javaMailSender).send((MimeMessage) messageCaptor.capture());
MimeMessage message = (MimeMessage) messageCaptor.getValue();
MimeMultipart mp = (MimeMultipart) message.getContent();
MimeBodyPart part = (MimeBodyPart) ((MimeMultipart) mp.getBodyPart(0).getContent()).getBodyPart(0);
ByteArrayOutputStream aos = new ByteArrayOutputStream();
part.writeTo(aos);
assertThat(message.getSubject()).isEqualTo("testSubject");
assertThat(message.getAllRecipients()[0].toString()).isEqualTo("[email protected]");
assertThat(message.getFrom()[0].toString()).isEqualTo("[email protected]");
assertThat(message.getContent()).isInstanceOf(Multipart.class);
assertThat(aos.toString()).isEqualTo("\r\ntestContent");
assertThat(part.getDataHandler().getContentType()).isEqualTo("text/plain; charset=UTF-8");
}
示例4: saveAttachMent
import javax.mail.Multipart; //導入依賴的package包/類
/**
* 【保存附件】
*/
public void saveAttachMent(Part part) throws Exception {
String fileName = "";
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)))) {
fileName = mpart.getFileName();
if (fileName.toLowerCase().indexOf("gb2312") != -1) {
fileName = MimeUtility.decodeText(fileName);
}
saveFile(fileName, mpart.getInputStream());
} else if (mpart.isMimeType("multipart/*")) {
saveAttachMent(mpart);
} else {
fileName = mpart.getFileName();
if ((fileName != null)
&& (fileName.toLowerCase().indexOf("GB2312") != -1)) {
fileName = MimeUtility.decodeText(fileName);
saveFile(fileName, mpart.getInputStream());
}
}
}
} else if (part.isMimeType("message/rfc822")) {
saveAttachMent((Part) part.getContent());
}
}
示例5: processMultiPart
import javax.mail.Multipart; //導入依賴的package包/類
/**
* Find "text" parts of message recursively and appends it to sb StringBuilder
*
* @param multipart Multipart to process
* @param sb StringBuilder
* @throws MessagingException
* @throws IOException
*/
private void processMultiPart(Multipart multipart, StringBuilder sb) throws MessagingException, IOException
{
boolean isAlternativeMultipart = multipart.getContentType().contains(MimetypeMap.MIMETYPE_MULTIPART_ALTERNATIVE);
if (isAlternativeMultipart)
{
processAlternativeMultipart(multipart, sb);
}
else
{
for (int i = 0, n = multipart.getCount(); i < n; i++)
{
Part part = multipart.getBodyPart(i);
if (part.getContent() instanceof Multipart)
{
processMultiPart((Multipart) part.getContent(), sb);
}
else
{
processPart(part, sb);
}
}
}
}
示例6: testSendMultipartHtmlEmail
import javax.mail.Multipart; //導入依賴的package包/類
@Test
public void testSendMultipartHtmlEmail() throws Exception {
mailService.sendEmail("[email protected]", "testSubject", "testContent", true, true);
verify(javaMailSender).send((MimeMessage) messageCaptor.capture());
MimeMessage message = (MimeMessage) messageCaptor.getValue();
MimeMultipart mp = (MimeMultipart) message.getContent();
MimeBodyPart part = (MimeBodyPart) ((MimeMultipart) mp.getBodyPart(0).getContent()).getBodyPart(0);
ByteArrayOutputStream aos = new ByteArrayOutputStream();
part.writeTo(aos);
assertThat(message.getSubject()).isEqualTo("testSubject");
assertThat(message.getAllRecipients()[0].toString()).isEqualTo("[email protected]");
assertThat(message.getFrom()[0].toString()).isEqualTo("[email protected]");
assertThat(message.getContent()).isInstanceOf(Multipart.class);
assertThat(aos.toString()).isEqualTo("\r\ntestContent");
assertThat(part.getDataHandler().getContentType()).isEqualTo("text/html;charset=UTF-8");
}
示例7: isContainAttach
import javax.mail.Multipart; //導入依賴的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;
}
示例8: getAttachments
import javax.mail.Multipart; //導入依賴的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;
}
示例9: buildMessage
import javax.mail.Multipart; //導入依賴的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;
}
示例10: handleAttachments
import javax.mail.Multipart; //導入依賴的package包/類
/**
* ������
*/
private void handleAttachments(Message message, Part part) throws Exception {
if (part.isMimeType("multipart/*")) {
Multipart mp = (Multipart) part.getContent();
for (int i = 0; i < mp.getCount(); i++) {
BodyPart bp = mp.getBodyPart(i);
String disposition = bp.getDisposition();
if (disposition != null && (disposition.equals(Part.ATTACHMENT) || disposition.equals(Part.INLINE))) {
saveFile(message, bp);
} else if (bp.isMimeType("multipart/*")) {
handleAttachments(message, (Part) part.getContent());
} else {
saveFile(message, bp);
}
}
} else if (part.isMimeType("message/rfc822")) {
handleAttachments(message, (Part) part.getContent());
}
}
示例11: getMailTextContent
import javax.mail.Multipart; //導入依賴的package包/類
public void getMailTextContent(Part part, StringBuffer content)
throws MessagingException, IOException {
// 如果是文本類型的附件,通過getContent方法可以取到文本內容,但這不是我們需要的結果,所以在這裏要做判斷
boolean isContainTextAttach = part.getContentType().indexOf("name") > 0;
if (part.isMimeType("text/*") && !isContainTextAttach) {
content.append(part.getContent().toString());
} else if (part.isMimeType("message/rfc822")) {
getMailTextContent((Part) part.getContent(), content);
} else if (part.isMimeType("multipart/*")) {
Multipart multipart = (Multipart) part.getContent();
int partCount = multipart.getCount();
for (int i = 0; i < partCount; i++) {
BodyPart bodyPart = multipart.getBodyPart(i);
getMailTextContent(bodyPart, content);
}
}
}
示例12: buildMultipartAlternativeMail
import javax.mail.Multipart; //導入依賴的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;
}
示例13: getAttachments
import javax.mail.Multipart; //導入依賴的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;
}
示例14: getBodyText
import javax.mail.Multipart; //導入依賴的package包/類
private List<String> getBodyText(Part part) throws MessagingException, IOException {
Object c = part.getContent();
if (c instanceof String) {
return UtilMisc.toList((String) c);
} else if (c instanceof Multipart) {
List<String> textContent = new LinkedList<String>();
int count = ((Multipart) c).getCount();
for (int i = 0; i < count; i++) {
BodyPart bp = ((Multipart) c).getBodyPart(i);
textContent.addAll(this.getBodyText(bp));
}
return textContent;
} else {
return new LinkedList<String>();
}
}
示例15: processMessageContent
import javax.mail.Multipart; //導入依賴的package包/類
protected static void processMessageContent(Message message, Mail mail) throws MessagingException, IOException {
if (isMultipartMessage(message)) {
Multipart multipart = (Multipart) message.getContent();
int numberOfParts = multipart.getCount();
for (int partCount = 0; partCount < numberOfParts; partCount++) {
BodyPart bodyPart = multipart.getBodyPart(partCount);
processMessagePartContent(bodyPart, mail);
}
} else {
processMessagePartContent(message, mail);
}
}