Search in sources :

Example 6 with MailSender

use of com.zimbra.cs.mailbox.MailSender in project zm-mailbox by Zimbra.

the class SendMsg method doSendMessage.

public static ItemId doSendMessage(OperationContext oc, Mailbox mbox, MimeMessage mm, List<Upload> uploads, ItemId origMsgId, String replyType, String identityId, String dataSourceId, boolean noSaveToSent, boolean needCalendarSentByFixup, boolean isCalendarForward) throws ServiceException {
    boolean isCalendarMessage = false;
    ItemId id;
    OutlookICalendarFixupMimeVisitor mv = null;
    if (needCalendarSentByFixup || isCalendarForward) {
        mv = new OutlookICalendarFixupMimeVisitor(mbox.getAccount(), mbox).needFixup(needCalendarSentByFixup).setIsCalendarForward(isCalendarForward);
        try {
            mv.accept(mm);
        } catch (MessagingException e) {
            throw ServiceException.PARSE_ERROR("Error while fixing up SendMsg for SENT-BY", e);
        }
        isCalendarMessage = mv.isCalendarMessage();
    }
    MailSender sender;
    if (dataSourceId == null) {
        sender = isCalendarMessage ? CalendarMailSender.getCalendarMailSender(mbox) : mbox.getMailSender();
        if (noSaveToSent) {
            id = sender.sendMimeMessage(oc, mbox, false, mm, uploads, origMsgId, replyType, null, false, processor);
        } else {
            id = sender.sendMimeMessage(oc, mbox, mm, uploads, origMsgId, replyType, identityId, false, processor);
        }
    } else {
        com.zimbra.cs.account.DataSource dataSource = mbox.getAccount().getDataSourceById(dataSourceId);
        if (dataSource == null) {
            throw ServiceException.INVALID_REQUEST("No data source with id " + dataSourceId, null);
        }
        if (!dataSource.isSmtpEnabled()) {
            throw ServiceException.INVALID_REQUEST("Data source SMTP is not enabled", null);
        }
        sender = mbox.getDataSourceMailSender(dataSource, isCalendarMessage);
        id = sender.sendDataSourceMimeMessage(oc, mbox, mm, uploads, origMsgId, replyType);
    }
    // Send Calendar Forward Invitation notification if applicable
    try {
        if (isCalendarMessage && isCalendarForward && mv.getOriginalInvite() != null) {
            ZOrganizer org = mv.getOriginalInvite().getOrganizer();
            if (org != null) {
                String orgAddress = org.getAddress();
                Account orgAccount = Provisioning.getInstance().getAccountByName(orgAddress);
                if (orgAccount != null && !orgAccount.getId().equals(mbox.getAccount().getId())) {
                    MimeMessage notifyMimeMsg = CalendarMailSender.createForwardNotifyMessage(mbox.getAccount(), orgAccount, orgAddress, mm.getAllRecipients(), mv.getOriginalInvite());
                    CalendarMailSender.sendPartial(oc, mbox, notifyMimeMsg, null, null, null, null, false, true);
                }
            }
        }
    } catch (Exception e) {
        ZimbraLog.soap.warn("Ignoring error while sending Calendar Invitation Forward Notification", e);
    }
    return id;
}
Also used : Account(com.zimbra.cs.account.Account) MessagingException(javax.mail.MessagingException) ZOrganizer(com.zimbra.cs.mailbox.calendar.ZOrganizer) CalendarMailSender(com.zimbra.cs.mailbox.calendar.CalendarMailSender) MailSender(com.zimbra.cs.mailbox.MailSender) ItemId(com.zimbra.cs.service.util.ItemId) MessagingException(javax.mail.MessagingException) ServiceException(com.zimbra.common.service.ServiceException) IOException(java.io.IOException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage)

Example 7 with MailSender

use of com.zimbra.cs.mailbox.MailSender in project zm-mailbox by Zimbra.

the class FilterUtil method notify.

