use of javax.mail.Address in project zm-mailbox by Zimbra.
the class ScheduleOutbox method handleEventRequest.
private void handleEventRequest(DavContext ctxt, ZCalendar.ZVCalendar cal, ZComponent req, DelegationInfo delegationInfo, String rcpt, Element resp) throws ServiceException, DavException {
if (!DavResource.isSchedulingEnabled()) {
resp.addElement(DavElements.E_RECIPIENT).addElement(DavElements.E_HREF).setText(rcpt);
resp.addElement(DavElements.E_REQUEST_STATUS).setText("5.3;No scheduling for the user");
return;
}
ArrayList<Address> recipients = new java.util.ArrayList<Address>();
InternetAddress from, sender, to;
Account target = null;
try {
sender = new JavaMailInternetAddress(delegationInfo.getOriginatorEmail());
Provisioning prov = Provisioning.getInstance();
if (ctxt.getActingAsDelegateFor() != null) {
target = prov.getAccountByName(ctxt.getActingAsDelegateFor());
}
if (target != null) {
from = AccountUtil.getFriendlyEmailAddress(target);
} else {
if (delegationInfo.getOwnerEmail() != null) {
from = new JavaMailInternetAddress(delegationInfo.getOwnerEmail());
} else {
target = getMailbox(ctxt).getAccount();
if (AccountUtil.addressMatchesAccount(target, delegationInfo.getOriginatorEmail())) {
// Make sure we don't use two different aliases for From and Sender.
// This is a concern with Apple iCal, which picks a random alias as originator.
from = sender;
} else {
from = AccountUtil.getFriendlyEmailAddress(target);
}
}
}
if (sender.getAddress() != null && sender.getAddress().equalsIgnoreCase(from.getAddress())) {
sender = null;
}
to = new JavaMailInternetAddress(CalDavUtils.stripMailto(rcpt));
recipients.add(to);
} catch (AddressException e) {
resp.addElement(DavElements.E_RECIPIENT).addElement(DavElements.E_HREF).setText(rcpt);
resp.addElement(DavElements.E_REQUEST_STATUS).setText("3.7;" + rcpt);
return;
}
String status = req.getPropVal(ICalTok.STATUS, "");
String method = cal.getPropVal(ICalTok.METHOD, "REQUEST");
String subject = "";
if (method.equals("REQUEST")) {
ZProperty organizerProp = req.getProperty(ICalTok.ORGANIZER);
if (organizerProp != null) {
String organizerStr = this.getAddressFromPrincipalURL(new ZOrganizer(organizerProp).getAddress());
if (!AccountUtil.addressMatchesAccount(getMailbox(ctxt).getAccount(), organizerStr)) {
ZimbraLog.dav.debug("scheduling appointment on behalf of %s", organizerStr);
}
}
} else if (method.equals("REPLY")) {
ZProperty attendeeProp = req.getProperty(ICalTok.ATTENDEE);
if (attendeeProp == null)
throw new DavException("missing property ATTENDEE", HttpServletResponse.SC_BAD_REQUEST);
ZAttendee attendee = new ZAttendee(attendeeProp);
String partStat = attendee.getPartStat();
if (partStat.equals(IcalXmlStrMap.PARTSTAT_ACCEPTED)) {
subject = "Accept: ";
} else if (partStat.equals(IcalXmlStrMap.PARTSTAT_TENTATIVE)) {
subject = "Tentative: ";
} else if (partStat.equals(IcalXmlStrMap.PARTSTAT_DECLINED)) {
subject = "Decline: ";
}
}
if (status.equals("CANCELLED"))
subject = "Cancelled: ";
subject += req.getPropVal(ICalTok.SUMMARY, "");
String uid = req.getPropVal(ICalTok.UID, null);
if (uid == null) {
resp.addElement(DavElements.E_RECIPIENT).addElement(DavElements.E_HREF).setText(rcpt);
resp.addElement(DavElements.E_REQUEST_STATUS).setText("3.1;UID");
return;
}
try {
List<Invite> components = Invite.createFromCalendar(ctxt.getAuthAccount(), null, cal, false);
FriendlyCalendaringDescription friendlyDesc = new FriendlyCalendaringDescription(components, ctxt.getAuthAccount());
String desc = friendlyDesc.getAsPlainText();
String descHtml = req.getDescriptionHtml();
if ((descHtml == null) || (descHtml.length() == 0))
descHtml = friendlyDesc.getAsHtml();
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(ctxt.getAuthAccount());
MimeMessage mm = CalendarMailSender.createCalendarMessage(target, from, sender, recipients, subject, desc, descHtml, uid, cal);
mbox.getMailSender().setSendPartial(true).sendMimeMessage(ctxt.getOperationContext(), mbox, true, mm, null, null, null, null, false);
} catch (ServiceException e) {
resp.addElement(DavElements.E_RECIPIENT).addElement(DavElements.E_HREF).setText(rcpt);
resp.addElement(DavElements.E_REQUEST_STATUS).setText("5.1");
return;
}
resp.addElement(DavElements.E_RECIPIENT).addElement(DavElements.E_HREF).setText(rcpt);
resp.addElement(DavElements.E_REQUEST_STATUS).setText("2.0;Success");
}
use of javax.mail.Address in project zm-mailbox by Zimbra.
the class Mime method removeRecipients.
/**
* Remove all email addresses in rcpts from To/Cc/Bcc headers of a
* MimeMessage.
* @param mm
* @param rcpts
* @throws MessagingException
*/
public static void removeRecipients(MimeMessage mm, String[] rcpts) throws MessagingException {
for (RecipientType rcptType : sRcptTypes) {
Address[] addrs = mm.getRecipients(rcptType);
if (addrs == null)
continue;
ArrayList<InternetAddress> list = new ArrayList<InternetAddress>(addrs.length);
for (int j = 0; j < addrs.length; j++) {
InternetAddress inetAddr = (InternetAddress) addrs[j];
String addr = inetAddr.getAddress();
boolean match = false;
for (int k = 0; k < rcpts.length; k++) if (addr.equalsIgnoreCase(rcpts[k]))
match = true;
if (!match)
list.add(inetAddr);
}
if (list.size() < addrs.length) {
InternetAddress[] newRcpts = new InternetAddress[list.size()];
list.toArray(newRcpts);
mm.setRecipients(rcptType, newRcpts);
}
}
}
use of javax.mail.Address in project zm-mailbox by Zimbra.
the class SmtpTransport method notify.
private void notify(Exception ex, Message msg, Address[] addrs) throws SendFailedException {
Set<String> validRcpts = connection.getValidRecipients();
Set<String> invalidRcpts = connection.getInvalidRecipients();
List<Address> validAddrs = new ArrayList<Address>(validRcpts.size());
List<Address> invalidAddrs = new ArrayList<Address>(invalidRcpts.size());
for (Address addr : addrs) {
if (addr instanceof InternetAddress) {
InternetAddress iaddr = (InternetAddress) addr;
if (validRcpts.contains(iaddr.getAddress())) {
validAddrs.add(iaddr);
} else if (invalidRcpts.contains(iaddr.getAddress())) {
invalidAddrs.add(iaddr);
}
}
}
Address[] validSent = Iterables.toArray(validAddrs, Address.class);
Address[] validUnsent = new Address[0];
Address[] invalid = Iterables.toArray(invalidAddrs, Address.class);
int notify = ex != null ? TransportEvent.MESSAGE_NOT_DELIVERED : invalid.length > 0 ? TransportEvent.MESSAGE_PARTIALLY_DELIVERED : TransportEvent.MESSAGE_DELIVERED;
notifyTransportListeners(notify, validSent, validUnsent, invalid, msg);
switch(notify) {
case TransportEvent.MESSAGE_NOT_DELIVERED:
throw new SendFailedException("MESSAGE_NOT_DELIVERED", ex, validSent, validUnsent, invalid);
case TransportEvent.MESSAGE_PARTIALLY_DELIVERED:
throw new SendFailedException("MESSAGE_PARTIALLY_DELIVERED", ex, validSent, validUnsent, invalid);
}
}
use of javax.mail.Address in project zm-mailbox by Zimbra.
the class CreateCalendarItemException 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);
ItemIdFormatter ifmt = new ItemIdFormatter(zsc);
// proxy handling
Element msgElem = request.getElement(MailConstants.E_MSG);
String folderStr = msgElem.getAttribute(MailConstants.A_FOLDER, null);
ItemId iid = new ItemId(request.getAttribute(MailConstants.A_ID), zsc);
if (!iid.belongsTo(acct)) {
// Proxy it.
if (folderStr != null) {
// make sure that the folder ID is fully qualified
ItemId folderFQ = new ItemId(folderStr, zsc);
msgElem.addAttribute(MailConstants.A_FOLDER, folderFQ.toString());
}
return proxyRequest(request, context, iid.getAccountId());
}
// Check if moving to a different mailbox.
boolean isInterMboxMove = false;
ItemId iidFolder = null;
if (folderStr != null) {
iidFolder = new ItemId(folderStr, zsc);
isInterMboxMove = !iidFolder.belongsTo(mbox);
}
int compNum = (int) request.getAttributeLong(MailConstants.E_INVITE_COMPONENT);
MailSendQueue sendQueue = new MailSendQueue();
Element response = getResponseElement(zsc);
mbox.lock.lock();
try {
CalendarItem calItem = mbox.getCalendarItemById(octxt, iid.getId());
if (calItem == null)
throw MailServiceException.NO_SUCH_CALITEM(iid.getId(), " for CreateCalendarItemExceptionRequest(" + iid + "," + compNum + ")");
// Reject the request if calendar item is under trash or is being moved to trash.
if (calItem.inTrash())
throw ServiceException.INVALID_REQUEST("cannot modify a calendar item under trash", null);
if (!isInterMboxMove && iidFolder != null) {
if (iidFolder.getId() != calItem.getFolderId()) {
Folder destFolder = mbox.getFolderById(octxt, iidFolder.getId());
if (destFolder.inTrash())
throw ServiceException.INVALID_REQUEST("cannot combine with a move to trash", null);
}
}
// Conflict detection. Do it only if requested by client. (for backward compat)
int modSeq = (int) request.getAttributeLong(MailConstants.A_MODIFIED_SEQUENCE, 0);
int revision = (int) request.getAttributeLong(MailConstants.A_REVISION, 0);
if (modSeq != 0 && revision != 0 && (modSeq < calItem.getModifiedSequence() || revision < calItem.getSavedSequence()))
throw MailServiceException.INVITE_OUT_OF_DATE(iid.toString());
Invite inv = calItem.getInvite(iid.getSubpartId(), compNum);
if (inv == null)
throw MailServiceException.INVITE_OUT_OF_DATE(iid.toString());
if (inv.hasRecurId())
throw MailServiceException.INVITE_OUT_OF_DATE("Invite id=" + ifmt.formatItemId(iid) + " comp=" + compNum + " is not the default invite");
if (!calItem.isRecurring())
throw ServiceException.INVALID_REQUEST("CalendarItem " + calItem.getId() + " is not a recurring calendar item", null);
CreateCalendarItemExceptionInviteParser parser = new CreateCalendarItemExceptionInviteParser(calItem.getUid(), inv, sendQueue);
CalSendData dat = handleMsgElement(zsc, octxt, msgElem, acct, mbox, parser);
dat.mDontNotifyAttendees = isInterMboxMove;
int folderId = calItem.getFolderId();
if (!isInterMboxMove && iidFolder != null)
folderId = iidFolder.getId();
// trace logging
if (!dat.mInvite.hasRecurId())
ZimbraLog.calendar.info("<CreateCalendarItemException> id=%d, folderId=%d, subject=\"%s\", UID=%s", iid.getId(), folderId, dat.mInvite.isPublic() ? dat.mInvite.getName() : "(private)", dat.mInvite.getUid());
else
ZimbraLog.calendar.info("<CreateCalendarItemException> id=%d, folderId=%d, subject=\"%s\", UID=%s, recurId=%s", iid.getId(), folderId, dat.mInvite.isPublic() ? dat.mInvite.getName() : "(private)", dat.mInvite.getUid(), dat.mInvite.getRecurId().getDtZ());
boolean hasRecipients;
try {
Address[] rcpts = dat.mMm.getAllRecipients();
hasRecipients = rcpts != null && rcpts.length > 0;
} catch (MessagingException e) {
throw ServiceException.FAILURE("Checking recipients of outgoing msg ", e);
}
// If we are sending this to other people, then we MUST be the organizer!
if (!dat.mInvite.isOrganizer() && hasRecipients)
throw MailServiceException.MUST_BE_ORGANIZER("CreateCalendarItemException");
if (!dat.mInvite.isOrganizer()) {
// neverSent is always false for attendee users.
dat.mInvite.setNeverSent(false);
} else if (!dat.mInvite.hasOtherAttendees()) {
// neverSent is always false for appointments without attendees.
dat.mInvite.setNeverSent(false);
} else if (hasRecipients) {
// neverSent is set to false when attendees are notified.
dat.mInvite.setNeverSent(false);
} else {
// This is the case of organizer saving an invite with attendees, but without sending the notification.
// Set neverSent to false, but only if it isn't already set to true on the series.
// !series.isNeverSent() ? false : true ==> series.isNeverSent()
dat.mInvite.setNeverSent(inv.isNeverSent());
}
boolean forceSend = request.getAttributeBool(MailConstants.A_CAL_FORCESEND, true);
sendCalendarMessage(zsc, octxt, folderId, acct, mbox, dat, response, true, forceSend, sendQueue);
boolean echo = request.getAttributeBool(MailConstants.A_CAL_ECHO, false);
if (echo && dat.mAddInvData != null) {
int maxSize = (int) request.getAttributeLong(MailConstants.A_MAX_INLINED_LENGTH, 0);
boolean wantHTML = request.getAttributeBool(MailConstants.A_WANT_HTML, false);
boolean neuter = request.getAttributeBool(MailConstants.A_NEUTER, true);
echoAddedInvite(response, ifmt, octxt, mbox, dat.mAddInvData, maxSize, wantHTML, neuter);
}
} finally {
mbox.lock.release();
sendQueue.send();
}
// Inter-mailbox move if necessary.
if (isInterMboxMove) {
CalendarItem calItem = mbox.getCalendarItemById(octxt, iid.getId());
List<Integer> ids = new ArrayList<Integer>(1);
ids.add(calItem.getId());
ItemActionHelper.MOVE(octxt, mbox, zsc.getResponseProtocol(), ids, calItem.getType(), null, iidFolder);
}
return response;
}
use of javax.mail.Address in project zm-mailbox by Zimbra.
the class CreateCalendarItem method handle.
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);
// <M>
Element msgElem = request.getElement(MailConstants.E_MSG);
CreateCalendarItemInviteParser parser = new CreateCalendarItemInviteParser();
CalSendData dat = handleMsgElement(zsc, octxt, msgElem, acct, mbox, parser);
int defaultFolder = dat.mInvite.isTodo() ? Mailbox.ID_FOLDER_TASKS : Mailbox.ID_FOLDER_CALENDAR;
String defaultFolderStr = Integer.toString(defaultFolder);
String folderIdStr = msgElem.getAttribute(MailConstants.A_FOLDER, defaultFolderStr);
ItemId iidFolder = new ItemId(folderIdStr, zsc);
// Don't allow creating in Trash folder/subfolder. We don't want to invite attendees to an appointment in trash.
Folder folder = mbox.getFolderById(octxt, iidFolder.getId());
if (folder.inTrash())
throw ServiceException.INVALID_REQUEST("cannot create a calendar item under trash", null);
// trace logging
if (!dat.mInvite.hasRecurId())
ZimbraLog.calendar.info("<CreateCalendarItem> folderId=%d, subject=\"%s\", UID=%s", iidFolder.getId(), dat.mInvite.isPublic() ? dat.mInvite.getName() : "(private)", dat.mInvite.getUid());
else
ZimbraLog.calendar.info("<CreateCalendarItem> folderId=%d, subject=\"%s\", UID=%s, recurId=%s", iidFolder.getId(), dat.mInvite.isPublic() ? dat.mInvite.getName() : "(private)", dat.mInvite.getUid(), dat.mInvite.getRecurId().getDtZ());
Element response = getResponseElement(zsc);
boolean hasRecipients;
try {
Address[] rcpts = dat.mMm.getAllRecipients();
hasRecipients = rcpts != null && rcpts.length > 0;
} catch (MessagingException e) {
throw ServiceException.FAILURE("Checking recipients of outgoing msg ", e);
}
// If we are sending this to other people, then we MUST be the organizer!
if (!dat.mInvite.isOrganizer() && hasRecipients)
throw MailServiceException.MUST_BE_ORGANIZER("CreateCalendarItem");
if (!dat.mInvite.isOrganizer()) {
// neverSent is always false for attendee users.
dat.mInvite.setNeverSent(false);
} else if (!dat.mInvite.hasOtherAttendees()) {
// neverSent is always false for appointments without attendees.
dat.mInvite.setNeverSent(false);
} else if (hasRecipients) {
// neverSent is set to false when attendees are notified.
dat.mInvite.setNeverSent(false);
} else {
// This is the case of organizer saving an invite with attendees, but without sending the notification.
dat.mInvite.setNeverSent(true);
}
boolean forceSend = request.getAttributeBool(MailConstants.A_CAL_FORCESEND, true);
MailSendQueue sendQueue = new MailSendQueue();
try {
sendCalendarMessage(zsc, octxt, iidFolder.getId(), acct, mbox, dat, response, true, forceSend, sendQueue);
} finally {
sendQueue.send();
}
boolean echo = request.getAttributeBool(MailConstants.A_CAL_ECHO, false);
if (echo && dat.mAddInvData != null) {
ItemIdFormatter ifmt = new ItemIdFormatter(zsc);
int maxSize = (int) request.getAttributeLong(MailConstants.A_MAX_INLINED_LENGTH, 0);
boolean wantHTML = request.getAttributeBool(MailConstants.A_WANT_HTML, false);
boolean neuter = request.getAttributeBool(MailConstants.A_NEUTER, true);
echoAddedInvite(response, ifmt, octxt, mbox, dat.mAddInvData, maxSize, wantHTML, neuter);
}
return response;
}
Aggregations