本文整理匯總了Java中javax.mail.internet.MimeMultipart類的典型用法代碼示例。如果您正苦於以下問題:Java MimeMultipart類的具體用法?Java MimeMultipart怎麽用?Java MimeMultipart使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
MimeMultipart類屬於javax.mail.internet包,在下文中一共展示了MimeMultipart類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getTextFromMimeMultipart
import javax.mail.internet.MimeMultipart; //導入依賴的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: getMessagePart
import javax.mail.internet.MimeMultipart; //導入依賴的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;
}
示例3: testSendMultipartEmail
import javax.mail.internet.MimeMultipart; //導入依賴的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: toString
import javax.mail.internet.MimeMultipart; //導入依賴的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;
}
示例5: setAttachment
import javax.mail.internet.MimeMultipart; //導入依賴的package包/類
public static void setAttachment(Message message, String filename) throws MessagingException {
// Create a multipar message
Multipart multipart = new MimeMultipart();
BodyPart messageBodyPart = new MimeBodyPart();
//Set File
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
//Add "file part" to multipart
multipart.addBodyPart(messageBodyPart);
//Set multipart to message
message.setContent(multipart);
}
示例6: testSendMultipartHtmlEmail
import javax.mail.internet.MimeMultipart; //導入依賴的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: addAttachment
import javax.mail.internet.MimeMultipart; //導入依賴的package包/類
public static String addAttachment(MimeMultipart mm, String path)
{
if(count == Integer.MAX_VALUE)
{
count = 0;
}
int cid = count++;
try
{
java.io.File file = new java.io.File(path);
MimeBodyPart mbp = new MimeBodyPart();
mbp.setDisposition(MimeBodyPart.INLINE);
mbp.setContent(new MimeMultipart("mixed"));
mbp.setHeader("Content-ID", "<" + cid + ">");
mbp.setDataHandler(new DataHandler(new FileDataSource(file)));
mbp.setFileName(new String(file.getName().getBytes("GBK"), "ISO-8859-1"));
mm.addBodyPart(mbp);
return String.valueOf(cid);
}
catch(Exception e)
{
e.printStackTrace();
}
return "";
}
示例8: isContainAttachment
import javax.mail.internet.MimeMultipart; //導入依賴的package包/類
/**
* �ж��ʼ����Ƿ��������
* @param msg �ʼ�����
* @return �ʼ��д��ڸ�������true�������ڷ���false
*/
public static boolean isContainAttachment(Part part) throws MessagingException, IOException {
boolean flag = false;
if (part.isMimeType("multipart/*")) {
MimeMultipart multipart = (MimeMultipart) part.getContent();
int partCount = multipart.getCount();
for (int i = 0; i < partCount; i++) {
BodyPart bodyPart = multipart.getBodyPart(i);
String disp = bodyPart.getDisposition();
if (disp != null && (disp.equalsIgnoreCase(Part.ATTACHMENT) || disp.equalsIgnoreCase(Part.INLINE))) {
flag = true;
} else if (bodyPart.isMimeType("multipart/*")) {
flag = isContainAttachment(bodyPart);
} else {
String contentType = bodyPart.getContentType();
if (contentType.indexOf("application") != -1) {
flag = true;
}
if (contentType.indexOf("name") != -1) {
flag = true;
}
}
if (flag) break;
}
} else if (part.isMimeType("message/rfc822")) {
flag = isContainAttachment((Part)part.getContent());
}
return flag;
}
示例9: isContainAttachment
import javax.mail.internet.MimeMultipart; //導入依賴的package包/類
/**
* 判斷郵件中是否包含附件
* @param msg 郵件內容
* @return 郵件中存在附件返回true,不存在返回false
* @throws MessagingException
* @throws IOException
*/
public static boolean isContainAttachment(Part part) throws MessagingException, IOException {
boolean flag = false;
if (part.isMimeType("multipart/*")) {
MimeMultipart multipart = (MimeMultipart) part.getContent();
int partCount = multipart.getCount();
for (int i = 0; i < partCount; i++) {
BodyPart bodyPart = multipart.getBodyPart(i);
String disp = bodyPart.getDisposition();
if (disp != null && (disp.equalsIgnoreCase(Part.ATTACHMENT) || disp.equalsIgnoreCase(Part.INLINE))) {
flag = true;
} else if (bodyPart.isMimeType("multipart/*")) {
flag = isContainAttachment(bodyPart);
} else {
String contentType = bodyPart.getContentType();
if (contentType.indexOf("application") != -1) {
flag = true;
}
if (contentType.indexOf("name") != -1) {
flag = true;
}
}
if (flag) break;
}
} else if (part.isMimeType("message/rfc822")) {
flag = isContainAttachment((Part)part.getContent());
}
return flag;
}
示例10: testSendMultipartEmail
import javax.mail.internet.MimeMultipart; //導入依賴的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");
}
示例11: testSendMultipartHtmlEmail
import javax.mail.internet.MimeMultipart; //導入依賴的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");
}
示例12: sendAttachMail
import javax.mail.internet.MimeMultipart; //導入依賴的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;
}
}
示例13: buildEmailMessage
import javax.mail.internet.MimeMultipart; //導入依賴的package包/類
private Message buildEmailMessage(EmailInfo emailInfo)
throws AddressException, MessagingException, UnsupportedEncodingException {
MimeMessage message = new MimeMessage(this.session);
message.setFrom(new InternetAddress(emailInfo.getFrom(), "網頁更新訂閱係統", "UTF-8"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(emailInfo.getTo()));
Multipart multipart = new MimeMultipart();
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(emailInfo.getContent(), "text/html;charset=UTF-8");
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
message.setSubject(emailInfo.getTitle());
message.saveChanges();
return message;
}
示例14: hasNonTextAttachments
import javax.mail.internet.MimeMultipart; //導入依賴的package包/類
public static boolean hasNonTextAttachments(Part m) {
try {
Object content = m.getContent();
if (content instanceof MimeMultipart) {
MimeMultipart mm = (MimeMultipart) content;
for (int i=0;i<mm.getCount();i++) {
BodyPart p = mm.getBodyPart(i);
if (hasNonTextAttachments(p)) {
return true;
}
}
return false;
} else {
return !m.getContentType().trim().toLowerCase().startsWith("text");
}
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
示例15: addContentTrace
import javax.mail.internet.MimeMultipart; //導入依賴的package包/類
private String addContentTrace(
Object content,
String level ) throws MessagingException {
final String level1 = level;
final String level2 = "\t" + level1;
StringBuilder msg = new StringBuilder();
if (content instanceof String) {
msg.append(level2 + "BODY STRING START:\n");
msg.append((String) content);
msg.append(level2 + "BODY STRING END:\n");
} else if (content instanceof MimeMultipart) {
MimeMultipart multipart = (MimeMultipart) content;
for (int i = 0; i < multipart.getCount(); i++) {
msg.append(addBodyPartTrace(multipart.getBodyPart(i), level2));
}
} else {
msg.append(level2 + "*** CANNOT CONVERT UNSUPPORTED CONTENT: "
+ content.getClass().getCanonicalName() + " ***\n");
}
return msg.toString();
}