use of javax.activation.DataHandler in project zm-mailbox by Zimbra.
the class ZMimeBodyPart method recalculateTransferEncoding.
static void recalculateTransferEncoding(ZMimePart part) throws MessagingException {
// don't overwrite an explicitly-set transfer encoding
if (part.getHeader("Content-Transfer-Encoding") != null)
return;
// don't set transfer encoding on messages or multiparts
DataHandler dh = part.getDataHandler();
ZContentType ctype = new ZContentType(dh.getContentType());
if (ctype.getBaseType().equals("message/rfc822") || ctype.getPrimaryType().equals("multipart"))
return;
boolean isText = ctype.getPrimaryType().equals("text");
String disp = part.getDisposition();
boolean isAttachment = disp != null && disp.equals(ATTACHMENT);
String encoding = "base64";
if (dh.getName() == null) {
try {
EncodingOutputStream eos = new EncodingOutputStream();
dh.writeTo(eos);
encoding = eos.tester.getEncoding(isAttachment, isText);
} catch (Exception e) {
}
} else {
EncodingInputStream eis = null;
try {
eis = new EncodingInputStream(dh.getDataSource().getInputStream());
ByteUtil.drain(eis, false);
encoding = eis.tester.getEncoding(isAttachment, isText);
} catch (IOException e) {
} finally {
ByteUtil.closeStream(eis);
}
}
part.setHeader("Content-Transfer-Encoding", encoding);
}
use of javax.activation.DataHandler in project zm-mailbox by Zimbra.
the class ZMimeMessage method cacheContent.
public void cacheContent(Object o) {
dh = new DataHandler(o, null);
boolean placeholder = content == null && contentStream == null;
if (placeholder) {
content = ZMimeBodyPart.CONTENT_PLACEHOLDER;
}
try {
super.getContent();
} catch (Exception e) {
// should never happen
} finally {
dh = null;
if (placeholder) {
content = null;
}
}
}
use of javax.activation.DataHandler in project zm-mailbox by Zimbra.
the class MailboxTestUtil method generateMessageWithAttachment.
public static ParsedMessage generateMessageWithAttachment(String subject) throws Exception {
MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession());
mm.setHeader("From", "Vera Oliphant <oli@example.com>");
mm.setHeader("To", "Jimmy Dean <jdean@example.com>");
mm.setHeader("Subject", subject);
mm.setText("Good as gold");
MimeMultipart multi = new ZMimeMultipart("mixed");
ContentDisposition cdisp = new ContentDisposition(Part.ATTACHMENT);
cdisp.setParameter("filename", "fun.txt");
ZMimeBodyPart bp = new ZMimeBodyPart();
// it gets called before setting Content-Type and CTE headers.
try {
bp.setDataHandler(new DataHandler(new ByteArrayDataSource("Feeling attached.", "text/plain")));
} catch (IOException e) {
throw new MessagingException("could not generate mime part content", e);
}
bp.addHeader("Content-Disposition", cdisp.toString());
bp.addHeader("Content-Type", "text/plain");
bp.addHeader("Content-Transfer-Encoding", MimeConstants.ET_8BIT);
multi.addBodyPart(bp);
mm.setContent(multi);
mm.saveChanges();
return new ParsedMessage(mm, false);
}
use of javax.activation.DataHandler in project zm-mailbox by Zimbra.
the class AutoProvision method sendNotifMessage.
protected void sendNotifMessage(Account acct, String password) throws ServiceException {
String subject = fillTemplate(acct, domain.getAutoProvNotificationSubject());
String body = fillTemplate(acct, domain.getAutoProvNotificationBody());
String from = domain.getAutoProvNotificationFromAddress();
if (from == null) {
// TODO: should we use a seperate boolean control?
return;
}
String toAddr = acct.getName();
try {
SMTPMessage out = new SMTPMessage(JMSession.getSmtpSession());
InternetAddress addr = null;
try {
addr = new JavaMailInternetAddress(from);
} catch (AddressException e) {
// log and try the next one
ZimbraLog.autoprov.warn("invalid address in " + Provisioning.A_zimbraAutoProvNotificationFromAddress, e);
}
Address fromAddr = addr;
Address replyToAddr = addr;
// From
out.setFrom(fromAddr);
// Reply-To
out.setReplyTo(new Address[] { replyToAddr });
// To
out.setRecipient(javax.mail.Message.RecipientType.TO, new JavaMailInternetAddress(toAddr));
// Date
out.setSentDate(new Date());
// Subject
Locale locale = acct.getLocale();
out.setSubject(subject);
// NOTIFY=NEVER
out.setNotifyOptions(SMTPMessage.NOTIFY_NEVER);
// body
MimeMultipart mmp = new ZMimeMultipart("alternative");
// TEXT part (add me first!)
String text = body;
MimeBodyPart textPart = new ZMimeBodyPart();
textPart.setText(text, MimeConstants.P_CHARSET_UTF8);
mmp.addBodyPart(textPart);
// HTML part
StringBuilder html = new StringBuilder();
html.append("<h4>\n");
html.append("<p>" + body + "</p>\n");
html.append("</h4>\n");
html.append("\n");
MimeBodyPart htmlPart = new ZMimeBodyPart();
htmlPart.setDataHandler(new DataHandler(new HtmlPartDataSource(html.toString())));
mmp.addBodyPart(htmlPart);
out.setContent(mmp);
// send it
Transport.send(out);
// log
Address[] rcpts = out.getRecipients(javax.mail.Message.RecipientType.TO);
StringBuilder rcptAddr = new StringBuilder();
for (Address a : rcpts) rcptAddr.append(a.toString());
ZimbraLog.autoprov.info("auto provision notification sent rcpt='" + rcptAddr + "' Message-ID=" + out.getMessageID());
} catch (MessagingException e) {
ZimbraLog.autoprov.warn("send auto provision notification failed rcpt='" + toAddr + "'", e);
}
}
use of javax.activation.DataHandler 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;
}
Aggregations