Search in sources :

Example 16 with MailSender

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

the class CalItemEmailReminderTask 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_zimbraPrefCalendarReminderEmail);
    if (to == null) {
        ZimbraLog.scheduler.info("Unable to send calendar reminder email since %s is not set", Provisioning.A_zimbraPrefCalendarReminderEmail);
        return;
    }
    mm.setRecipient(javax.mail.Message.RecipientType.TO, new JavaMailInternetAddress(to));
    mm.setSubject(L10nUtil.getMessage(calItem.getType() == MailItem.Type.APPOINTMENT ? L10nUtil.MsgKey.apptReminderEmailSubject : L10nUtil.MsgKey.taskReminderEmailSubject, locale, calItem.getSubject()), MimeConstants.P_CHARSET_UTF8);
    if (invite.getDescriptionHtml() == null) {
        mm.setText(getBody(calItem, invite, false, locale, tz), MimeConstants.P_CHARSET_UTF8);
    } else {
        MimeMultipart mmp = new ZMimeMultipart("alternative");
        mm.setContent(mmp);
        MimeBodyPart textPart = new ZMimeBodyPart();
        textPart.setText(getBody(calItem, invite, false, locale, tz), MimeConstants.P_CHARSET_UTF8);
        mmp.addBodyPart(textPart);
        MimeBodyPart htmlPart = new ZMimeBodyPart();
        htmlPart.setContent(getBody(calItem, invite, true, locale, tz), MimeConstants.CT_TEXT_HTML + "; " + MimeConstants.P_CHARSET + "=" + MimeConstants.P_CHARSET_UTF8);
        mmp.addBodyPart(htmlPart);
    }
    mm.setSentDate(new Date());
    mm.saveChanges();
    MailSender mailSender = calItem.getMailbox().getMailSender();
    mailSender.sendMimeMessage(null, calItem.getMailbox(), mm);
}
Also used : Locale(java.util.Locale) Account(com.zimbra.cs.account.Account) ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) MailSender(com.zimbra.cs.mailbox.MailSender) Date(java.util.Date) TimeZone(java.util.TimeZone) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Example 17 with MailSender

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

the class CalendarMailSender method sendInviteAutoForwardMessage.

public static void sendInviteAutoForwardMessage(final OperationContext octxt, final Mailbox mbox, final ItemId origMsgId, final MimeMessage mm) {
    // 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 sender = getCalendarMailSender(mbox).setSaveToSent(true).setOriginalMessageId(origMsgId).setReplyType(MailSender.MSGTYPE_REPLY).setSendPartial(true);
                // Preserve original From and Sender to avoid confusing the delegate user.
                sender.setRedirectMode(true);
                sender.sendMimeMessage(octxt, mbox, mm);
            } catch (ServiceException e) {
                ZimbraLog.calendar.warn("Ignoring error while sending permission-denied auto reply", e);
            } catch (OutOfMemoryError e) {
                Zimbra.halt("OutOfMemoryError while sending permission-denied auto reply", e);
            }
        }
    };
    Thread senderThread = new Thread(r, "CalendarInviteForwardSender");
    senderThread.setDaemon(true);
    senderThread.start();
}
Also used : ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) MailSender(com.zimbra.cs.mailbox.MailSender)

Example 18 with MailSender

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

the class CalendarMailSender method sendPartial.

public static ItemId sendPartial(OperationContext octxt, Mailbox mbox, MimeMessage mm, List<Upload> uploads, ItemId origMsgId, String replyType, String identityId, com.zimbra.cs.account.DataSource dataSource, boolean replyToSender, boolean asAdmin) throws ServiceException {
    ItemId id = null;
    try {
        if (dataSource == null) {
            MailSender mailSender = getCalendarMailSender(mbox).setSendPartial(true);
            if (asAdmin) {
                mailSender.setSkipHeaderUpdate(true);
                id = mailSender.sendMimeMessage(octxt, mbox, Boolean.FALSE, mm, uploads, origMsgId, replyType, null, replyToSender);
            } else {
                id = mailSender.sendMimeMessage(octxt, mbox, mm, uploads, origMsgId, replyType, identityId, replyToSender);
            }
        } else {
            MailSender mailSender = mbox.getDataSourceMailSender(dataSource, true).setSendPartial(true);
            id = mailSender.sendDataSourceMimeMessage(octxt, mbox, mm, uploads, origMsgId, replyType);
        }
    } catch (MailServiceException e) {
        if (e.getCode().equals(MailServiceException.SEND_PARTIAL_ADDRESS_FAILURE)) {
            ZimbraLog.calendar.info("Unable to send to some addresses: " + e);
        } else {
            throw e;
        }
    }
    return id;
}
Also used : MailSender(com.zimbra.cs.mailbox.MailSender) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) ItemId(com.zimbra.cs.service.util.ItemId)

Example 19 with MailSender

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

the class CalendarMailSender method sendInviteDeniedMessage.

