Search in sources :

Example 21 with JavaMailInternetAddress

use of com.zimbra.common.mime.shim.JavaMailInternetAddress in project zm-mailbox by Zimbra.

the class ComputeAggregateQuotaUsage method sendWarnMsg.

private void sendWarnMsg(final Domain domain, ExecutorService executor) {
    final String[] recipients = domain.getDomainAggregateQuotaWarnEmailRecipient();
    if (ArrayUtil.isEmpty(recipients)) {
        return;
    }
    executor.execute(new Runnable() {

        @Override
        public void run() {
            try {
                SMTPMessage out = new SMTPMessage(JMSession.getSmtpSession(domain));
                // should From be configurable?
                out.setFrom(new JavaMailInternetAddress("Postmaster <postmaster@" + domain.getName() + ">"));
                for (String recipient : recipients) {
                    out.setRecipient(javax.mail.Message.RecipientType.TO, new JavaMailInternetAddress(recipient));
                }
                out.setSentDate(new Date());
                // using default locale since not sure which locale to pick
                Locale locale = Locale.getDefault();
                out.setSubject(L10nUtil.getMessage(L10nUtil.MsgKey.domainAggrQuotaWarnMsgSubject, locale));
                out.setText(L10nUtil.getMessage(L10nUtil.MsgKey.domainAggrQuotaWarnMsgBody, locale, domain.getName(), domain.getAggregateQuotaLastUsage() / 1024.0 / 1024.0, domain.getDomainAggregateQuotaWarnPercent(), domain.getDomainAggregateQuota() / 1024.0 / 1024.0));
                Transport.send(out);
            } catch (Exception e) {
                ZimbraLog.misc.warn("Error in sending aggregate quota warning msg for domain %s", domain.getName(), e);
            }
        }
    });
}
Also used : Locale(java.util.Locale) SMTPMessage(com.sun.mail.smtp.SMTPMessage) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) Date(java.util.Date) ServiceException(com.zimbra.common.service.ServiceException)

Example 22 with JavaMailInternetAddress

use of com.zimbra.common.mime.shim.JavaMailInternetAddress in project zm-mailbox by Zimbra.

the class AccountUtil method generateMimeMessage.

public static MimeMessage generateMimeMessage(Account authAccount, Account ownerAccount, String subject, String charset, Collection<String> internalRecipients, String externalRecipient, String recipient, MimeMultipart mmp) throws MessagingException {
    MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSmtpSession(authAccount));
    mm.setSubject(subject, CharsetUtil.checkCharset(subject, charset));
    mm.setSentDate(new Date());
    // from the owner
    mm.setFrom(AccountUtil.getFriendlyEmailAddress(ownerAccount));
    // sent by auth account
    mm.setSender(AccountUtil.getFriendlyEmailAddress(authAccount));
    if (internalRecipients != null) {
        assert (externalRecipient == null);
        for (String iRecipient : internalRecipients) {
            try {
                mm.addRecipient(javax.mail.Message.RecipientType.TO, new JavaMailInternetAddress(iRecipient));
            } catch (AddressException e) {
                ZimbraLog.account.warn("Ignoring error while sending notification to " + iRecipient, e);
            }
        }
    } else if (externalRecipient != null) {
        mm.setRecipient(javax.mail.Message.RecipientType.TO, new JavaMailInternetAddress(externalRecipient));
    } else {
        mm.setRecipient(javax.mail.Message.RecipientType.TO, new JavaMailInternetAddress(recipient));
    }
    mm.setContent(mmp);
    mm.saveChanges();
    if (ZimbraLog.account.isDebugEnabled()) {
        try {
            ByteArrayOutputStream buf = new ByteArrayOutputStream();
            mm.writeTo(buf);
            String mmDump = new String(buf.toByteArray());
            ZimbraLog.account.debug("********\n" + mmDump);
        } catch (MessagingException e) {
            ZimbraLog.account.debug("failed log debug share notification message", e);
        } catch (IOException e) {
            ZimbraLog.account.debug("failed log debug share notification message", e);
        }
    }
    return mm;
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) AddressException(javax.mail.internet.AddressException) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Date(java.util.Date)

Example 23 with JavaMailInternetAddress

use of com.zimbra.common.mime.shim.JavaMailInternetAddress in project zm-mailbox by Zimbra.

the class TestSmtpClient method testMimeMessage.

@Test
public void testMimeMessage() throws Exception {
    TestUtil.createAccount(USER_NAME);
    TestUtil.createAccount(USER2_NAME);
    // Assemble the message.
    MimeMessage mm = new ZMimeMessage(JMSession.getSession());
    InternetAddress addr = new JavaMailInternetAddress(TestUtil.getAddress(USER_NAME));
    mm.setFrom(addr);
    mm.setRecipient(RecipientType.TO, addr);
    String subject = NAME_PREFIX + " testMimeMessage";
    mm.setSubject(subject);
    mm.setText("testMimeMessage");
    mm.saveChanges();
    // Initialize SMTP client.
    SmtpConfig config = new SmtpConfig(mHost, mPort, "localhost");
    SmtpConnection conn = new SmtpConnection(config);
    conn.sendMessage(mm);
    // Make sure it arrived.
    ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
    TestUtil.waitForMessage(mbox, "in:inbox subject:\"" + subject + "\"");
    // Send the same message to a different envelope recipient.
    conn.sendMessage(addr.getAddress(), new String[] { TestUtil.getAddress(USER2_NAME) }, mm);
    mbox = TestUtil.getZMailbox(USER2_NAME);
    TestUtil.waitForMessage(mbox, "in:inbox subject:\"" + subject + "\"");
}
Also used : SmtpConfig(com.zimbra.cs.mailclient.smtp.SmtpConfig) SmtpConnection(com.zimbra.cs.mailclient.smtp.SmtpConnection) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) InternetAddress(javax.mail.internet.InternetAddress) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) ZMailbox(com.zimbra.client.ZMailbox) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) Test(org.junit.Test)

