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;
}
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");
}
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);
}
}
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;
}
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;
}
Aggregations