Search in sources :

Example 11 with MailSender

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

the class FilterUtil method reply.

public static void reply(OperationContext octxt, Mailbox mailbox, ParsedMessage parsedMessage, String bodyTemplate) 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);
    }
    if (isDeliveryStatusNotification(mimeMessage)) {
        ZimbraLog.filter.debug("Not auto-replying to a DSN message");
        return;
    }
    Account account = mailbox.getAccount();
    MimeMessage replyMsg = 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)) {
        replyMsg.addHeader(HEADER_FORWARDED, headerFwdAccountName);
    }
    replyMsg.addHeader(HEADER_FORWARDED, account.getName());
    String to = mimeMessage.getHeader("Reply-To", null);
    if (StringUtil.isNullOrEmpty(to))
        to = Mime.getSender(mimeMessage);
    if (StringUtil.isNullOrEmpty(to))
        throw new MessagingException("Can't locate the address to reply to");
    replyMsg.setRecipient(javax.mail.Message.RecipientType.TO, new JavaMailInternetAddress(to));
    String subject = mimeMessage.getSubject();
    if (subject == null) {
        subject = "";
    }
    String replySubjectPrefix = L10nUtil.getMessage(L10nUtil.MsgKey.replySubjectPrefix, account.getLocale());
    if (!subject.toLowerCase().startsWith(replySubjectPrefix.toLowerCase())) {
        subject = replySubjectPrefix + " " + subject;
    }
    replyMsg.setSubject(subject, getCharset(account, subject));
    //getVarsMap() result is now being passed to replaceVariables(), so following call is redundant
    //Map<String, String> vars = getVarsMap(mailbox, parsedMessage, mimeMessage);
    replyMsg.setText(bodyTemplate, getCharset(account, bodyTemplate));
    String origMsgId = mimeMessage.getMessageID();
    if (!StringUtil.isNullOrEmpty(origMsgId))
        replyMsg.setHeader("In-Reply-To", origMsgId);
    replyMsg.setSentDate(new Date());
    replyMsg.saveChanges();
    MailSender mailSender = mailbox.getMailSender();
    mailSender.setReplyType(MailSender.MSGTYPE_REPLY);
    mailSender.setDsnNotifyOptions(MailSender.DsnNotifyOption.NEVER);
    mailSender.sendMimeMessage(octxt, mailbox, replyMsg);
}
Also used : Account(com.zimbra.cs.account.Account) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) MailSender(com.zimbra.cs.mailbox.MailSender) Date(java.util.Date)

Example 12 with MailSender

use of com.zimbra.cs.mailbox.MailSender 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;
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Account(com.zimbra.cs.account.Account) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MessagingException(javax.mail.MessagingException) InputStream(java.io.InputStream) Element(com.zimbra.common.soap.Element) Upload(com.zimbra.cs.service.FileUploadServlet.Upload) MailSender(com.zimbra.cs.mailbox.MailSender) IOException(java.io.IOException) ItemId(com.zimbra.cs.service.util.ItemId) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext)

Example 13 with MailSender

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

the class CounterCalendarItem 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);
    Invite oldInv = null;
    // proxy handling
    String idStr = request.getAttribute(MailConstants.A_ID, null);
    if (idStr != null) {
        ItemId iid = new ItemId(idStr, zsc);
        if (!iid.belongsTo(acct))
            return proxyRequest(request, context, iid.getAccountId());
        CalendarItem calItem = mbox.getCalendarItemById(octxt, iid.getId());
        if (calItem == null) {
            throw MailServiceException.NO_SUCH_CALITEM(iid.toString(), "Could not find calendar item");
        }
        // 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());
        int compNum = (int) request.getAttributeLong(MailConstants.A_CAL_COMP, 0);
        if (compNum != 0) {
            oldInv = calItem.getInvite(iid.getSubpartId(), compNum);
            if (oldInv == null)
                throw MailServiceException.INVITE_OUT_OF_DATE(iid.toString());
        }
    }
    Element msgElem = request.getElement(MailConstants.E_MSG);
    InviteParser parser = new InviteParser(oldInv);
    CalSendData dat = handleMsgElement(zsc, octxt, msgElem, acct, mbox, parser);
    MailSender mailSender = mbox.getMailSender();
    mailSender.sendMimeMessage(octxt, mbox, dat.mMm);
    Element response = getResponseElement(zsc);
    return response;
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Account(com.zimbra.cs.account.Account) Element(com.zimbra.common.soap.Element) MailSender(com.zimbra.cs.mailbox.MailSender) ItemId(com.zimbra.cs.service.util.ItemId) CalendarItem(com.zimbra.cs.mailbox.CalendarItem) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) Invite(com.zimbra.cs.mailbox.calendar.Invite)

Example 14 with MailSender

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

the class DeclineCounterCalendarItem 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);
    InviteParser parser = new InviteParser();
    CalSendData dat = handleMsgElement(zsc, octxt, msgElem, acct, mbox, parser);
    MailSender mailSender = mbox.getMailSender();
    mailSender.sendMimeMessage(octxt, mbox, dat.mMm);
    Element response = getResponseElement(zsc);
    return response;
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Account(com.zimbra.cs.account.Account) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) Element(com.zimbra.common.soap.Element) MailSender(com.zimbra.cs.mailbox.MailSender)

Example 15 with MailSender

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

the class SendVerificationCode method sendVerificationCode.

static void sendVerificationCode(String emailAddr, String code, Mailbox mbox) throws MessagingException, ServiceException {
    MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSmtpSession(mbox.getAccount()));
    mm.setRecipient(javax.mail.Message.RecipientType.TO, new JavaMailInternetAddress(emailAddr));
    mm.setText(L10nUtil.getMessage(L10nUtil.MsgKey.deviceSendVerificationCodeText, mbox.getAccount().getLocale(), code), MimeConstants.P_CHARSET_UTF8);
    mm.saveChanges();
    MailSender mailSender = mbox.getMailSender();
    mailSender.setSaveToSent(false);
    mailSender.sendMimeMessage(null, mbox, mm);
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) MailSender(com.zimbra.cs.mailbox.MailSender)

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