List of usage examples for javax.mail.internet InternetAddress setPersonal
public void setPersonal(String name, String charset) throws UnsupportedEncodingException
From source file:org.obm.push.mail.MailBackendImpl.java
@VisibleForTesting InternetAddress getUserEmail(UserDataRequest udr) throws UnexpectedObmSyncServerException { String email = authenticationService.getUserEmail(udr); try {// w w w .j av a2s.c o m InternetAddress address = new InternetAddress(email, true); address.setPersonal(udr.getUser().getDisplayName(), Charsets.UTF_8.name()); return address; } catch (UnsupportedEncodingException | AddressException e) { logger.warn("Cannot set internet address", e); throw Throwables.propagate(e); } }
From source file:org.silverpeas.core.mail.engine.SmtpMailSender.java
@Override public void send(final MailToSend mail) { SmtpConfiguration smtpConfiguration = SmtpConfiguration.fromDefaultSettings(); MailAddress fromMailAddress = mail.getFrom(); Session session = getMailSession(smtpConfiguration); try {//w w w. java2 s . com InternetAddress fromAddress = fromMailAddress.getAuthorizedInternetAddress(); InternetAddress replyToAddress = null; List<InternetAddress[]> toAddresses = new ArrayList<>(); // Parsing destination address for compliance with RFC822. final Collection<ReceiverMailAddressSet> addressBatches = mail.getTo().getBatchedReceiversList(); for (ReceiverMailAddressSet addressBatch : addressBatches) { try { toAddresses.add(InternetAddress.parse(addressBatch.getEmailsSeparatedByComma(), false)); } catch (AddressException e) { SilverTrace.warn("mail", "MailSender.send()", "root.MSG_GEN_PARAM_VALUE", "From = " + fromMailAddress + ", To = " + addressBatch.getEmailsSeparatedByComma(), e); } } try { if (mail.isReplyToRequired()) { replyToAddress = new InternetAddress(fromMailAddress.getEmail(), false); if (StringUtil.isDefined(fromMailAddress.getName())) { replyToAddress.setPersonal(fromMailAddress.getName(), Charsets.UTF_8.name()); } } } catch (AddressException e) { SilverTrace.warn("mail", "MailSender.send()", "root.MSG_GEN_PARAM_VALUE", "ReplyTo = " + fromMailAddress + " is malformed.", e); } MimeMessage email = new MimeMessage(session); email.setFrom(fromAddress); if (replyToAddress != null) { email.setReplyTo(new InternetAddress[] { replyToAddress }); } email.setHeader("Precedence", "list"); email.setHeader("List-ID", fromAddress.getAddress()); email.setSentDate(new Date()); email.setSubject(mail.getSubject(), CharEncoding.UTF_8); mail.getContent().applyOn(email); // Sending. performSend(mail, smtpConfiguration, session, email, toAddresses); } catch (MessagingException | UnsupportedEncodingException e) { SilverLogger.getLogger(this).error(e.getMessage(), e); } catch (Exception e) { throw new RuntimeException(e); } }