use of javax.mail.internet.AddressException in project zm-mailbox by Zimbra.
the class IDNUtil method toAsciiWithPersonalPart.
private static String toAsciiWithPersonalPart(String name) {
String asciiName = name;
// extract out the domain, convert it to ACE, and put it back
Matcher m = S_DOMAIN.matcher(name);
if (m.matches() && m.groupCount() == 4) {
// if matches(), then groupCount() must be 4 according to our regex, just to be safe
String domain = m.group(3);
String asciiDomain = toAsciiDomainName(domain);
// put everything back
asciiName = m.group(1) + m.group(2) + asciiDomain + m.group(4);
}
try {
InternetAddress ia = new JavaMailInternetAddress(asciiName);
String personal = ia.getPersonal();
if (personal != null)
ia = new JavaMailInternetAddress(ia.getAddress(), personal, MimeConstants.P_CHARSET_UTF8);
/*
* note, if personal part contains non-ascii chars, it will be
* converted (by InternetAddress.toString()) to RFC 2047 encoded address.
* The resulting string contains only US-ASCII characters, and
* hence is mail-safe.
*
* e.g. a personal part: 中文
* will be converted to =?utf-8?B?5Lit5paH?=
* and stored in LDAP in the encoded form
*/
return ia.toString();
} catch (UnsupportedEncodingException e) {
ZimbraLog.account.info("cannot convert to ascii, returning original addr: [" + name + "]", e);
} catch (AddressException e) {
ZimbraLog.account.info("cannot convert to ascii, returning original addr: [" + name + "]", e);
}
return name;
}
use of javax.mail.internet.AddressException 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;
}
use of javax.mail.internet.AddressException in project zm-mailbox by Zimbra.
the class AutoProvision method sendNotifMessage.
protected void sendNotifMessage(Account acct, String password) throws ServiceException {
String subject = fillTemplate(acct, domain.getAutoProvNotificationSubject());
String body = fillTemplate(acct, domain.getAutoProvNotificationBody());
String from = domain.getAutoProvNotificationFromAddress();
if (from == null) {
// TODO: should we use a seperate boolean control?
return;
}
String toAddr = acct.getName();
try {
SMTPMessage out = new SMTPMessage(JMSession.getSmtpSession());
InternetAddress addr = null;
try {
addr = new JavaMailInternetAddress(from);
} catch (AddressException e) {
// log and try the next one
ZimbraLog.autoprov.warn("invalid address in " + Provisioning.A_zimbraAutoProvNotificationFromAddress, e);
}
Address fromAddr = addr;
Address replyToAddr = addr;
// From
out.setFrom(fromAddr);
// Reply-To
out.setReplyTo(new Address[] { replyToAddr });
// To
out.setRecipient(javax.mail.Message.RecipientType.TO, new JavaMailInternetAddress(toAddr));
// Date
out.setSentDate(new Date());
// Subject
Locale locale = acct.getLocale();
out.setSubject(subject);
// NOTIFY=NEVER
out.setNotifyOptions(SMTPMessage.NOTIFY_NEVER);
// body
MimeMultipart mmp = new ZMimeMultipart("alternative");
// TEXT part (add me first!)
String text = body;
MimeBodyPart textPart = new ZMimeBodyPart();
textPart.setText(text, MimeConstants.P_CHARSET_UTF8);
mmp.addBodyPart(textPart);
// HTML part
StringBuilder html = new StringBuilder();
html.append("<h4>\n");
html.append("<p>" + body + "</p>\n");
html.append("</h4>\n");
html.append("\n");
MimeBodyPart htmlPart = new ZMimeBodyPart();
htmlPart.setDataHandler(new DataHandler(new HtmlPartDataSource(html.toString())));
mmp.addBodyPart(htmlPart);
out.setContent(mmp);
// send it
Transport.send(out);
// log
Address[] rcpts = out.getRecipients(javax.mail.Message.RecipientType.TO);
StringBuilder rcptAddr = new StringBuilder();
for (Address a : rcpts) rcptAddr.append(a.toString());
ZimbraLog.autoprov.info("auto provision notification sent rcpt='" + rcptAddr + "' Message-ID=" + out.getMessageID());
} catch (MessagingException e) {
ZimbraLog.autoprov.warn("send auto provision notification failed rcpt='" + toAddr + "'", e);
}
}
use of javax.mail.internet.AddressException in project Asqatasun by Asqatasun.
the class EmailSender method sendEmail.
/**
*
* @param emailFrom
* @param emailToSet
* @param emailBccSet (can be null)
* @param replyTo (can be null)
* @param emailSubject
* @param emailContent
*/
public void sendEmail(String emailFrom, Set<String> emailToSet, Set<String> emailBccSet, String replyTo, String emailSubject, String emailContent) {
boolean debug = false;
// Set the host smtp address
Properties props = new Properties();
props.put("mail.smtp.host", smtpHost);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
// create some properties and get the default Session
Session session = Session.getInstance(props);
session.setDebug(debug);
try {
Transport t = session.getTransport("smtp");
t.connect(smtpHost, userName, password);
// create a message
MimeMessage msg = new MimeMessage(session);
// set the from and to address
InternetAddress addressFrom;
try {
// Used default from address is passed one is null or empty or
// blank
addressFrom = (StringUtils.isNotBlank(emailFrom)) ? new InternetAddress(emailFrom) : new InternetAddress(from);
msg.setFrom(addressFrom);
Address[] recipients = new InternetAddress[emailToSet.size()];
int i = 0;
for (String emailTo : emailToSet) {
recipients[i] = new InternetAddress(emailTo);
i++;
}
msg.setRecipients(Message.RecipientType.TO, recipients);
if (CollectionUtils.isNotEmpty(emailBccSet)) {
Address[] bccRecipients = new InternetAddress[emailBccSet.size()];
i = 0;
for (String emailBcc : emailBccSet) {
bccRecipients[i] = new InternetAddress(emailBcc);
i++;
}
msg.setRecipients(Message.RecipientType.BCC, bccRecipients);
}
if (StringUtils.isNotBlank(replyTo)) {
Address[] replyToRecipients = { new InternetAddress(replyTo) };
msg.setReplyTo(replyToRecipients);
}
// Setting the Subject
msg.setSubject(emailSubject, CHARSET_KEY);
// Setting content and charset (warning: both declarations of
// charset are needed)
msg.setHeader(CONTENT_TYPE_KEY, FULL_CHARSET_KEY);
LOGGER.debug("emailContent " + emailContent);
msg.setContent(emailContent, FULL_CHARSET_KEY);
try {
LOGGER.debug("emailContent from message object " + msg.getContent().toString());
} catch (IOException ex) {
LOGGER.error(ex.getMessage());
} catch (MessagingException ex) {
LOGGER.error(ex.getMessage());
}
for (Address addr : msg.getAllRecipients()) {
LOGGER.debug("addr " + addr);
}
t.sendMessage(msg, msg.getAllRecipients());
} catch (AddressException ex) {
LOGGER.warn("AddressException " + ex.getMessage());
LOGGER.warn("AddressException " + ex.getStackTrace());
}
} catch (NoSuchProviderException e) {
LOGGER.warn("NoSuchProviderException " + e.getMessage());
LOGGER.warn("NoSuchProviderException " + e.getStackTrace());
} catch (MessagingException e) {
LOGGER.warn("MessagingException " + e.getMessage());
LOGGER.warn("MessagingException " + e.getStackTrace());
}
}
use of javax.mail.internet.AddressException in project zm-mailbox by Zimbra.
the class CalendarItem method processNewInviteReply.
boolean processNewInviteReply(Invite reply, String sender) throws ServiceException {
List<ZAttendee> attendees = reply.getAttendees();
String senderAddress = null;
if (sender != null && !sender.isEmpty()) {
try {
JavaMailInternetAddress address = new JavaMailInternetAddress(sender);
senderAddress = address.getAddress();
} catch (AddressException e) {
// ignore invalid sender address.
}
}
if (senderAddress != null && !attendees.isEmpty()) {
AccountAddressMatcher acctMatcher = null;
Account acct = Provisioning.getInstance().get(AccountBy.name, senderAddress);
if (acct != null) {
acctMatcher = new AccountAddressMatcher(acct);
}
Iterator<ZAttendee> iter = attendees.iterator();
while (iter.hasNext()) {
ZAttendee att = iter.next();
// Remove the attendee if not same as the sender.
if (!(att.addressMatches(senderAddress) || (acctMatcher != null && acctMatcher.matches(att.getAddress())))) {
iter.remove();
}
}
}
// trace logging
ZAttendee att1 = !attendees.isEmpty() ? attendees.get(0) : null;
if (att1 != null) {
String ptst = IcalXmlStrMap.sPartStatMap.toIcal(att1.getPartStat());
if (!reply.hasRecurId())
ZimbraLog.calendar.info("Processing CalendarItem reply: attendee=%s, partstat=%s, id=%d, folderId=%d, subject=\"%s\", UID=%s", att1.getAddress(), ptst, mId, getFolderId(), reply.isPublic() ? reply.getName() : "(private)", mUid);
else
ZimbraLog.calendar.info("Processing CalendarItem reply: attendee=%s, partstat=%s, id=%d, folderId=%d, subject=\"%s\", UID=%s, recurId=%s", att1.getAddress(), ptst, mId, getFolderId(), reply.isPublic() ? reply.getName() : "(private)", mUid, reply.getRecurId().getDtZ());
}
// Require private access permission only when we're replying to a private series/instance.
boolean requirePrivateCheck = requirePrivateCheck(reply);
OperationContext octxt = getMailbox().getOperationContext();
Account authAccount = octxt != null ? octxt.getAuthenticatedUser() : null;
boolean asAdmin = octxt != null ? octxt.isUsingAdminPrivileges() : false;
if (!canAccess(ACL.RIGHT_ACTION, authAccount, asAdmin, requirePrivateCheck))
throw ServiceException.PERM_DENIED("you do not have sufficient permissions to change this appointment/task's state");
boolean dirty = false;
// unique ID: UID+RECURRENCE_ID
// See RFC2446: 2.1.5 Message Sequencing
// UID already matches...next check if RecurId matches
// if so, then seqNo is next
// finally use DTStamp
Invite matchingInvite = matchingInvite(reply.getRecurId());
if (matchingInvite != null) {
// up to date with the organizer's event, provided there were no major changes.
if ((matchingInvite.isOrganizer() && (matchingInvite.getLastFullSeqNo() > reply.getSeqNo())) || (!matchingInvite.isOrganizer() && (matchingInvite.getSeqNo() > reply.getSeqNo()))) {
sLog.info("Invite-Reply %s is outdated (Calendar entry has higher SEQUENCE), ignoring!", reply);
return false;
}
// maybeStoreNewReply does some further checks which might invalidate this reply
// so, postpone updating attendee information until after that.
}
// they must be replying to a arbitrary instance)
for (ZAttendee at : attendees) {
if (mReplyList.maybeStoreNewReply(reply, at, this))
dirty = true;
}
if (!dirty) {
sLog.info("Invite-Reply %s is outdated ignoring!", reply);
return false;
}
if (matchingInvite != null) {
matchingInvite.updateMatchingAttendeesFromReply(reply);
updateLocalExceptionsWhichMatchSeriesReply(reply);
} else {
createPseudoExceptionForSingleInstanceReplyIfNecessary(reply);
}
saveMetadata();
return true;
}
Aggregations