本文整理汇总了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();
}