use of com.zimbra.common.zmime.ZMimeBodyPart 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 com.zimbra.common.zmime.ZMimeBodyPart 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 com.zimbra.common.zmime.ZMimeBodyPart 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 com.zimbra.common.zmime.ZMimeBodyPart 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);
}
}
use of com.zimbra.common.zmime.ZMimeBodyPart 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;
}
Aggregations