public static void sendInviteDeniedMessage(final OperationContext octxt, Account fromAccount, Account senderAccount, boolean onBehalfOf, boolean allowPrivateAccess, final Mailbox mbox, final ItemId origMsgId, String toEmail, Invite inv) throws ServiceException {
    Address toAddr;
    try {
        toAddr = new JavaMailInternetAddress(toEmail);
    } catch (AddressException e) {
        throw ServiceException.FAILURE("Bad address: " + toEmail, e);
    }
    MsgKey bodyTextKey;
    if (fromAccount instanceof CalendarResource)
        bodyTextKey = MsgKey.calendarResourceDefaultReplyPermissionDenied;
    else
        bodyTextKey = MsgKey.calendarUserReplyPermissionDenied;
    final MimeMessage mm = createCalendarInviteDeniedMessage(fromAccount, senderAccount, onBehalfOf, allowPrivateAccess, toAddr, inv, bodyTextKey);
    // 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, true, mm, null, origMsgId, MailSender.MSGTYPE_REPLY, null, false);
            } catch (ServiceException e) {
                ZimbraLog.calendar.warn("Ignoring error while sending permission-denied auto reply", e);
            } catch (OutOfMemoryError e) {
                Zimbra.halt("OutOfMemoryError while sending permission-denied auto reply", e);
            }
        }
    };
    Thread senderThread = new Thread(r, "CalendarPermDeniedReplySender");
    senderThread.setDaemon(true);
    senderThread.start();
}
Also used : Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) AddressException(javax.mail.internet.AddressException) MsgKey(com.zimbra.common.util.L10nUtil.MsgKey) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) MailSender(com.zimbra.cs.mailbox.MailSender) CalendarResource(com.zimbra.cs.account.CalendarResource)

Example 20 with MailSender

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

the class TestMailSender method testRejectRecipient.

@Test
public void testRejectRecipient() throws Exception {
    String errorMsg = "Sender address rejected: User unknown in relay recipient table";
    String bogusAddress = TestUtil.getAddress("bogus");
    startDummySmtpServer(bogusAddress, errorMsg);
    Server server = Provisioning.getInstance().getLocalServer();
    server.setSmtpPort(TEST_SMTP_PORT);
    String content = TestUtil.getTestMessage(NAME_PREFIX + " testRejectSender", bogusAddress, SENDER_NAME, null);
    MimeMessage msg = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes()));
    Mailbox mbox = TestUtil.getMailbox(SENDER_NAME);
    // Test reject first recipient, get partial send value from LDAP.
    boolean sendFailed = false;
    server.setSmtpSendPartial(false);
    try {
        mbox.getMailSender().sendMimeMessage(null, mbox, msg);
    } catch (MailServiceException e) {
        validateException(e, MailServiceException.SEND_ABORTED_ADDRESS_FAILURE, bogusAddress, errorMsg);
        sendFailed = true;
    }
    Assert.assertTrue(sendFailed);
    // Test reject first recipient, set partial send value explicitly.
    startDummySmtpServer(bogusAddress, errorMsg);
    sendFailed = false;
    server.setSmtpSendPartial(true);
    MailSender sender = mbox.getMailSender().setSendPartial(false);
    try {
        sender.sendMimeMessage(null, mbox, msg);
    } catch (MailServiceException e) {
        validateException(e, MailServiceException.SEND_ABORTED_ADDRESS_FAILURE, bogusAddress, errorMsg);
        sendFailed = true;
    }
    Assert.assertTrue(sendFailed);
    // Test reject second recipient, get partial send value from LDAP.
    startDummySmtpServer(bogusAddress, errorMsg);
    sendFailed = false;
    String validAddress = TestUtil.getAddress(RECIPIENT_NAME);
    InternetAddress[] recipients = new InternetAddress[2];
    recipients[0] = new JavaMailInternetAddress(validAddress);
    recipients[1] = new JavaMailInternetAddress(bogusAddress);
    msg.setRecipients(MimeMessage.RecipientType.TO, recipients);
    server.setSmtpSendPartial(false);
    try {
        mbox.getMailSender().sendMimeMessage(null, mbox, msg);
    } catch (MailServiceException e) {
        validateException(e, MailServiceException.SEND_ABORTED_ADDRESS_FAILURE, bogusAddress, errorMsg);
        sendFailed = true;
    }
    Assert.assertTrue(sendFailed);
    // Test partial send, get value from LDAP.
    startDummySmtpServer(bogusAddress, errorMsg);
    server.setSmtpSendPartial(true);
    sendFailed = false;
    try {
        mbox.getMailSender().sendMimeMessage(null, mbox, msg);
    } catch (MailServiceException e) {
        validateException(e, MailServiceException.SEND_PARTIAL_ADDRESS_FAILURE, bogusAddress, null);
        sendFailed = true;
    }
    Assert.assertTrue(sendFailed);
    // Test partial send, specify value explicitly.
    server.setSmtpSendPartial(false);
    startDummySmtpServer(bogusAddress, errorMsg);
    sendFailed = false;
    sender = mbox.getMailSender().setSendPartial(true);
    try {
        sender.sendMimeMessage(null, mbox, msg);
    } catch (MailServiceException e) {
        // Don't check error message.  JavaMail does not give us the SMTP protocol error in the
        // partial send case.
        validateException(e, MailServiceException.SEND_PARTIAL_ADDRESS_FAILURE, bogusAddress, null);
        sendFailed = true;
    }
    Assert.assertTrue(sendFailed);
}
Also used : JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) InternetAddress(javax.mail.internet.InternetAddress) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) Server(com.zimbra.cs.account.Server) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) FixedMimeMessage(com.zimbra.cs.mime.Mime.FixedMimeMessage) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) MailSender(com.zimbra.cs.mailbox.MailSender) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) Test(org.junit.Test)

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