use of javax.mail.internet.MimeBodyPart in project zm-mailbox by Zimbra.
the class ParsedContact method generateMimeMessage.
private static MimeMessage generateMimeMessage(List<Attachment> attachments) throws MessagingException {
MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession());
MimeMultipart multi = new ZMimeMultipart("mixed");
int part = 1;
for (Attachment attach : attachments) {
ContentDisposition cdisp = new ContentDisposition(Part.ATTACHMENT);
cdisp.setParameter("filename", attach.getFilename()).setParameter("field", attach.getName());
MimeBodyPart bp = new ZMimeBodyPart();
// it gets called before setting Content-Type and CTE headers.
try {
bp.setDataHandler(new DataHandler(new ByteArrayDataSource(attach.getContent(), attach.getContentType())));
} catch (IOException e) {
throw new MessagingException("could not generate mime part content", e);
}
bp.addHeader("Content-Disposition", cdisp.toString());
bp.addHeader("Content-Type", attach.getContentType());
bp.addHeader("Content-Transfer-Encoding", MimeConstants.ET_8BIT);
multi.addBodyPart(bp);
attach.setPartName(Integer.toString(part++));
}
mm.setContent(multi);
mm.saveChanges();
return mm;
}
use of javax.mail.internet.MimeBodyPart in project zm-mailbox by Zimbra.
the class MimeVisitor method accept.
private final synchronized boolean accept(MimePart mp, int depth) throws MessagingException {
// do not recurse beyond a fixed depth
if (depth >= MAX_VISITOR_DEPTH)
return false;
boolean modified = false;
if (mp instanceof MimeMessage)
modified |= visitMessage((MimeMessage) mp, VisitPhase.VISIT_BEGIN);
String ctype = Mime.getContentType(mp);
boolean isMultipart = ctype.startsWith(MimeConstants.CT_MULTIPART_PREFIX);
boolean isMessage = !isMultipart && ctype.equals(MimeConstants.CT_MESSAGE_RFC822);
if (isMultipart) {
Object content = null;
try {
content = Mime.getMultipartContent(mp, ctype);
} catch (Exception e) {
ZimbraLog.extensions.warn("could not fetch multipart content; skipping", e);
}
if (content instanceof MimeMultipart) {
MimeMultipart multi = (MimeMultipart) content;
boolean multiModified = false;
if (visitMultipart(multi, VisitPhase.VISIT_BEGIN))
modified = multiModified = true;
try {
for (int i = 0; i < multi.getCount(); i++) {
BodyPart bp = multi.getBodyPart(i);
if (bp instanceof MimeBodyPart) {
if (accept((MimeBodyPart) bp, depth + 1))
modified = multiModified = true;
} else {
ZimbraLog.extensions.info("unexpected BodyPart subclass: " + bp.getClass().getName());
}
}
} catch (MessagingException e) {
ZimbraLog.extensions.warn("could not fetch body subpart; skipping remainder", e);
}
if (visitMultipart(multi, VisitPhase.VISIT_END))
modified = multiModified = true;
if (multiModified)
mp.setContent(multi);
}
} else if (isMessage) {
MimeMessage content = null;
try {
content = Mime.getMessageContent(mp);
} catch (Exception e) {
ZimbraLog.extensions.warn("could not fetch attached message content; skipping", e);
}
if (content != null)
modified |= accept(content, depth + 1);
} else if (mp instanceof MimeBodyPart) {
modified |= visitBodyPart((MimeBodyPart) mp);
} else if (!(mp instanceof MimeMessage)) {
ZimbraLog.extensions.info("unexpected MimePart subclass: " + mp.getClass().getName() + " (ctype='" + ctype + "')");
}
if (mp instanceof MimeMessage) {
MimeMessage mm = (MimeMessage) mp;
modified |= visitMessage(mm, VisitPhase.VISIT_END);
// commit changes to the message
if (modified)
mm.saveChanges();
}
return modified;
}
use of javax.mail.internet.MimeBodyPart in project zm-mailbox by Zimbra.
the class SpamHandler method sendReport.
private void sendReport(SpamReport sr) throws ServiceException, MessagingException {
Config config = Provisioning.getInstance().getConfig();
String isSpamString = sr.isSpam ? config.getSpamReportTypeSpam() : config.getSpamReportTypeHam();
SMTPMessage out = new SMTPMessage(JMSession.getSmtpSession());
Mailbox mbox = MailboxManager.getInstance().getMailboxById(sr.mailboxId);
Message msg = mbox.getMessageById(null, sr.messageId);
MimeMultipart mmp = new ZMimeMultipart("mixed");
MimeBodyPart infoPart = new ZMimeBodyPart();
infoPart.setHeader("Content-Description", "Zimbra spam classification report");
String body = String.format("Classified-By: %s\r\n" + "Classified-As: %s\r\n" + "Action: %s\r\n" + "Source-Folder: %s\r\n" + "Destination-Folder: %s\r\n" + "Destination-Mailbox: %s\r\n", Strings.nullToEmpty(sr.accountName), isSpamString, Strings.nullToEmpty(sr.action), Strings.nullToEmpty(sr.sourceFolder), Strings.nullToEmpty(sr.destFolder), Strings.nullToEmpty(sr.destAccountName));
infoPart.setText(body);
mmp.addBodyPart(infoPart);
MailboxBlob blob = msg.getBlob();
MimeBodyPart mbp = new ZMimeBodyPart();
mbp.setDataHandler(new DataHandler(new MailboxBlobDataSource(blob)));
mbp.setHeader("Content-Type", MimeConstants.CT_MESSAGE_RFC822);
mbp.setHeader("Content-Disposition", Part.ATTACHMENT);
mmp.addBodyPart(mbp);
out.setContent(mmp);
out.addHeader(config.getSpamReportSenderHeader(), sr.accountName);
out.addHeader(config.getSpamReportTypeHeader(), isSpamString);
if (config.isSmtpSendAddOriginatingIP() && sr.origIp != null)
out.addHeader(MailSender.X_ORIGINATING_IP, MailSender.formatXOrigIpHeader(sr.origIp));
out.setRecipient(javax.mail.Message.RecipientType.TO, sr.reportRecipient);
out.setEnvelopeFrom(config.getSpamReportEnvelopeFrom());
out.setSubject(config.getSpamTrainingSubjectPrefix() + " " + sr.accountName + ": " + isSpamString);
Transport.send(out);
ZimbraLog.misc.info("Sent " + sr);
}
use of javax.mail.internet.MimeBodyPart in project zm-mailbox by Zimbra.
the class UUEncodeConverter method visitMessage.
@Override
protected boolean visitMessage(MimeMessage mm, VisitPhase visitKind) throws MessagingException {
// do the decode in the exit phase
if (visitKind != VisitPhase.VISIT_END)
return false;
MimeMultipart mmp = null;
try {
// only check "text/plain" parts for uudecodeable attachments
if (!mm.isMimeType(MimeConstants.CT_TEXT_PLAIN))
return false;
// don't check transfer-encoded parts for uudecodeable attachments
String cte = mm.getHeader("Content-Transfer-Encoding", null);
if (cte != null) {
cte = cte.trim().toLowerCase();
if (!cte.equals(MimeConstants.ET_7BIT) && !cte.equals(MimeConstants.ET_8BIT) && !cte.equals(MimeConstants.ET_BINARY))
return false;
}
List<UUDecodedFile> uufiles = null;
// go through top-level text/plain part and extract uuencoded files
PositionInputStream is = null;
long size;
try {
is = new PositionInputStream(new BufferedInputStream(mm.getInputStream()));
for (int c = is.read(); c != -1; ) {
long start = is.getPosition() - 1;
// check for uuencode header: "begin NNN filename"
if (c == 'b' && (c = is.read()) == 'e' && (c = is.read()) == 'g' && (c = is.read()) == 'i' && (c = is.read()) == 'n' && ((c = is.read()) == ' ' || c == '\t') && Character.isDigit((c = is.read())) && Character.isDigit(c = is.read()) && Character.isDigit(c = is.read()) && ((c = is.read()) == ' ' || c == '\t')) {
StringBuilder sb = new StringBuilder();
while ((c = is.read()) != '\r' && c != '\n' && c != -1) sb.append((char) c);
String filename = FileUtil.trimFilename(sb.toString().trim());
if (c != -1 && filename.length() > 0) {
if (uufiles == null)
uufiles = new ArrayList<UUDecodedFile>(3);
try {
uufiles.add(new UUDecodedFile(is, filename, start));
// check to make sure that the caller's OK with altering the message
if (uufiles.size() == 1 && mCallback != null && !mCallback.onModification())
return false;
} catch (IOException ioe) {
}
}
}
// skip to the beginning of the next line
while (c != '\r' && c != '\n' && c != -1) c = is.read();
while (c == '\r' || c == '\n') c = is.read();
}
size = is.getPosition();
} finally {
ByteUtil.closeStream(is);
}
if (uufiles == null || uufiles.isEmpty())
return false;
// create MimeParts for the extracted files
mmp = new ZMimeMultipart("mixed");
for (UUDecodedFile uu : uufiles) {
MimeBodyPart mbp = new ZMimeBodyPart();
mbp.setHeader("Content-Type", uu.getContentType());
mbp.setHeader("Content-Disposition", new ContentDisposition(Part.ATTACHMENT).setParameter("filename", uu.getFilename()).toString());
mbp.setDataHandler(new DataHandler(uu.getDataSource()));
mmp.addBodyPart(mbp);
size -= uu.getEndOffset() - uu.getStartOffset();
}
// take the remaining text and put it in as the first "related" part
InputStream isOrig = null;
try {
isOrig = mm.getInputStream();
long offset = 0;
ByteArrayOutputStream baos = new ByteArrayOutputStream((int) size);
byte[] buffer = new byte[8192];
for (UUDecodedFile uu : uufiles) {
long count = uu.getStartOffset() - offset, numRead;
while (count > 0 && (numRead = isOrig.read(buffer, 0, (int) Math.min(count, 8192))) >= 0) {
baos.write(buffer, 0, (int) numRead);
count -= numRead;
}
isOrig.skip(uu.getEndOffset() - uu.getStartOffset());
offset = uu.getEndOffset();
}
ByteUtil.copy(isOrig, true, baos, true);
MimeBodyPart mbp = new ZMimeBodyPart();
mbp.setDataHandler(new DataHandler(new ByteArrayDataSource(baos.toByteArray(), MimeConstants.CT_TEXT_PLAIN)));
mmp.addBodyPart(mbp, 0);
} finally {
ByteUtil.closeStream(isOrig);
}
} catch (MessagingException e) {
ZimbraLog.extensions.warn("exception while uudecoding message part; skipping part", e);
return false;
} catch (IOException e) {
ZimbraLog.extensions.warn("exception while uudecoding message part; skipping part", e);
return false;
}
// replace the top-level part with a new multipart/related
mm.setContent(mmp);
mm.setHeader("Content-Type", mmp.getContentType() + "; generated=true");
return true;
}
use of javax.mail.internet.MimeBodyPart in project zm-mailbox by Zimbra.
the class TnefConverter method visitMessage.
@Override
protected boolean visitMessage(MimeMessage msg, VisitPhase visitKind) throws MessagingException {
// do the decode in the exit phase
if (visitKind == VisitPhase.VISIT_BEGIN) {
mMimeMessage = msg;
return false;
}
try {
// we only care about "application/ms-tnef" content
if (!TNEFUtils.isTNEFMimeType(msg.getContentType()))
return false;
// check to make sure that the caller's OK with altering the message
if (mCallback != null && !mCallback.onModification())
return false;
} catch (MessagingException e) {
ZimbraLog.extensions.warn("exception while checking message for TNEF content; skipping part");
return false;
}
MimeMultipart multi = null;
MimeBodyPart tnefPart = null;
try {
Object content = msg.getContent();
if (!(content instanceof MimeBodyPart))
return false;
tnefPart = (MimeBodyPart) content;
// try to expand the TNEF into a suitable multipart
multi = expandTNEF(tnefPart);
if (multi == null)
return false;
// add the TNEF to the expanded multipart
multi.addBodyPart(tnefPart, 0);
} catch (MessagingException e) {
ZimbraLog.extensions.warn("exception while decoding TNEF message content; skipping part", e);
return false;
} catch (IOException e) {
ZimbraLog.extensions.warn("exception while decoding TNEF message content; skipping part", e);
return false;
}
// and put the new multipart/alternative where the TNEF used to be
msg.setContent(multi);
msg.setHeader("Content-Type", multi.getContentType() + "; generated=true");
return true;
}
Aggregations