public static void notify(OperationContext octxt, Mailbox mailbox, ParsedMessage parsedMessage, String emailAddr, String subjectTemplate, String bodyTemplate, int maxBodyBytes, List<String> origHeaders) throws MessagingException, ServiceException {
    MimeMessage mimeMessage = parsedMessage.getMimeMessage();
    if (isMailLoop(mailbox, mimeMessage, new String[] { HEADER_FORWARDED })) {
        String error = String.format("Detected a mail loop for message %s.", Mime.getMessageID(mimeMessage));
        throw ServiceException.FAILURE(error, null);
    }
    Account account = mailbox.getAccount();
    MimeMessage notification = new Mime.FixedMimeMessage(JMSession.getSmtpSession(account));
    // add the forwarded header account names to detect the mail loop between accounts
    for (String headerFwdAccountName : Mime.getHeaders(mimeMessage, HEADER_FORWARDED)) {
        notification.addHeader(HEADER_FORWARDED, headerFwdAccountName);
    }
    notification.addHeader(HEADER_FORWARDED, account.getName());
    MailSender mailSender = mailbox.getMailSender().setSaveToSent(false);
    //Map<String, String> vars = getVarsMap(mailbox, parsedMessage, mimeMessage);
    if (origHeaders == null || origHeaders.isEmpty()) {
        // no headers need to be copied from the original message
        notification.setRecipient(javax.mail.Message.RecipientType.TO, new JavaMailInternetAddress(emailAddr));
        notification.setSentDate(new Date());
        if (!StringUtil.isNullOrEmpty(subjectTemplate)) {
            notification.setSubject(subjectTemplate, getCharset(account, subjectTemplate));
        }
    } else {
        if (origHeaders.size() == 1 && "*".equals(origHeaders.get(0))) {
            // all headers need to be copied from the original message
            Enumeration enumeration = mimeMessage.getAllHeaders();
            while (enumeration.hasMoreElements()) {
                Header header = (Header) enumeration.nextElement();
                if (StringUtil.equal(header.getName(), HEADER_FORWARDED)) {
                    continue;
                }
                if (StringUtil.equal(header.getName(), HEADER_CONTENT_TYPE) || StringUtil.equal(header.getName(), HEADER_CONTENT_DISPOSITION)) {
                    // Zimbra Mime parser will add the correct Content Type if absent
                    continue;
                }
                notification.addHeader(header.getName(), header.getValue());
            }
        } else {
            // some headers need to be copied from the original message
            Set<String> headersToCopy = Sets.newHashSet(origHeaders);
            boolean copySubject = false;
            for (String header : headersToCopy) {
                if ("Subject".equalsIgnoreCase(header)) {
                    copySubject = true;
                }
                if (StringUtil.equal(header, HEADER_FORWARDED)) {
                    continue;
                }
                String[] hdrVals = mimeMessage.getHeader(header);
                if (hdrVals == null) {
                    continue;
                }
                for (String hdrVal : hdrVals) {
                    notification.addHeader(header, hdrVal);
                }
            }
            if (!copySubject && !StringUtil.isNullOrEmpty(subjectTemplate)) {
                notification.setSubject(subjectTemplate, getCharset(account, subjectTemplate));
            }
        }
        mailSender.setRedirectMode(true);
        mailSender.setRecipients(emailAddr);
    }
    String body = StringUtil.truncateIfRequired(bodyTemplate, maxBodyBytes);
    notification.setText(body, getCharset(account, body));
    notification.saveChanges();
    if (isDeliveryStatusNotification(mimeMessage)) {
        mailSender.setEnvelopeFrom("<>");
    } else {
        mailSender.setEnvelopeFrom(account.getName());
    }
    mailSender.setDsnNotifyOptions(MailSender.DsnNotifyOption.NEVER);
    mailSender.sendMimeMessage(octxt, mailbox, notification);
}
Also used : Account(com.zimbra.cs.account.Account) Enumeration(java.util.Enumeration) ZInternetHeader(com.zimbra.common.zmime.ZInternetHeader) Header(javax.mail.Header) MimeMessage(javax.mail.internet.MimeMessage) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) MailSender(com.zimbra.cs.mailbox.MailSender) Date(java.util.Date)

Example 8 with MailSender

use of com.zimbra.cs.mailbox.MailSender in project zm-mailbox by Zimbra.

the class CalItemSmsReminderTask method sendReminder.

