use of javax.mail.Multipart in project fess-crawler by codelibs.
the class EmlExtractor method getBodyText.
protected String getBodyText(final MimeMessage message) {
StringBuilder buf = new StringBuilder(1000);
try {
final Object content = message.getContent();
if (content instanceof Multipart) {
final Multipart multipart = (Multipart) content;
final int count = multipart.getCount();
for (int i = 0; i < count; i++) {
final BodyPart bodyPart = multipart.getBodyPart(i);
if (Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition())) {
appendAttachment(buf, bodyPart);
} else if (bodyPart.isMimeType("text/plain")) {
buf.append(bodyPart.getContent().toString()).append(' ');
} else if (bodyPart.isMimeType("text/html")) {
buf.append(bodyPart.getContent().toString()).append(' ');
} else if (bodyPart.isMimeType("multipart/alternative") && bodyPart.getContent() instanceof Multipart) {
final Multipart alternativePart = (Multipart) bodyPart.getContent();
for (int j = 0; j < alternativePart.getCount(); j++) {
final BodyPart innerBodyPart = alternativePart.getBodyPart(j);
if (innerBodyPart.isMimeType("text/plain")) {
buf.append(innerBodyPart.getContent().toString()).append(' ');
break;
}
}
}
}
} else if (content instanceof String) {
buf.append(content.toString());
}
} catch (MessagingException | IOException e) {
throw new ExtractException(e);
}
return buf.toString();
}
use of javax.mail.Multipart in project java_Projects by listar2000.
the class MailGenerator method creatMessage.
public Message creatMessage(Session session) throws Exception {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(MailProperties.getEmail(), "BestBook.net", "UTF-8"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(receiver, "customer", "UTF-8"));
message.setSubject(mailTitle);
Multipart multipart = new MimeMultipart();
BodyPart textBody = new MimeBodyPart();
textBody.setContent(mailContent, "text/html; charset=utf-8");
multipart.addBodyPart(textBody);
if (!file.exists()) {
textBody.setContent("����ʾ����", "text/html; charset=utf-8");
} else {
BodyPart fileBody = new MimeBodyPart();
FileDataSource dataSource = new FileDataSource(file);
fileBody.setDataHandler(new DataHandler(dataSource));
multipart.addBodyPart(fileBody);
}
message.setContent(multipart);
message.saveChanges();
return message;
}
use of javax.mail.Multipart in project serverless by bluenimble.
the class SmtpMessenger method send.
@Override
public void send(Sender sender, Recipient[] recipients, String subject, String content, ApiStreamSource... attachments) throws MessengerException {
ClassLoader pcl = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
// creates a new e-mail message
try {
if (sender == null) {
sender = NoSender;
}
Message message = new MimeMessage(session);
String senderEmail = sender.id();
if (Lang.isNullOrEmpty(senderEmail)) {
senderEmail = user;
}
String senderName = sender.name();
if (Lang.isNullOrEmpty(senderName)) {
message.setFrom(new InternetAddress(senderEmail));
} else {
message.setFrom(new InternetAddress(senderEmail, senderName));
}
InternetAddress[] toAddresses = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++) {
toAddresses[i] = new InternetAddress(recipients[i].id());
}
message.setRecipients(Message.RecipientType.TO, toAddresses);
message.setSubject(subject);
message.setSentDate(new Date());
// creates message part
MimeBodyPart messageText = new MimeBodyPart();
messageText.setContent(content, ApiContentTypes.Html);
// creates multi-part
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageText);
// adds attachments
if (attachments != null && attachments.length > 0) {
for (final ApiStreamSource attachment : attachments) {
MimeBodyPart mbd = new MimeBodyPart();
DataSource ds = new DataSource() {
@Override
public String getContentType() {
return attachment.contentType();
}
@Override
public InputStream getInputStream() throws IOException {
return attachment.stream();
}
@Override
public String getName() {
return attachment.name();
}
@Override
public OutputStream getOutputStream() throws IOException {
throw new UnsupportedOperationException("getOutputStream not supported");
}
};
mbd.setDataHandler(new DataHandler(ds));
multipart.addBodyPart(mbd);
}
}
// sets the multi-part as e-mail's content
message.setContent(multipart);
// sends the e-mail
Transport.send(message);
} catch (Exception ex) {
throw new MessengerException(ex.getMessage(), ex);
} finally {
Thread.currentThread().setContextClassLoader(pcl);
}
}
use of javax.mail.Multipart in project i2p.i2p-bote by i2p.
the class Email method setContent.
/**
* Sets the message text and adds attachments.
* @param text
* @param attachments Can be <code>null</code>
* @throws MessagingException
*/
public void setContent(String text, List<Attachment> attachments) throws MessagingException {
if (attachments == null || attachments.isEmpty())
setText(text, "UTF-8");
else {
Multipart multiPart = new MimeMultipart();
MimeBodyPart textPart = new MimeBodyPart();
textPart.setText(text, "UTF-8");
multiPart.addBodyPart(textPart);
attach(multiPart, attachments);
setContent(multiPart);
}
}
use of javax.mail.Multipart in project perun by CESNET.
the class MessagePreparator method createHtmlMessage.
/**
* Creates HTML message from freemarker template
*
* @return
* @throws Exception
*/
private BodyPart createHtmlMessage() throws Exception {
Multipart htmlContent = new MimeMultipart("related");
BodyPart htmlPage = new MimeBodyPart();
htmlPage.setDataHandler(createDataHandler(content.getBytes(StandardCharsets.UTF_8), "text/html;charset=utf-8"));
htmlContent.addBodyPart(htmlPage);
BodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(htmlContent);
logger.debug("HTML MESSAGE CREATED ; content: " + content);
return htmlPart;
}
Aggregations