use of javax.mail.internet.InternetAddress 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.InternetAddress 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.InternetAddress in project zm-mailbox by Zimbra.
the class Identity method getFriendlyEmailAddress.
public InternetAddress getFriendlyEmailAddress() {
String personalPart = getAttr(Provisioning.A_zimbraPrefFromDisplay);
if (personalPart == null || personalPart.trim().equals(""))
personalPart = null;
String address = getAttr(Provisioning.A_zimbraPrefFromAddress);
try {
return new JavaMailInternetAddress(address, personalPart, MimeConstants.P_CHARSET_UTF8);
} catch (UnsupportedEncodingException e) {
}
// UTF-8 should *always* be supported (i.e. this is actually unreachable)
try {
// fall back to using the system's default charset (also pretty much guaranteed not to be "unsupported")
return new JavaMailInternetAddress(address, personalPart);
} catch (UnsupportedEncodingException e) {
}
// if we ever reached this point (which we won't), just return an address with no personal part
InternetAddress ia = new JavaMailInternetAddress();
ia.setAddress(address);
return ia;
}
use of javax.mail.internet.InternetAddress 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.InternetAddress in project perun by CESNET.
the class MessagePreparator method prepare.
/*
* (non-Javadoc)
*
* @see org.springframework.mail.javamail.MimeMessagePreparator#prepare(javax.mail.internet.MimeMessage)
*/
public void prepare(MimeMessage mimeMessage) throws Exception {
MimeMultipart mpRoot = new MimeMultipart("mixed");
Multipart mp = new MimeMultipart("alternative");
// Create a body part to house the multipart/alternative Part
MimeBodyPart contentPartRoot = new MimeBodyPart();
contentPartRoot.setContent(mp);
// Add the root body part to the root multipart
mpRoot.addBodyPart(contentPartRoot);
// adding recipients, cc and bcc
if (getTo() != null) {
for (String to : getTo()) {
mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
}
}
if (getCc() != null) {
for (String cc : getCc()) {
mimeMessage.addRecipient(Message.RecipientType.CC, new InternetAddress(cc));
}
}
if (getBcc() != null) {
for (String bcc : getBcc()) {
mimeMessage.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc));
}
}
// adding from and subject
mimeMessage.setFrom(new InternetAddress(getFrom(), getFromText()));
mimeMessage.setSubject(getSubject());
if (getEmailType().equals(EmailType.HTML)) {
mp.addBodyPart(createHtmlMessage());
} else if (getEmailType().equals(EmailType.PLAIN)) {
mp.addBodyPart(createTextMessage());
}
// Create an "ATTACHMENT" - we must put it to mpRoot(mixed content)
if (getFileNames() != null) {
for (String filename : getFileNames()) {
mpRoot.addBodyPart(createAttachment(filename));
}
}
mimeMessage.setContent(mpRoot);
logger.debug("Message is prepared to send");
}
Aggregations