use of javax.mail.internet.MimeBodyPart in project perun by CESNET.
the class MessagePreparator method createAttachment.
/**
* Creates attachment from filename
*
* @param filename
* @return
* @throws Exception
*/
private BodyPart createAttachment(String filename) throws Exception {
BodyPart attachBodypart = new MimeBodyPart();
File file = new File(filename);
FileDataSource fds = new FileDataSource(file);
DataHandler dh = new DataHandler(fds);
attachBodypart.setFileName(file.getName());
attachBodypart.setDisposition(Part.ATTACHMENT);
attachBodypart.setDescription("Attached file: " + file.getName());
attachBodypart.setDataHandler(dh);
logger.debug("ATTACHMENT ADDED ; filename: " + filename);
return attachBodypart;
}
use of javax.mail.internet.MimeBodyPart 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("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;
}
use of javax.mail.internet.MimeBodyPart in project ats-framework by Axway.
the class MimePackage method addAlternativePart.
/**
* Add a multipart/alternative part to message body
*
* @param plainContent
* the content of the text/plain sub-part
* @param htmlContent
* the content of the text/html sub-part
* @param charset
* the character set for the part
* @throws PackageException
*/
@PublicAtsApi
public void addAlternativePart(String plainContent, String htmlContent, String charset) throws PackageException {
MimeMultipart alternativePart = new MimeMultipart("alternative");
try {
// create a new text/plain part
MimeBodyPart plainPart = new MimeBodyPart();
plainPart.setText(plainContent, charset, PART_TYPE_TEXT_PLAIN);
plainPart.setDisposition(MimeBodyPart.INLINE);
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setText(htmlContent, charset, PART_TYPE_TEXT_HTML);
htmlPart.setDisposition(MimeBodyPart.INLINE);
alternativePart.addBodyPart(plainPart, 0);
alternativePart.addBodyPart(htmlPart, 1);
MimeBodyPart mimePart = new MimeBodyPart();
mimePart.setContent(alternativePart);
addPart(mimePart, PART_POSITION_LAST);
} catch (MessagingException me) {
throw new PackageException(me);
}
}
use of javax.mail.internet.MimeBodyPart in project zm-mailbox by Zimbra.
the class FilterUtil method reject.
public static void reject(OperationContext octxt, Mailbox mailbox, ParsedMessage parsedMessage, String reason, LmtpEnvelope envelope) throws MessagingException, ServiceException {
MimeMessage mimeMessage = parsedMessage.getMimeMessage();
if (isMailLoop(mailbox, mimeMessage, new String[] { HEADER_FORWARDED })) {
// Detected a mail loop. Do not send MDN, but just discard the message
String error = String.format("Detected a mail loop for message %s. No MDN sent.", Mime.getMessageID(mimeMessage));
ZimbraLog.filter.info(error);
throw ServiceException.FAILURE(error, null);
}
String reportTo = null;
if (envelope != null && envelope.hasSender()) {
reportTo = envelope.getSender().getEmailAddress();
}
if (reportTo == null || reportTo.isEmpty()) {
String[] returnPath = mimeMessage.getHeader(HEADER_RETURN_PATH);
if (returnPath == null || returnPath.length == 0) {
// >> NOT be generated if the MAIL FROM (or Return-Path) is empty.
throw new MessagingException("Neither 'envelope from' nor 'Return-Path' specified. Can't locate the address to reject to (No MDN sent)");
} else {
// At least one 'return-path' should exist.
reportTo = returnPath[0];
}
}
Account owner = mailbox.getAccount();
Locale locale = owner.getLocale();
String charset = owner.getPrefMailDefaultCharset();
if (charset == null) {
charset = MimeConstants.P_CHARSET_UTF8;
}
SMTPMessage report = new SMTPMessage(JMSession.getSmtpSession());
// add the forwarded header account names to detect the mail loop between accounts
for (String headerFwdAccountName : Mime.getHeaders(mimeMessage, HEADER_FORWARDED)) {
report.addHeader(HEADER_FORWARDED, headerFwdAccountName);
}
report.addHeader(HEADER_FORWARDED, owner.getName());
// MDN header
report.setEnvelopeFrom("<>");
report.setRecipient(javax.mail.Message.RecipientType.TO, new JavaMailInternetAddress(reportTo));
String subject = L10nUtil.getMessage(MsgKey.seiveRejectMDNSubject, locale);
report.setSubject(subject);
report.setSentDate(new Date());
InternetAddress address = new JavaMailInternetAddress(owner.getName());
report.setFrom(address);
MimeMultipart multi = new ZMimeMultipart("report");
// part 1: human-readable notification
String text = L10nUtil.getMessage(MsgKey.seiveRejectMDNErrorMsg, locale) + "\n" + reason;
MimeBodyPart mpText = new ZMimeBodyPart();
mpText.setText(text, CharsetUtil.checkCharset(text, charset));
multi.addBodyPart(mpText);
// part 2: disposition notification
StringBuilder mdn = new StringBuilder();
mdn.append("Final-Recipient: rfc822;").append(owner.getName()).append("\r\n");
mdn.append("Disposition: automatic-action/MDN-sent-automatically");
mdn.append("; deleted\r\n");
MimeBodyPart mpMDN = new ZMimeBodyPart();
mpMDN.setText(mdn.toString(), MimeConstants.P_CHARSET_UTF8);
mpMDN.setHeader("Content-Type", "message/disposition-notification; charset=utf-8");
multi.addBodyPart(mpMDN);
// Assemble the MDN
report.setContent(multi);
report.setHeader("Content-Type", multi.getContentType() + "; report-type=disposition-notification");
report.saveChanges();
MailSender mailSender = mailbox.getMailSender().setSaveToSent(false);
mailSender.setRecipients(reportTo);
mailSender.setEnvelopeFrom("<>");
mailSender.sendMimeMessage(octxt, mailbox, report);
}
use of javax.mail.internet.MimeBodyPart in project zm-mailbox by Zimbra.
the class CalendarItem method createBlob.
/**
* The Blob for the appointment/task is currently single Mime multipart/digest which has
* each invite's MimeMessage stored as a part.
*
* @param invPm
* @param firstInvite
* @throws ServiceException
*/
private MailboxBlob createBlob(ParsedMessage invPm, Invite firstInvite) throws ServiceException {
// Create blob only if there's an attachment or DESCRIPTION is too big to be stored in metadata.
if (!firstInvite.hasAttachment() && (invPm == null || firstInvite.descInMeta())) {
// If we're not going to create a blob after all, we must at least save the metadata to db.
// It's weird that the db update is implicitly required of this method, but that's the way
// it is, unfortunately. If we're creating a blob, the implicit db update is done by
// storeUpdatedBlob() call. (see below)
saveMetadata();
return null;
}
try {
// create the toplevel multipart/digest...
MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession());
MimeMultipart mmp = new ZMimeMultipart("digest");
mm.setContent(mmp);
// add the invite
MimeBodyPart mbp = new ZMimeBodyPart();
mbp.setDataHandler(new DataHandler(new PMDataSource(invPm)));
mbp.addHeader("invId", Integer.toString(firstInvite.getMailItemId()));
mmp.addBodyPart(mbp);
mm.saveChanges();
return storeUpdatedBlob(mm);
} catch (MessagingException e) {
throw ServiceException.FAILURE("MessagingException " + e, e);
} catch (IOException e) {
throw ServiceException.FAILURE("IOException " + e, e);
}
}
Aggregations