use of com.zimbra.common.zmime.ZMimeMessage in project zm-mailbox by Zimbra.
the class TestUtil method getHeaderValue.
public static String getHeaderValue(ZMailbox mbox, ZMessage msg, String headerName) throws Exception {
String content = msg.getContent();
if (content == null) {
content = getContent(mbox, msg.getId());
}
assertNotNull("Content was not fetched from the server", content);
MimeMessage mimeMsg = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes()));
return mimeMsg.getHeader(headerName, null);
}
use of com.zimbra.common.zmime.ZMimeMessage in project zm-mailbox by Zimbra.
the class CalendarMailSender method createCalendarMessage.
public static MimeMessage createCalendarMessage(Account account, Address fromAddr, Address senderAddr, List<Address> toAddrs, MimeMessage srcMm, Invite inv, ZVCalendar cal, boolean replyToSender) throws ServiceException {
try {
String uid = inv.getUid();
if (srcMm != null) {
// Get a copy so we can modify it.
MimeMessage mm = new ZMimeMessage(srcMm);
// Discard all old headers except Subject and Content-*.
Enumeration eh = srcMm.getAllHeaders();
while (eh.hasMoreElements()) {
Header hdr = (Header) eh.nextElement();
String hdrNameUpper = hdr.getName().toUpperCase();
if (!hdrNameUpper.startsWith("CONTENT-") && !hdrNameUpper.equals("SUBJECT")) {
mm.removeHeader(hdr.getName());
}
}
mm.setSentDate(new Date());
if (toAddrs != null) {
Address[] addrs = new Address[toAddrs.size()];
toAddrs.toArray(addrs);
mm.setRecipients(javax.mail.Message.RecipientType.TO, addrs);
} else {
mm.setRecipients(javax.mail.Message.RecipientType.TO, (Address[]) null);
}
mm.setRecipients(javax.mail.Message.RecipientType.CC, (Address[]) null);
mm.setRecipients(javax.mail.Message.RecipientType.BCC, (Address[]) null);
if (fromAddr != null)
mm.setFrom(fromAddr);
if (senderAddr != null) {
mm.setSender(senderAddr);
if (replyToSender)
mm.setReplyTo(new Address[] { senderAddr });
}
// Find and replace the existing calendar part with the new calendar object.
CalendarPartReplacingVisitor visitor = new CalendarPartReplacingVisitor(uid, cal);
visitor.accept(mm);
mm.saveChanges();
return mm;
} else {
String subject = inv.getName();
String desc = inv.getDescription();
String descHtml = inv.getDescriptionHtml();
return createCalendarMessage(account, fromAddr, senderAddr, toAddrs, subject, desc, descHtml, uid, cal, false);
}
} catch (MessagingException e) {
throw ServiceException.FAILURE("Messaging Exception while building calendar message from source MimeMessage", e);
}
}
use of com.zimbra.common.zmime.ZMimeMessage in project zm-mailbox by Zimbra.
the class CalendarMailSender method createForwardedInviteMessage.
public static MimeMessage createForwardedInviteMessage(MimeMessage mmOrig, String origSenderEmail, String forwarderEmail, String[] forwardTo) {
List<Address> rcpts = new ArrayList<Address>();
for (String to : forwardTo) {
try {
rcpts.add(new JavaMailInternetAddress(to));
} catch (AddressException e) {
ZimbraLog.calendar.warn("Ignoring invalid address \"" + to + "\" during invite forward");
}
}
if (rcpts.isEmpty())
return null;
MimeMessage mm = null;
try {
mm = new ZMimeMessage(mmOrig);
mm.removeHeader("To");
mm.removeHeader("Cc");
mm.removeHeader("Bcc");
mm.addRecipients(RecipientType.TO, rcpts.toArray(new Address[0]));
// Set Reply-To to the original sender.
mm.setReplyTo(new Address[] { new JavaMailInternetAddress(origSenderEmail) });
mm.removeHeader("Date");
mm.removeHeader("Message-ID");
mm.removeHeader("Return-Path");
mm.removeHeader("Received");
// Set special header to indicate the forwarding attendee.
mm.setHeader(CalendarMailSender.X_ZIMBRA_CALENDAR_INTENDED_FOR, forwarderEmail);
mm.saveChanges();
} catch (MessagingException e) {
ZimbraLog.calendar.warn("Unable to compose email for invite forwarding", e);
}
return mm;
}
use of com.zimbra.common.zmime.ZMimeMessage in project zm-mailbox by Zimbra.
the class TestSmtpClient method testMimeMessage.
@Test
public void testMimeMessage() throws Exception {
TestUtil.createAccount(USER_NAME);
TestUtil.createAccount(USER2_NAME);
// Assemble the message.
MimeMessage mm = new ZMimeMessage(JMSession.getSession());
InternetAddress addr = new JavaMailInternetAddress(TestUtil.getAddress(USER_NAME));
mm.setFrom(addr);
mm.setRecipient(RecipientType.TO, addr);
String subject = NAME_PREFIX + " testMimeMessage";
mm.setSubject(subject);
mm.setText("testMimeMessage");
mm.saveChanges();
// Initialize SMTP client.
SmtpConfig config = new SmtpConfig(mHost, mPort, "localhost");
SmtpConnection conn = new SmtpConnection(config);
conn.sendMessage(mm);
// Make sure it arrived.
ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
TestUtil.waitForMessage(mbox, "in:inbox subject:\"" + subject + "\"");
// Send the same message to a different envelope recipient.
conn.sendMessage(addr.getAddress(), new String[] { TestUtil.getAddress(USER2_NAME) }, mm);
mbox = TestUtil.getZMailbox(USER2_NAME);
TestUtil.waitForMessage(mbox, "in:inbox subject:\"" + subject + "\"");
}
use of com.zimbra.common.zmime.ZMimeMessage in project zm-mailbox by Zimbra.
the class ForwardCalendarItem method createMergedMessage.
// Take mmInv and mutate it. text/calendar part is replaced by cal and plain and html parts
// are replaced by plainDescPart and htmlDescPart.
private static MimeMessage createMergedMessage(Account account, Address fromAddr, Address senderAddr, MimeMessage mmInv, Invite inv, ZVCalendar cal, MimeBodyPart plainDescPart, MimeBodyPart htmlDescPart) throws ServiceException {
try {
String uid = inv.getUid();
if (mmInv != null) {
// Get a copy so we can modify it.
MimeMessage mm = new ZMimeMessage(mmInv);
// Discard all old headers except Subject and Content-*.
@SuppressWarnings("rawtypes") Enumeration eh = mmInv.getAllHeaders();
while (eh.hasMoreElements()) {
Header hdr = (Header) eh.nextElement();
String hdrNameUpper = hdr.getName().toUpperCase();
if (!hdrNameUpper.startsWith("CONTENT-") && !hdrNameUpper.equals("SUBJECT")) {
mm.removeHeader(hdr.getName());
}
}
mm.setSentDate(new Date());
mm.setRecipients(javax.mail.Message.RecipientType.TO, (Address[]) null);
mm.setRecipients(javax.mail.Message.RecipientType.CC, (Address[]) null);
mm.setRecipients(javax.mail.Message.RecipientType.BCC, (Address[]) null);
if (fromAddr != null)
mm.setFrom(fromAddr);
if (senderAddr != null)
mm.setSender(senderAddr);
// Find and replace the existing calendar part with the new calendar object.
ReplacingVisitor visitor = new ReplacingVisitor(uid, cal, plainDescPart, htmlDescPart);
visitor.accept(mm);
mm.saveChanges();
return mm;
} else {
String subject = inv.getName();
String desc, descHtml;
try {
Object plainContent = plainDescPart != null ? plainDescPart.getContent() : null;
desc = plainContent != null ? plainContent.toString() : null;
Object htmlContent = htmlDescPart != null ? htmlDescPart.getContent() : null;
descHtml = htmlContent != null ? htmlContent.toString() : null;
} catch (MessagingException e) {
throw ServiceException.FAILURE("Messaging Exception while retrieving description", e);
} catch (IOException e) {
throw ServiceException.FAILURE("Messaging Exception while retrieving description", e);
}
return CalendarMailSender.createCalendarMessage(account, fromAddr, senderAddr, null, subject, desc, descHtml, uid, cal, false);
}
} catch (MessagingException e) {
throw ServiceException.FAILURE("Messaging Exception while building calendar message from source MimeMessage", e);
}
}
Aggregations