Search in sources :

Example 1 with MailDateFormat

use of javax.mail.internet.MailDateFormat in project fess-crawler by codelibs.

the class EmlExtractor method getReceivedDate.

protected static Date getReceivedDate(final javax.mail.Message message) throws MessagingException {
    final Date today = new Date();
    final String[] received = message.getHeader("received");
    if (received != null) {
        for (final String v : received) {
            String dateStr = null;
            try {
                dateStr = getDateString(v);
                final Date receivedDate = new MailDateFormat().parse(dateStr);
                if (!receivedDate.after(today)) {
                    return receivedDate;
                }
            } catch (final ParseException e) {
            // ignore
            }
        }
    }
    return null;
}
Also used : MailDateFormat(javax.mail.internet.MailDateFormat) ParseException(java.text.ParseException) Date(java.util.Date)

Example 2 with MailDateFormat

use of javax.mail.internet.MailDateFormat in project i2p.i2p-bote by i2p.

the class Email method updateHeaders.

/**
 * Removes all headers that are not on the whitelist, and initializes some
 * basic header fields.<br/>
 * Called by {@link #saveChanges()}, see JavaMail JavaDoc.
 * @throws MessagingException
 */
@Override
public void updateHeaders() throws MessagingException {
    super.updateHeaders();
    scrubHeaders();
    removeRecipientNames();
    // Depending on includeSendTime, set the send time or remove the send time field
    if (includeSendTime) {
        // Ensure the "Date" field is set in UTC time, using the English locale.
        MailDateFormat formatter = new MailDateFormat();
        // always use UTC for outgoing mail
        formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
        if (getSentDate() == null)
            setHeader("Date", formatter.format(new Date()));
        else
            setHeader("Date", formatter.format(getSentDate()));
    } else
        removeHeader("Date");
}
Also used : MailDateFormat(javax.mail.internet.MailDateFormat) Date(java.util.Date)

Example 3 with MailDateFormat

use of javax.mail.internet.MailDateFormat in project zm-mailbox by Zimbra.

the class QuotaWarning method afterDelivery.

@Override
public void afterDelivery(Account account, Mailbox mbox, String envelopeSender, String recipientEmail, Message newMessage) {
    try {
        int warnPercent = account.getIntAttr(Provisioning.A_zimbraQuotaWarnPercent, 90);
        long quota = AccountUtil.getEffectiveQuota(account);
        long warnInterval = account.getTimeInterval(Provisioning.A_zimbraQuotaWarnInterval, Constants.MILLIS_PER_DAY);
        String template = account.getAttr(Provisioning.A_zimbraQuotaWarnMessage, null);
        Date lastWarnTime = account.getGeneralizedTimeAttr(Provisioning.A_zimbraQuotaLastWarnTime, null);
        Date now = new Date();
        ZimbraLog.lmtp.debug("Checking quota warning: mbox size=%d, quota=%d, lastWarnTime=%s, warnInterval=%d, warnPercent=%d", mbox.getSize(), quota, lastWarnTime, warnInterval, warnPercent);
        // has already been sent.
        if (quota == 0 || warnPercent == 0) {
            return;
        }
        if (mbox.getSize() * 100 / quota < warnPercent) {
            return;
        }
        if (lastWarnTime != null && now.getTime() - lastWarnTime.getTime() < warnInterval) {
            return;
        }
        if (template == null) {
            ZimbraLog.lmtp.warn("%s not specified.  Unable to send quota warning message.", Provisioning.A_zimbraQuotaWarnMessage);
        }
        // Send the quota warning
        ZimbraLog.lmtp.info("Sending quota warning: mbox size=%d, quota=%d, lastWarnTime=%s, warnInterval=%dms, warnPercent=%d", mbox.getSize(), quota, lastWarnTime, warnInterval, warnPercent);
        String address = account.getAttr(Provisioning.A_zimbraMailDeliveryAddress);
        String domain = EmailUtil.getLocalPartAndDomain(address)[1];
        Map<String, Object> vars = new HashMap<String, Object>();
        vars.put("RECIPIENT_NAME", account.getAttr(Provisioning.A_displayName));
        vars.put("RECIPIENT_DOMAIN", domain);
        vars.put("RECIPIENT_ADDRESS", address);
        vars.put("DATE", new MailDateFormat().format(new Date()));
        vars.put("MBOX_SIZE_MB", String.format("%.2f", mbox.getSize() / 1024.0 / 1024.0));
        vars.put("QUOTA_MB", String.format("%.2f", quota / 1024.0 / 1024.0));
        vars.put("WARN_PERCENT", warnPercent);
        vars.put("NEWLINE", "\r\n");
        String msgBody = StringUtil.fillTemplate(template, vars);
        ParsedMessage pm = new ParsedMessage(msgBody.getBytes(), now.getTime(), false);
        DeliveryOptions dopt = new DeliveryOptions().setFolderId(Mailbox.ID_FOLDER_INBOX).setFlags(Flag.BITMASK_UNREAD | Flag.BITMASK_HIGH_PRIORITY);
        mbox.addMessage(null, pm, dopt, null);
        // Update last sent date
        Map<String, String> attrs = new HashMap<String, String>();
        attrs.put(Provisioning.A_zimbraQuotaLastWarnTime, LdapDateUtil.toGeneralizedTime(now));
        Provisioning.getInstance().modifyAttrs(account, attrs);
    } catch (Exception e) {
        ZimbraLog.lmtp.warn("Unable to send quota warning message", e);
    }
}
Also used : HashMap(java.util.HashMap) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) MailDateFormat(javax.mail.internet.MailDateFormat) Date(java.util.Date)

Example 4 with MailDateFormat

use of javax.mail.internet.MailDateFormat in project i2p.i2p-bote by i2p.

the class EmailMetadata method getCreateTime.

/**
 * Returns the date and time the email was submitted by the user, or <code>null</code>
 * if the value cannot be parsed.
 */
public Date getCreateTime() {
    String dateStr = getProperty(PROPERTY_CREATE_TIME);
    Date createTime;
    try {
        createTime = new MailDateFormat().parse(dateStr);
    } catch (ParseException e) {
        log.error("Can't parse create time.", e);
        createTime = null;
    }
    return createTime;
}
Also used : MailDateFormat(javax.mail.internet.MailDateFormat) ParseException(java.text.ParseException) Date(java.util.Date)

Example 5 with MailDateFormat

use of javax.mail.internet.MailDateFormat 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

MailDateFormat (javax.mail.internet.MailDateFormat)8 Date (java.util.Date)7 ServiceException (com.zimbra.common.service.ServiceException)2 ParseException (java.text.ParseException)2 HashMap (java.util.HashMap)2 ZMailbox (com.zimbra.client.ZMailbox)1 ZMessage (com.zimbra.client.ZMessage)1 ZPop3DataSource (com.zimbra.client.ZPop3DataSource)1 JavaMailInternetAddress (com.zimbra.common.mime.shim.JavaMailInternetAddress)1 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)1 MailboxInfo (com.zimbra.cs.mailclient.imap.MailboxInfo)1 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 MessagingException (javax.mail.MessagingException)1 InternetAddress (javax.mail.internet.InternetAddress)1 MimeMessage (javax.mail.internet.MimeMessage)1 Test (org.junit.Test)1