Search in sources :

Example 1 with MailSender

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

the class SendShareNotificationTest method init.

@BeforeClass
public static void init() throws Exception {
    MailboxTestUtil.initServer();
    Provisioning prov = Provisioning.getInstance();
    Map<String, Object> attrs = Maps.newHashMap();
    prov.createDomain("zimbra.com", attrs);
    attrs = Maps.newHashMap();
    prov.createAccount("test@zimbra.com", "secret", attrs);
    attrs = Maps.newHashMap();
    attrs.put(Provisioning.A_zimbraId, UUID.randomUUID().toString());
    prov.createAccount("test2@zimbra.com", "secret", attrs);
    attrs = Maps.newHashMap();
    attrs.put(Provisioning.A_zimbraId, UUID.randomUUID().toString());
    prov.createAccount("test3@zimbra.com", "secret", attrs);
    // this MailboxManager does everything except actually send mail
    MailboxManager.setInstance(new MailboxManager() {

        @Override
        protected Mailbox instantiateMailbox(MailboxData data) {
            return new Mailbox(data) {

                @Override
                public MailSender getMailSender() {
                    return new MailSender() {

                        @Override
                        protected Collection<Address> sendMessage(Mailbox mbox, MimeMessage mm, Collection<RollbackData> rollbacks) throws SafeMessagingException, IOException {
                            try {
                                return Arrays.asList(getRecipients(mm));
                            } catch (Exception e) {
                                return Collections.emptyList();
                            }
                        }
                    };
                }
            };
        }
    });
    L10nUtil.setMsgClassLoader("conf/msgs");
}
Also used : MailboxData(com.zimbra.cs.mailbox.Mailbox.MailboxData) MailSender(com.zimbra.cs.mailbox.MailSender) IOException(java.io.IOException) Provisioning(com.zimbra.cs.account.Provisioning) MessagingException(javax.mail.MessagingException) ServiceException(com.zimbra.common.service.ServiceException) IOException(java.io.IOException) MailboxManager(com.zimbra.cs.mailbox.MailboxManager) Mailbox(com.zimbra.cs.mailbox.Mailbox) MimeMessage(javax.mail.internet.MimeMessage) Collection(java.util.Collection) BeforeClass(org.junit.BeforeClass)

Example 2 with MailSender

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

the class GetMsgTest method setUp.

/**
     * @throws java.lang.Exception
     */
@Before
public void setUp() throws Exception {
    MailboxTestUtil.initServer();
    MailboxTestUtil.clearData();
    Provisioning prov = Provisioning.getInstance();
    Map<String, Object> attrs = Maps.newHashMap();
    prov.createDomain("zimbra.com", attrs);
    attrs = Maps.newHashMap();
    attrs.put(Provisioning.A_zimbraId, UUID.randomUUID().toString());
    prov.createAccount("test@zimbra.com", "secret", attrs);
    attrs = Maps.newHashMap();
    attrs.put(Provisioning.A_zimbraId, UUID.randomUUID().toString());
    prov.createAccount("test2@zimbra.com", "secret", attrs);
    // this MailboxManager does everything except actually send mail
    MailboxManager.setInstance(new MailboxManager() {

        @Override
        protected Mailbox instantiateMailbox(MailboxData data) {
            return new Mailbox(data) {

                @Override
                public MailSender getMailSender() {
                    return new MailSender() {

                        @Override
                        protected Collection<Address> sendMessage(Mailbox mbox, MimeMessage mm, Collection<RollbackData> rollbacks) {
                            try {
                                return Arrays.asList(getRecipients(mm));
                            } catch (Exception e) {
                                return Collections.emptyList();
                            }
                        }
                    };
                }
            };
        }
    });
}
Also used : MailboxData(com.zimbra.cs.mailbox.Mailbox.MailboxData) MailSender(com.zimbra.cs.mailbox.MailSender) Provisioning(com.zimbra.cs.account.Provisioning) MailboxManager(com.zimbra.cs.mailbox.MailboxManager) Mailbox(com.zimbra.cs.mailbox.Mailbox) MimeMessage(javax.mail.internet.MimeMessage) Collection(java.util.Collection) Before(org.junit.Before)

Example 3 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 4 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 5 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)

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