Example 24 with JavaMailInternetAddress

use of com.zimbra.common.mime.shim.JavaMailInternetAddress 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");
}
Also used : Account(com.zimbra.cs.account.Account) InternetAddress(javax.mail.internet.InternetAddress) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) DavException(com.zimbra.cs.dav.DavException) ArrayList(java.util.ArrayList) ZOrganizer(com.zimbra.cs.mailbox.calendar.ZOrganizer) Provisioning(com.zimbra.cs.account.Provisioning) FriendlyCalendaringDescription(com.zimbra.cs.mailbox.calendar.FriendlyCalendaringDescription) Mailbox(com.zimbra.cs.mailbox.Mailbox) ServiceException(com.zimbra.common.service.ServiceException) MimeMessage(javax.mail.internet.MimeMessage) AddressException(javax.mail.internet.AddressException) ZAttendee(com.zimbra.cs.mailbox.calendar.ZAttendee) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) ZProperty(com.zimbra.common.calendar.ZCalendar.ZProperty) Invite(com.zimbra.cs.mailbox.calendar.Invite)

Example 25 with JavaMailInternetAddress

use of com.zimbra.common.mime.shim.JavaMailInternetAddress in project zm-mailbox by Zimbra.

the class Notification method assembleNotificationMessage.

private MimeMessage assembleNotificationMessage(Account account, Message msg, String rcpt, String destination, Session smtpSession) throws MessagingException {
    String recipientDomain = getDomain(rcpt);
    Map<String, String> vars = new HashMap<String, String>();
    vars.put("SENDER_ADDRESS", ZInternetHeader.decode(msg.getSender()));
    vars.put("RECIPIENT_ADDRESS", rcpt);
    vars.put("RECIPIENT_DOMAIN", recipientDomain);
    vars.put("NOTIFICATION_ADDRESS", destination);
    vars.put("SUBJECT", msg.getSubject());
    vars.put("DATE", new MailDateFormat().format(new Date()));
    vars.put("NEWLINE", "\n");
    MimeMessage out = null;
    String template = account.getAttr(Provisioning.A_zimbraNewMailNotificationMessage, null);
    if (template != null) {
        String msgBody = StringUtil.fillTemplate(template, vars);
        InputStream is = new ByteArrayInputStream(msgBody.getBytes());
        out = new MimeMessage(smtpSession, is);
        InternetAddress address = new JavaMailInternetAddress(destination);
        out.setRecipient(javax.mail.Message.RecipientType.TO, address);
    } else {
        out = new ZMimeMessage(smtpSession);
        String from = account.getAttr(Provisioning.A_zimbraNewMailNotificationFrom);
        String subject = account.getAttr(Provisioning.A_zimbraNewMailNotificationSubject);
        String body = account.getAttr(Provisioning.A_zimbraNewMailNotificationBody);
        if (from == null || subject == null || body == null) {
            nfailed("null from, subject or body", destination, rcpt, msg);
            return null;
        }
        from = StringUtil.fillTemplate(from, vars);
        subject = StringUtil.fillTemplate(subject, vars);
        body = StringUtil.fillTemplate(body, vars);
        InternetAddress address = new JavaMailInternetAddress(from);
        out.setFrom(address);
        address = new JavaMailInternetAddress(destination);
        out.setRecipient(javax.mail.Message.RecipientType.TO, address);
        String charset = getCharset(account, subject);
        out.setSubject(subject, charset);
        charset = getCharset(account, body);
        out.setText(body, charset);
    }
    if (out != null) {
        out.setHeader("Auto-Submitted", "auto-replied (notification; " + rcpt + ")");
    }
    return out;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) HashMap(java.util.HashMap) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) MailDateFormat(javax.mail.internet.MailDateFormat) Date(java.util.Date)

Aggregations

JavaMailInternetAddress (com.zimbra.common.mime.shim.JavaMailInternetAddress)46 InternetAddress (javax.mail.internet.InternetAddress)31 MimeMessage (javax.mail.internet.MimeMessage)25 AddressException (javax.mail.internet.AddressException)17 Date (java.util.Date)15 MessagingException (javax.mail.MessagingException)14 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)13 Account (com.zimbra.cs.account.Account)12 MailSender (com.zimbra.cs.mailbox.MailSender)9 UnsupportedEncodingException (java.io.UnsupportedEncodingException)9 ArrayList (java.util.ArrayList)9 Address (javax.mail.Address)9 ServiceException (com.zimbra.common.service.ServiceException)8 Locale (java.util.Locale)8 SMTPMessage (com.sun.mail.smtp.SMTPMessage)7 MimeBodyPart (javax.mail.internet.MimeBodyPart)7 ZMimeBodyPart (com.zimbra.common.zmime.ZMimeBodyPart)6 ZMimeMultipart (com.zimbra.common.zmime.ZMimeMultipart)6 IOException (java.io.IOException)6 MimeMultipart (javax.mail.internet.MimeMultipart)6