@Override
protected void sendReminder(CalendarItem calItem, Invite invite) throws Exception {
    Account account = calItem.getAccount();
    Locale locale = account.getLocale();
    TimeZone tz = Util.getAccountTimeZone(account);
    MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSmtpSession(account));
    String to = account.getAttr(Provisioning.A_zimbraCalendarReminderDeviceEmail);
    if (to == null) {
        ZimbraLog.scheduler.info("Unable to send calendar reminder sms since %s is not set", Provisioning.A_zimbraCalendarReminderDeviceEmail);
        return;
    }
    mm.setRecipient(javax.mail.Message.RecipientType.TO, new JavaMailInternetAddress(to));
    mm.setText(getText(calItem, invite, locale, tz), MimeConstants.P_CHARSET_UTF8);
    mm.saveChanges();
    MailSender mailSender = calItem.getMailbox().getMailSender();
    mailSender.setSaveToSent(false);
    mailSender.sendMimeMessage(null, calItem.getMailbox(), mm);
}
Also used : Locale(java.util.Locale) Account(com.zimbra.cs.account.Account) TimeZone(java.util.TimeZone) MimeMessage(javax.mail.internet.MimeMessage) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) MailSender(com.zimbra.cs.mailbox.MailSender)

Example 9 with MailSender

use of com.zimbra.cs.mailbox.MailSender in project zm-mailbox by Zimbra.

the class CalendarMailSender method getCalendarMailSender.

/** Returns a {@link MailSender} object that can be used to send calendar messages.  Calendar emails
     * must always allow send-on-behalf-of because an attendee may forward any received invite to any
     * other user using send-on-behalf-of mechanism.  Microsoft Outlook client works this way and ZCS
     * does the same for compatibility.
     */
public static MailSender getCalendarMailSender(Mailbox mbox) throws ServiceException {
    MailSender sender = mbox.getMailSender();
    sender.setCalendarMode(true);
    return sender;
}
Also used : MailSender(com.zimbra.cs.mailbox.MailSender)

Example 10 with MailSender

use of com.zimbra.cs.mailbox.MailSender in project zm-mailbox by Zimbra.

the class CalendarMailSender method sendResourceAutoReply.

public static void sendResourceAutoReply(final OperationContext octxt, final Mailbox mbox, final boolean saveToSent, Verb verb, boolean partialAccept, String additionalMsgBody, CalendarItem calItem, Invite inv, Invite[] replies, MimeMessage mmInv) throws ServiceException {
    Identity iden = getTargetedIdentity(mbox.getAccount(), inv);
    final MimeMessage mm = createResourceAutoReply(octxt, iden.getId(), iden.getId(), mbox, verb, partialAccept, additionalMsgBody, calItem, inv, replies, mmInv, true);
    final String replyType = MailSender.MSGTYPE_REPLY;
    final int invId = inv.getMailItemId();
    // Send in a separate thread to avoid nested transaction error when saving a copy to Sent folder.
    Runnable r = new Runnable() {

        @Override
        public void run() {
            try {
                MailSender mailSender = getCalendarMailSender(mbox).setSendPartial(true);
                mailSender.sendMimeMessage(octxt, mbox, saveToSent, mm, null, new ItemId(mbox, invId), replyType, null, false);
            } catch (ServiceException e) {
                ZimbraLog.calendar.warn("Ignoring error while sending auto accept/decline reply", e);
            } catch (OutOfMemoryError e) {
                Zimbra.halt("OutOfMemoryError while sending calendar resource auto accept/decline reply", e);
            }
        }
    };
    Thread senderThread = new Thread(r, "CalendarAutoAcceptDeclineReplySender");
    senderThread.setDaemon(true);
    senderThread.start();
}
Also used : ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) MailSender(com.zimbra.cs.mailbox.MailSender) Identity(com.zimbra.cs.account.Identity) ItemId(com.zimbra.cs.service.util.ItemId)

Aggregations

MailSender (com.zimbra.cs.mailbox.MailSender)20 MimeMessage (javax.mail.internet.MimeMessage)14 Account (com.zimbra.cs.account.Account)11 JavaMailInternetAddress (com.zimbra.common.mime.shim.JavaMailInternetAddress)10 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)6 Mailbox (com.zimbra.cs.mailbox.Mailbox)6 MessagingException (javax.mail.MessagingException)6 ServiceException (com.zimbra.common.service.ServiceException)5 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)5 ItemId (com.zimbra.cs.service.util.ItemId)5 Date (java.util.Date)5 IOException (java.io.IOException)4 InternetAddress (javax.mail.internet.InternetAddress)4 Element (com.zimbra.common.soap.Element)3 OperationContext (com.zimbra.cs.mailbox.OperationContext)3 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)3 Locale (java.util.Locale)3 ZMimeBodyPart (com.zimbra.common.zmime.ZMimeBodyPart)2 ZMimeMultipart (com.zimbra.common.zmime.ZMimeMultipart)2 Provisioning (com.zimbra.cs.account.Provisioning)2