use of com.zimbra.cs.service.mail.ParseMimeMessage.MessageAddresses in project zm-mailbox by Zimbra.
the class BounceMsg method getResentAddressees.
/** Retrieves the sender and recipient addresses from the {@code <m>}
* element. Note that "to" {@code <e>} elements map to {@code Resent-To}
* addresses, "from" {@code <e>} elements map to {@code Resent-From}
* addresses, etc. The authenticated user's default charset is used
* to 2047-encode the display names when needed.
* @param msgElem The {@code <m>} element containing the addresses.
* @param acct The authenticated user.
* @param msender The {@link MailSender} to be used to send the message.
* @return a {@link MessageAddresses} element encapsulating all the
* addresses specified by {@code <e>} subelements. */
MessageAddresses getResentAddressees(Element msgElem, Account acct, MailSender msender) throws ServiceException {
String defaultCharset = acct.getPrefMailDefaultCharset();
if (Strings.isNullOrEmpty(defaultCharset)) {
defaultCharset = MimeConstants.P_CHARSET_UTF8;
}
MessageAddresses maddrs = new MessageAddresses();
for (Element e : msgElem.listElements(MailConstants.E_EMAIL)) {
try {
maddrs.add(e, defaultCharset);
} catch (IOException ioe) {
throw ServiceException.FAILURE("error generating addressees", ioe);
}
}
if (maddrs.isEmpty()) {
throw ServiceException.INVALID_REQUEST("no recipients specified", null);
}
return maddrs;
}
use of com.zimbra.cs.service.mail.ParseMimeMessage.MessageAddresses in project zm-mailbox by Zimbra.
the class BounceMsg method addResentHeaders.
/** Adds a full set of {@code Resent-*} headers to the {@code MimeMessage}.
* Addressees are retrieved from the {@code <m>} element; note that "to"
* {@code <e>} elements map to {@code Resent-To} addresses, "from" {@code
* <e>} elements map to {@code Resent-From} addresses, etc. Validates
* {@code Resent-From} and {@code Resent-Sender} in the same manner as
* {@code From} and {@code Sender} are treated in normal mail send.
* Updates the {@link MailSender}'s envelope with the sender and recipient
* {@code Resent-*} addresses. */
MailSender addResentHeaders(Element msgElem, ZMimeMessage mm, ZimbraSoapContext zsc, OperationContext octxt, Account acct, MailSender msender) throws MessagingException, ServiceException {
MessageAddresses maddrs = getResentAddressees(msgElem, acct, msender);
// RFC 5322 section 3.6.6:
// When resent fields are used, the "Resent-From:" and "Resent-Date:"
// fields MUST be sent. The "Resent-Message-ID:" field SHOULD be sent.
// "Resent-Sender:" SHOULD NOT be used if "Resent-Sender:" would be
// identical to "Resent-From:".
// UniqueValue.getUniqueMessageIDValue() isn't visible, so get a message-id another way
String msgid = new ZMimeMessage(mm.getSession()).getMessageID();
mm.addHeader("Resent-Message-ID", msgid);
List<String> recipients = new ArrayList<String>(5);
recipients.addAll(addResentRecipientHeader(mm, "Resent-Bcc", maddrs.get(EmailType.BCC.toString())));
recipients.addAll(addResentRecipientHeader(mm, "Resent-Cc", maddrs.get(EmailType.CC.toString())));
recipients.addAll(addResentRecipientHeader(mm, "Resent-To", maddrs.get(EmailType.TO.toString())));
if (recipients.isEmpty()) {
throw ServiceException.INVALID_REQUEST("no recipients specified", null);
}
InternetAddress rfrom = ArrayUtil.getFirstElement(maddrs.get(EmailType.FROM.toString()));
InternetAddress rsender = ArrayUtil.getFirstElement(maddrs.get(EmailType.SENDER.toString()));
Pair<InternetAddress, InternetAddress> fromsender = msender.getSenderHeaders(rfrom, rsender, acct, getAuthenticatedAccount(zsc), octxt != null ? octxt.isUsingAdminPrivileges() : false);
InternetAddress from = fromsender.getFirst();
InternetAddress sender = fromsender.getSecond();
assert (from != null);
if (sender != null) {
mm.addHeader("Resent-Sender", sender.toString());
}
mm.addHeader("Resent-From", from.toString());
mm.addHeader("Resent-Date", DateUtil.toRFC822Date(new Date(System.currentTimeMillis())));
mm.saveChanges();
// now that we've updated the MimeMessage's headers, we can update the MailSender's envelope
msender.setEnvelopeFrom(from.getAddress());
msender.setRecipients(recipients.toArray(new String[recipients.size()]));
return msender;
}
Aggregations