use of javax.mail.BodyPart in project Lucee by lucee.
the class MailClient method getMultiPart.
private void getMultiPart(Query query, int row, Array attachments, Array attachmentFiles, Struct cids, Multipart multiPart, StringBuffer body) throws MessagingException, IOException {
int j = multiPart.getCount();
for (int k = 0; k < j; k++) {
BodyPart bodypart = multiPart.getBodyPart(k);
Object content;
if (bodypart.getFileName() != null) {
String filename = bodypart.getFileName();
try {
filename = Normalizer.normalize(MimeUtility.decodeText(filename), Normalizer.Form.NFC);
} catch (Throwable t) {
ExceptionUtil.rethrowIfNecessary(t);
}
if (bodypart.getHeader("Content-ID") != null) {
String[] ids = bodypart.getHeader("Content-ID");
String cid = ids[0].substring(1, ids[0].length() - 1);
cids.setEL(KeyImpl.init(filename), cid);
}
if (filename != null && ArrayUtil.find(attachments, filename) == 0) {
attachments.appendEL(filename);
if (attachmentDirectory != null) {
Resource file = attachmentDirectory.getRealResource(filename);
int l = 1;
String s2;
for (; uniqueFilenames && file.exists(); file = attachmentDirectory.getRealResource(s2)) {
String[] as = ResourceUtil.splitFileName(filename);
s2 = as.length != 1 ? as[0] + l++ + '.' + as[1] : as[0] + l++;
}
IOUtil.copy(bodypart.getInputStream(), file, true);
attachmentFiles.appendEL(file.getAbsolutePath());
}
}
} else if (bodypart.isMimeType("text/plain")) {
content = getConent(bodypart);
query.setAtEL(TEXT_BODY, row, content);
if (body.length() == 0)
body.append(content);
} else if (bodypart.isMimeType("text/html")) {
content = getConent(bodypart);
query.setAtEL(HTML_BODY, row, content);
if (body.length() == 0)
body.append(content);
} else if ((content = bodypart.getContent()) instanceof Multipart) {
getMultiPart(query, row, attachments, attachmentFiles, cids, (Multipart) content, body);
} else if (bodypart.getHeader("Content-ID") != null) {
String[] ids = bodypart.getHeader("Content-ID");
String cid = ids[0].substring(1, ids[0].length() - 1);
String filename = "cid:" + cid;
attachments.appendEL(filename);
if (attachmentDirectory != null) {
filename = "_" + Md5.getDigestAsString(filename);
Resource file = attachmentDirectory.getRealResource(filename);
int l = 1;
String s2;
for (; uniqueFilenames && file.exists(); file = attachmentDirectory.getRealResource(s2)) {
String[] as = ResourceUtil.splitFileName(filename);
s2 = as.length != 1 ? as[0] + l++ + '.' + as[1] : as[0] + l++;
}
IOUtil.copy(bodypart.getInputStream(), file, true);
attachmentFiles.appendEL(file.getAbsolutePath());
}
cids.setEL(KeyImpl.init(filename), cid);
}
}
}
use of javax.mail.BodyPart in project vboard by voyages-sncf-technologies.
the class MessagesController method sendNotificationEmail.
// Send notification and global emails
public void sendNotificationEmail(User to, String content, List<Pin> pins, String title) throws Exception {
// Javax mail used
MimeMessage message = this.emailSenderService.setEmail(to);
message.setSubject(title, "utf-8");
String footer = this.footerTemplate;
footer = String.format(footer, hostName);
content = content + footer;
message.setText(content, "utf-8", "html");
// This mail has 2 part, the BODY and the embedded image
MimeMultipart multipart = new MimeMultipart();
// first part (the html)
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(content, "text/html; charset=UTF-8");
multipart.addBodyPart(messageBodyPart);
HashSet<Profil> users = new HashSet<>();
// Images
for (Pin pin : pins) {
users.add(this.userDAO.findByEmail(User.getEmailFromString(pin.getAuthor()).get()));
if (!isBlank(pin.getImgType()) && !pin.getImgType().contains("<iframe") && !pin.getImgType().startsWith("https") && !pin.getImgType().endsWith("svg") && !pin.getImgType().endsWith("gif")) {
try {
messageBodyPart = new MimeBodyPart();
String vblogUrl = "http://" + hostName + "/vblog/wp-content/uploads";
DataSource img;
if (pin.getImgType().contains(vblogUrl)) {
img = new FileDataSource(imageManager.getBlogImagesDirectory().resolve(pin.getImgType().substring(pin.getImgType().indexOf(vblogUrl) + vblogUrl.length())).toFile());
} else {
img = new FileDataSource(imageManager.getPinsImagesDirectory().resolve(pin.getPinId() + ".png").toFile());
}
messageBodyPart.setDataHandler(new DataHandler(img));
messageBodyPart.setHeader("Content-ID", "<" + pin.getPinId() + ">");
// add image to the multipart
multipart.addBodyPart(messageBodyPart);
} catch (Exception e) {
this.logger.error("Image non trouvée sur le NAS:{}.png", pin.getPinId());
}
}
}
this.addLeadersImgs(multipart, users);
messageBodyPart = new MimeBodyPart();
DataSource img = new FileDataSource(imageManager.getAvatarImagesDirectory().resolve("default.png").toFile());
messageBodyPart.setDataHandler(new DataHandler(img));
messageBodyPart.setHeader("Content-ID", "<default>");
multipart.addBodyPart(messageBodyPart);
// put everything together
message.setContent(multipart);
// Send message
Transport.send(message);
}
Aggregations