use of com.zimbra.common.zmime.ZMimeMessage 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;
}
use of com.zimbra.common.zmime.ZMimeMessage in project zm-mailbox by Zimbra.
the class BounceMsg method handle.
@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
ZimbraSoapContext zsc = getZimbraSoapContext(context);
Account acct = getRequestedAccount(zsc);
Mailbox mbox = getRequestedMailbox(zsc);
OperationContext octxt = getOperationContext(zsc, context);
Element msgElem = request.getElement(MailConstants.E_MSG);
ItemId iid = new ItemId(msgElem.getAttribute(MailConstants.A_ID), zsc);
MailSender msender = mbox.getMailSender().setSaveToSent(false).setRedirectMode(true).setSkipHeaderUpdate(true);
Upload upload = null;
try {
InputStream is;
if (iid.belongsTo(mbox)) {
is = mbox.getMessageById(octxt, iid.getId()).getContentStream();
} else if (iid.isLocal()) {
Mailbox mboxSrc = MailboxManager.getInstance().getMailboxByAccountId(iid.getAccountId());
is = mboxSrc.getMessageById(octxt, iid.getId()).getContentStream();
} else {
upload = UserServlet.getRemoteResourceAsUpload(zsc.getAuthToken(), iid, Maps.<String, String>newHashMap());
is = upload.getInputStream();
}
ZMimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSmtpSession(acct), is);
addResentHeaders(msgElem, mm, zsc, octxt, acct, msender);
msender.sendMimeMessage(octxt, mbox, mm);
} catch (MessagingException me) {
throw ServiceException.FAILURE("error generating new message", me);
} catch (IOException ioe) {
throw ServiceException.FAILURE("error fetching remote message", ioe);
} finally {
// always want to delete the upload; msender.setUploads() deletes only on success
if (upload != null) {
FileUploadServlet.deleteUpload(upload);
}
}
Element response = zsc.createElement(MailConstants.BOUNCE_MSG_RESPONSE);
return response;
}
use of com.zimbra.common.zmime.ZMimeMessage in project zm-mailbox by Zimbra.
the class TestSpam method testSpam.
/**
* Tests {@link Mime#isSpam}.
*/
@Test
public void testSpam() throws Exception {
String coreContent = TestUtil.getTestMessage(NAME_PREFIX + " testSpam", USER_NAME, USER_NAME, null);
MimeMessage msg = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(coreContent.getBytes()));
assertFalse("Msg should not be identified as spam", SpamHandler.isSpam(msg));
// Test single-line spam header (common case)
String headerName = prov.getConfig().getSpamHeader();
String singleLineSpamContent = headerName + ": YES\r\n" + coreContent;
msg = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(singleLineSpamContent.getBytes()));
assertTrue("Msg should be identified as spam", SpamHandler.isSpam(msg));
// Test folded spam header (bug 24954).
prov.getConfig().setSpamHeaderValue("spam.*");
String folderSpamContent = headerName + ": spam, SpamAssassin (score=5.701, required 5,\r\n" + " DCC_CHECK 1.37, FH_RELAY_NODNS 1.45, RATWARE_RCVD_PF 2.88)\r\n" + coreContent;
msg = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(folderSpamContent.getBytes()));
assertTrue("Msg should be identified as spam", SpamHandler.isSpam(msg));
}
use of com.zimbra.common.zmime.ZMimeMessage in project zm-mailbox by Zimbra.
the class SendMsg method parseUploadedMessage.
static MimeMessage parseUploadedMessage(ZimbraSoapContext zsc, String attachId, MimeMessageData mimeData, boolean needCalendarSentByFixup) throws ServiceException {
boolean anySystemMutators = MimeVisitor.anyMutatorsRegistered();
Upload up = FileUploadServlet.fetchUpload(zsc.getAuthtokenAccountId(), attachId, zsc.getAuthToken());
if (up == null) {
throw MailServiceException.NO_SUCH_UPLOAD(attachId);
}
(mimeData.uploads = new ArrayList<Upload>(1)).add(up);
try {
// if we may need to mutate the message, we can't use the "updateHeaders" hack...
if (anySystemMutators || needCalendarSentByFixup) {
MimeMessage mm = new ZMimeMessage(JMSession.getSession(), up.getInputStream());
if (anySystemMutators) {
return mm;
}
OutlookICalendarFixupMimeVisitor.ICalendarModificationCallback callback = new OutlookICalendarFixupMimeVisitor.ICalendarModificationCallback();
MimeVisitor mv = new OutlookICalendarFixupMimeVisitor(getRequestedAccount(zsc), getRequestedMailbox(zsc)).needFixup(true).setCallback(callback);
try {
mv.accept(mm);
} catch (MessagingException e) {
}
if (callback.wouldCauseModification()) {
return mm;
}
}
// ... but in general, for most installs this is safe
return new ZMimeMessage(JMSession.getSession(), up.getInputStream()) {
@Override
protected void updateHeaders() throws MessagingException {
setHeader("MIME-Version", "1.0");
if (getMessageID() == null)
updateMessageID();
}
};
} catch (MessagingException e) {
throw MailServiceException.MESSAGE_PARSE_ERROR(e);
} catch (IOException e) {
throw ServiceException.FAILURE("IOException when parsing upload", e);
}
}
use of com.zimbra.common.zmime.ZMimeMessage in project zm-mailbox by Zimbra.
the class MessageBuilder method create.
public String create() throws MessagingException, ServiceException, IOException {
if (toRecipient == null) {
toRecipient = "user1";
}
if (from == null) {
from = "jspiccoli";
}
if (date == null) {
date = new Date();
}
if (contentType == null) {
contentType = MimeConstants.CT_TEXT_PLAIN;
}
if (body == null) {
body = MessageBuilder.DEFAULT_MESSAGE_BODY;
}
from = TestUtil.addDomainIfNecessary(from);
toRecipient = TestUtil.addDomainIfNecessary(toRecipient);
sender = TestUtil.addDomainIfNecessary(sender);
MimeMessage msg = addMessageIdHeader ? new ZMimeMessage(JMSession.getSession()) : new MimeMessageWithNoId();
msg.setRecipient(RecipientType.TO, new JavaMailInternetAddress(toRecipient));
if (ccRecipient != null) {
ccRecipient = TestUtil.addDomainIfNecessary(ccRecipient);
msg.setRecipient(RecipientType.CC, new JavaMailInternetAddress(ccRecipient));
}
msg.setFrom(new JavaMailInternetAddress(from));
if (sender != null) {
msg.setSender(new JavaMailInternetAddress(sender));
}
msg.setSentDate(date);
msg.setSubject(subject);
if (attachment == null) {
// Need to specify the data handler explicitly because JavaMail
// doesn't know what to do with text/enriched.
msg.setDataHandler(new DataHandler(new ByteArrayDataSource(body.getBytes(), contentType)));
} else {
MimeMultipart multi = new ZMimeMultipart("mixed");
MimeBodyPart body = new ZMimeBodyPart();
body.setDataHandler(new DataHandler(new ByteArrayDataSource(this.body.getBytes(), contentType)));
multi.addBodyPart(body);
MimeBodyPart attachment = new ZMimeBodyPart();
attachment.setContent(this.attachment, attachmentContentType);
attachment.setHeader("Content-Disposition", "attachment; filename=" + attachmentFilename);
multi.addBodyPart(attachment);
msg.setContent(multi);
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
msg.writeTo(out);
return new String(out.toByteArray());
}
Aggregations