use of com.zimbra.common.mime.shim.JavaMailInternetAddress in project zm-mailbox by Zimbra.
the class Notification method outOfOfficeIfNecessary.
/**
* If the recipient's account requires out of office notification,
* send it out. We send these out based on users' setting, and
* the incoming message meeting certain criteria.
*/
private void outOfOfficeIfNecessary(Account account, Mailbox mbox, MimeMessage mm, Integer msgId, String rcpt, String envSenderString) throws ServiceException, MessagingException {
boolean replyEnabled = account.isPrefOutOfOfficeReplyEnabled();
if (ZimbraLog.mailbox.isDebugEnabled()) {
ZimbraLog.mailbox.debug("outofoffice reply enabled=" + replyEnabled + " rcpt='" + rcpt + "' mid=" + msgId);
}
if (!replyEnabled) {
return;
}
// Check if we are in any configured out of office interval
Date now = new Date();
Date fromDate = account.getGeneralizedTimeAttr(Provisioning.A_zimbraPrefOutOfOfficeFromDate, null);
if (fromDate != null && now.before(fromDate)) {
ofailed("from date not reached", null, rcpt, msgId);
return;
}
Date untilDate = account.getGeneralizedTimeAttr(Provisioning.A_zimbraPrefOutOfOfficeUntilDate, null);
if (untilDate != null && now.after(untilDate)) {
ofailed("until date reached", null, rcpt, msgId);
return;
}
// If envelope sender is empty
if (envSenderString == null) {
ofailed("envelope sender null", null, rcpt, msgId);
return;
}
if (envSenderString.length() < 1) {
// be conservative
ofailed("envelope sender empty", null, rcpt, msgId);
return;
}
InternetAddress envSender;
try {
// NB: 'strict' being 'true' causes <> to except
envSender = new JavaMailInternetAddress(envSenderString, true);
} catch (AddressException ae) {
ofailed("envelope sender invalid", envSenderString, rcpt, msgId, ae);
return;
}
String destination = envSender.getAddress();
if (Mime.isAutoSubmitted(mm)) {
ofailed("auto-submitted not no", destination, rcpt, msgId);
return;
}
// If precedence is bulk, junk or list
String[] precedence = mm.getHeader("Precedence");
if (hasPrecedence(precedence, "bulk")) {
ofailed("precedence bulk", destination, rcpt, msgId);
return;
} else if (hasPrecedence(precedence, "junk")) {
ofailed("precedence junk", destination, rcpt, msgId);
return;
} else if (hasPrecedence(precedence, "list")) {
ofailed("precedence list", destination, rcpt, msgId);
return;
}
// Check if the envelope sender indicates a mailing list owner and such
String[] envSenderAddrParts = EmailUtil.getLocalPartAndDomain(destination);
if (envSenderAddrParts == null) {
ofailed("envelope sender invalid", destination, rcpt, msgId);
return;
}
String envSenderLocalPart = envSenderAddrParts[0];
envSenderLocalPart = envSenderLocalPart.toLowerCase();
if (envSenderLocalPart.startsWith("owner-") || envSenderLocalPart.endsWith("-owner")) {
ofailed("envelope sender has owner- or -owner", destination, rcpt, msgId);
return;
}
if (envSenderLocalPart.contains("-request")) {
ofailed("envelope sender contains -request", destination, rcpt, msgId);
return;
}
if (envSenderLocalPart.equals("mailer-daemon")) {
ofailed("envelope sender is mailer-daemon", destination, rcpt, msgId);
return;
}
if (envSenderLocalPart.equals("majordomo")) {
ofailed("envelope sender is majordomo", destination, rcpt, msgId);
return;
}
if (envSenderLocalPart.equals("listserv")) {
ofailed("envelope sender is listserv", destination, rcpt, msgId);
return;
}
// multipart/report is also machine generated
String ct = mm.getContentType();
if (ct != null && ct.equalsIgnoreCase("multipart/report")) {
ofailed("content-type multipart/report", destination, rcpt, msgId);
return;
}
// Check if recipient was directly mentioned in to/cc of this message
String[] otherAccountAddrs = account.getMultiAttr(Provisioning.A_zimbraPrefOutOfOfficeDirectAddress);
if (!AccountUtil.isDirectRecipient(account, otherAccountAddrs, mm, OUT_OF_OFFICE_DIRECT_CHECK_NUM_RECIPIENTS)) {
ofailed("not direct", destination, rcpt, msgId);
return;
}
// If we've already sent to this user, do not send again
DbConnection conn = null;
try {
conn = DbPool.getConnection(mbox);
if (DbOutOfOffice.alreadySent(conn, mbox, destination, account.getTimeInterval(Provisioning.A_zimbraPrefOutOfOfficeCacheDuration, DEFAULT_OUT_OF_OFFICE_CACHE_DURATION_MILLIS))) {
ofailed("already sent", destination, rcpt, msgId);
return;
}
} finally {
DbPool.quietClose(conn);
}
// Send the message
try {
SMTPMessage out = AccountUtil.getSmtpMessageObj(account);
// Set From and Reply-To.
out.setFrom(AccountUtil.getFromAddress(account));
InternetAddress replyTo = AccountUtil.getReplyToAddress(account);
if (replyTo != null) {
out.setReplyTo(new Address[] { replyTo });
}
// To
out.setRecipient(javax.mail.Message.RecipientType.TO, envSender);
// Date
out.setSentDate(new Date());
// Subject
String subject = Mime.getSubject(mm);
String replySubjectPrefix = L10nUtil.getMessage(L10nUtil.MsgKey.replySubjectPrefix, account.getLocale());
if (subject == null) {
subject = replySubjectPrefix;
} else if (!subject.toLowerCase().startsWith(replySubjectPrefix.toLowerCase())) {
subject = replySubjectPrefix + " " + subject;
}
String charset = getCharset(account, subject);
out.setSubject(subject, charset);
// In-Reply-To
String messageId = mm.getMessageID();
if (messageId != null && !messageId.trim().equals("")) {
out.setHeader("In-Reply-To", messageId);
}
// Auto-Submitted
out.setHeader("Auto-Submitted", "auto-replied (zimbra; vacation)");
// Precedence (discourage older systems from responding)
out.setHeader("Precedence", "bulk");
// check whether to send "external" OOO reply
if (skipOutOfOfficeMsg(destination, account, mbox)) {
ZimbraLog.mailbox.info("%s is external user and no external reply option is set, so no OOO will be sent.", destination);
return;
}
boolean sendExternalReply = sendOutOfOfficeExternalReply(destination, account, mbox);
String body = account.getAttr(sendExternalReply ? Provisioning.A_zimbraPrefOutOfOfficeExternalReply : Provisioning.A_zimbraPrefOutOfOfficeReply, "");
charset = getCharset(account, body);
out.setText(body, charset);
if (Provisioning.getInstance().getConfig().getBooleanAttr(Provisioning.A_zimbraAutoSubmittedNullReturnPath, true)) {
out.setEnvelopeFrom("<>");
} else {
out.setEnvelopeFrom(account.getName());
}
MailSender sender = mbox.getMailSender();
sender.setSaveToSent(false);
sender.setDsnNotifyOptions(MailSender.DsnNotifyOption.NEVER);
sender.sendMimeMessage(null, mbox, out);
ZimbraLog.mailbox.info("outofoffice sent dest='" + destination + "' rcpt='" + rcpt + "' mid=" + msgId);
// Save so we will not send to again
try {
conn = DbPool.getConnection(mbox);
DbOutOfOffice.setSentTime(conn, mbox, destination);
conn.commit();
} finally {
DbPool.quietClose(conn);
}
} catch (MessagingException me) {
ofailed("send failed", destination, rcpt, msgId, me);
}
}
use of com.zimbra.common.mime.shim.JavaMailInternetAddress in project zm-mailbox by Zimbra.
the class MailUtil method populateFailureDeliveryMessageFields.
public static void populateFailureDeliveryMessageFields(MimeMessage failedDeliverymm, String subject, String to, List<Argument> addressArgs, InternetAddress iAddr) throws MessagingException, UnsupportedEncodingException {
failedDeliverymm.setSubject("Send Partial Failure Notice");
failedDeliverymm.setSentDate(new Date());
failedDeliverymm.setFrom(iAddr);
failedDeliverymm.setRecipient(RecipientType.TO, new JavaMailInternetAddress(to));
StringBuilder text = new StringBuilder();
String sentDate = new SimpleDateFormat("MMMMM d, yyyy").format(new Date());
String sentTime = new SimpleDateFormat("h:mm a").format(new Date());
text.append("Your message \"" + subject + "\" sent on " + sentDate + " at " + sentTime + " " + TimeZone.getDefault().getDisplayName() + " " + "could not be delivered to one or more recipients.\n\n" + "For further assistance, please send mail to postmaster.\n\n");
List<String> invalidAddrs = new ArrayList<String>();
List<String> unsentAddrs = new ArrayList<String>();
if (addressArgs != null) {
for (Argument arg : addressArgs) {
if (arg.name != null && arg.value != null && arg.value.length() > 0) {
if (arg.name.equals("invalid"))
invalidAddrs.add(arg.value);
else if (arg.name.equals("unsent"))
unsentAddrs.add(arg.value);
}
}
}
for (String addr : invalidAddrs) text.append(addr + ":" + "Invalid Address\n");
for (String addr : unsentAddrs) text.append(addr + ":" + "Unsent Address\n");
failedDeliverymm.setText(text.toString());
failedDeliverymm.saveChanges();
}
use of com.zimbra.common.mime.shim.JavaMailInternetAddress in project zm-mailbox by Zimbra.
the class AppendMessage method store.
private int store(ImapMailboxStore mboxStore, FolderStore folderStore) throws ImapSessionClosedException, ServiceException, IOException {
if (mboxStore instanceof LocalImapMailboxStore) {
boolean idxAttach = ((LocalImapMailboxStore) mboxStore).attachmentsIndexingEnabled();
Long receivedDate = date != null ? date.getTime() : null;
ParsedMessage pm = new ParsedMessage(content, receivedDate, idxAttach);
try {
if (!pm.getSender().isEmpty()) {
InternetAddress ia = new JavaMailInternetAddress(pm.getSender());
if (mboxStore.addressMatchesAccountOrSendAs(ia.getAddress())) {
flags |= Flag.BITMASK_FROM_ME;
}
}
} catch (Exception e) {
}
int folderId = Integer.parseInt(folderStore.getFolderIdAsString());
DeliveryOptions dopt = new DeliveryOptions().setFolderId(folderId).setNoICal(true).setFlags(flags).setTags(tags);
Message msg = ((LocalImapMailboxStore) mboxStore).getMailbox().addMessage(handler.getContext(), pm, dopt, null);
if (msg != null && sflags != 0 && handler.getState() == ImapHandler.State.SELECTED) {
ImapFolder selectedFolder = handler.getSelectedFolder();
// (note that this leaves session flags unset on remote appended messages)
if (selectedFolder != null) {
ImapMessage i4msg = selectedFolder.getById(msg.getId());
if (i4msg != null) {
i4msg.setSessionFlags(sflags, selectedFolder);
}
}
}
return msg == null ? -1 : msg.getId();
}
if (mboxStore instanceof RemoteImapMailboxStore) {
String id;
try (InputStream is = content.getInputStream()) {
TagSpecifier tagSpec = tags.isEmpty() ? null : TagSpecifier.tagByName(Joiner.on(",").join(tags));
long receivedDate = date != null ? date.getTime() : 0;
id = ((RemoteImapMailboxStore) mboxStore).getZMailbox().addMessage(folderStore.getFolderIdAsString(), Flag.toString(flags), tagSpec, receivedDate, is, content.getRawSize(), true);
}
int msgId = new ItemId(id, mboxStore.getAccountId()).getId();
ImapFolder selectedFolder = handler.getSelectedFolder();
if (selectedFolder != null) {
ImapMessage i4msg = selectedFolder.getById(msgId);
if (i4msg != null) {
i4msg.setSessionFlags(sflags, selectedFolder);
}
}
return msgId;
}
return -1;
}
use of com.zimbra.common.mime.shim.JavaMailInternetAddress in project zm-mailbox by Zimbra.
the class MailSenderTest method sendExternalMessage.
// @Test
public void sendExternalMessage() throws Exception {
Provisioning prov = Provisioning.getInstance();
Server server = prov.getLocalServer();
server.setSmtpHostname(new String[] { "bogusname.test" });
server.setSmtpPort(25);
server.setSmtpTimeout(60);
server.setSmtpSendPartial(true);
server.setShareNotificationMtaHostname("mta02.zimbra.com");
server.setShareNotificationMtaPort(25);
server.setShareNotificationMtaAuthRequired(true);
server.setShareNotificationMtaConnectionType(ShareNotificationMtaConnectionType.STARTTLS);
server.setShareNotificationMtaAuthAccount("test-jylee");
server.setShareNotificationMtaAuthPassword("test123");
MimeMessage mm = new MimeMessage(JMSession.getSmtpSession());
InternetAddress address = new JavaMailInternetAddress("test-jylee@zimbra.com");
mm.setFrom(address);
address = new JavaMailInternetAddress("test-jylee@zimbra.com");
mm.setRecipient(javax.mail.Message.RecipientType.TO, address);
mm.setSubject("test mail");
mm.setText("hello world");
mm.saveChanges();
ZimbraLog.smtp.setLevel(Level.trace);
MailSender.relayMessage(mm);
}
use of com.zimbra.common.mime.shim.JavaMailInternetAddress in project zm-mailbox by Zimbra.
the class IDNUtil method toUnicodeWithPersonalPart.
private static String toUnicodeWithPersonalPart(String name) {
try {
InternetAddress ia = new JavaMailInternetAddress(name, true);
/*
* InternetAddress.toUnicodeString only deals with
* non-ascii chars in personal part, it has nothing
* to do with IDN.
*/
// return ia.toUnicodeString();
String addr = ia.getAddress();
String unicodeAddr = toUnicode(addr);
try {
ia = new JavaMailInternetAddress(unicodeAddr, ia.getPersonal(), MimeConstants.P_CHARSET_UTF8);
/*
* call InternetAddress.toUnicodeString instead of
* InternetAddress.toString so non-ascii chars in
* personal part can be returned in Unicode instead of
* RFC 2047 encoded.
*/
return ia.toUnicodeString();
} catch (UnsupportedEncodingException e) {
ZimbraLog.account.info("cannot convert to unicode, returning original addr: [" + name + "]", e);
}
} catch (AddressException e) {
ZimbraLog.account.info("cannot convert to unicode, returning original addr: [" + name + "]", e);
}
return name;
}
Aggregations