List of usage examples for javax.mail.internet InternetAddress toString
@Override
public String toString()
From source file:org.beangle.notification.notifiers.mail.MailMessage.java
public List<String> getRecipients() { List<String> recipients = new ArrayList<String>(); for (InternetAddress address : to) recipients.add(address.toString()); for (InternetAddress address : cc) recipients.add(address.toString()); for (InternetAddress address : bcc) recipients.add(address.toString()); return recipients; }
From source file:org.gatherdata.alert.notify.mail.internal.EmailNotifier.java
public void sendEmail(Iterable<InternetAddress> toAddresses, Map<String, String> headers, String body) { SimpleEmail email = new SimpleEmail(); configure(email);/* w w w .j a v a 2 s . c o m*/ try { for (InternetAddress addy : toAddresses) { email.addTo(addy.toString()); } email.setFrom(senderAddress.toString()); String subject = ""; if (headers != null) { subject = headers.containsKey(EmailHeaders.SUBJECT_HEADER) ? headers.get(EmailHeaders.SUBJECT_HEADER) : ""; } email.setSubject(subject); if (body != null) email.setMsg(body); email.send(); } catch (EmailException e) { log.error("failed to send event notification", e); } }
From source file:org.jasig.ssp.service.impl.MessageServiceImpl.java
@Override @Transactional(readOnly = false)/*w w w. j a va 2s .c o m*/ public boolean sendMessage(@NotNull final Message message) throws ObjectNotFoundException, SendFailedException, UnsupportedEncodingException { LOGGER.info("BEGIN : sendMessage()"); LOGGER.info(addMessageIdToError(message) + "Sending message: {}", message.toString()); try { final MimeMessage mimeMessage = javaMailSender.createMimeMessage(); final MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage); // process FROM addresses InternetAddress from; String appName = configService.getByName("app_title").getValue(); //We used the configured outbound email address for every outgoing message //If a message was initiated by an end user, their name will be attached to the 'from' while //the configured outbound address will be the actual address used for example "Amy Aministrator (SSP) <myconfiguredaddress@foobar.com>" String name = appName + " Administrator"; if (message.getSender() != null && !message.getSender().getEmailAddresses().isEmpty() && !message.getSender().getId().equals(Person.SYSTEM_ADMINISTRATOR_ID)) { InternetAddress[] froms = getEmailAddresses(message.getSender(), "from:", message.getId()); if (froms.length > 0) { name = message.getSender().getFullName() + " (" + appName + ")"; } } from = new InternetAddress(configService.getByName("outbound_email_address").getValue(), name); if (!this.validateEmail(from.getAddress())) { throw new AddressException("Invalid from: email address [" + from.getAddress() + "]"); } mimeMessageHelper.setFrom(from); message.setSentFromAddress(from.toString()); mimeMessageHelper.setReplyTo(from); message.setSentReplyToAddress(from.toString()); // process TO addresses InternetAddress[] tos = null; if (message.getRecipient() != null && message.getRecipient().hasEmailAddresses()) { // NOPMD by jon.adams tos = getEmailAddresses(message.getRecipient(), "to:", message.getId()); } else { tos = getEmailAddresses(message.getRecipientEmailAddress(), "to:", message.getId()); } if (tos.length > 0) { mimeMessageHelper.setTo(tos); message.setSentToAddresses(StringUtils.join(tos, ",").trim()); } else { StringBuilder errorMsg = new StringBuilder(); errorMsg.append(addMessageIdToError(message) + " Message " + message.toString() + " could not be sent. No valid recipient email address found: '"); if (message.getRecipient() != null) { errorMsg.append(message.getRecipient().getPrimaryEmailAddress()); } else { errorMsg.append(message.getRecipientEmailAddress()); } LOGGER.error(errorMsg.toString()); throw new MessagingException(errorMsg.toString()); } // process BCC addresses try { InternetAddress[] bccs = getEmailAddresses(getBcc(), "bcc:", message.getId()); if (bccs.length > 0) { mimeMessageHelper.setBcc(bccs); message.setSentBccAddresses(StringUtils.join(bccs, ",").trim()); } } catch (Exception exp) { LOGGER.warn("Unrecoverable errors were generated adding carbon copy to message: " + message.getId() + "Attempt to send message still initiated.", exp); } // process CC addresses try { InternetAddress[] carbonCopies = getEmailAddresses(message.getCarbonCopy(), "cc:", message.getId()); if (carbonCopies.length > 0) { mimeMessageHelper.setCc(carbonCopies); message.setSentCcAddresses(StringUtils.join(carbonCopies, ",").trim()); } } catch (Exception exp) { LOGGER.warn("Unrecoverable errors were generated adding bcc to message: " + message.getId() + "Attempt to send message still initiated.", exp); } mimeMessageHelper.setSubject(message.getSubject()); mimeMessageHelper.setText(message.getBody()); mimeMessage.setContent(message.getBody(), "text/html"); send(mimeMessage); message.setSentDate(new Date()); messageDao.save(message); } catch (final MessagingException e) { LOGGER.error("ERROR : sendMessage() : {}", e); handleSendMessageError(message); throw new SendFailedException(addMessageIdToError(message) + "The message parameters were invalid.", e); } LOGGER.info("END : sendMessage()"); return true; }
From source file:org.jasig.ssp.service.impl.MessageServiceImpl.java
private InternetAddress[] getEmailAddresses(Person person, String type, UUID messageId) { List<InternetAddress> validAddresses = new ArrayList<InternetAddress>(); if (person.hasEmailAddresses()) { InternetAddress[] addresses = getEmailAddresses(person.getEmailAddresses(), "to:", messageId); for (InternetAddress address : addresses) { try { validAddresses.add(new InternetAddress(address.getAddress(), person.getFullName())); } catch (UnsupportedEncodingException e) { LOGGER.warn("Invalid email address found: " + address.toString() + " for " + type + "of message " + messageId, e); }/* w w w.j a v a 2s . co m*/ } } return validAddresses.toArray(new InternetAddress[validAddresses.size()]); }
From source file:org.silverpeas.core.mail.SmtpMailSendingTest.java
private void assertMailSent(MailToSend verifiedMailToSend, GreenMailOperations mail) throws Exception { assertThat("assertMailSent is compatible with one receiver only...", verifiedMailToSend.getTo(), hasSize(1));//from w ww .jav a 2 s . co m assertThat(verifiedMailToSend.getTo().getRecipientType().getTechnicalType(), is(Message.RecipientType.TO)); MimeMessage[] messages = mail.getReceivedMessages(); assertThat(messages, arrayWithSize(1)); MimeMessage sentMessage = messages[0]; MailAddress originalReceiverMailAddress = verifiedMailToSend.getTo().iterator().next(); assertThat(sentMessage.getFrom().length, is(1)); assertThat(sentMessage.getFrom()[0], instanceOf(InternetAddress.class)); InternetAddress internetAddressFrom = (InternetAddress) sentMessage.getFrom()[0]; assertThat(internetAddressFrom.getAddress(), is(verifiedMailToSend.getFrom().getEmail())); assertThat(internetAddressFrom.getPersonal(), is(StringUtil.defaultStringIfNotDefined(verifiedMailToSend.getFrom().getName(), null))); assertThat(sentMessage.getRecipients(Message.RecipientType.TO).length, is(1)); assertThat(sentMessage.getRecipients(Message.RecipientType.CC), nullValue()); assertThat(sentMessage.getRecipients(Message.RecipientType.BCC), nullValue()); assertThat(sentMessage.getRecipients(Message.RecipientType.TO)[0], instanceOf(InternetAddress.class)); InternetAddress internetAddressTo = (InternetAddress) sentMessage .getRecipients(Message.RecipientType.TO)[0]; assertThat(internetAddressTo.getAddress(), is(originalReceiverMailAddress.getEmail())); assertThat(internetAddressTo.getPersonal(), nullValue()); assertThat(sentMessage.getSubject(), is(verifiedMailToSend.getSubject())); if (verifiedMailToSend.getContent().getValue() instanceof Multipart) { assertThat(sentMessage.getContent(), instanceOf(verifiedMailToSend.getContent().getValue().getClass())); } else { assertThat(sentMessage.getContent().toString().replaceAll("[\n\r]*$", ""), is(verifiedMailToSend.getContent().getValue())); } assertThat(DateUtils.addSeconds(sentMessage.getSentDate(), 10), greaterThanOrEqualTo(new Date())); assertThat(sentMessage.getReplyTo().length, is(1)); if (verifiedMailToSend.isReplyToRequired()) { assertThat(sentMessage.getHeader("Reply-To"), notNullValue()); assertThat(sentMessage.getReplyTo()[0], instanceOf(InternetAddress.class)); InternetAddress internetAddressReplyTo = (InternetAddress) sentMessage.getReplyTo()[0]; assertThat(internetAddressReplyTo.getAddress(), is(verifiedMailToSend.getFrom().getEmail())); assertThat(internetAddressReplyTo.getPersonal(), is(StringUtil.defaultStringIfNotDefined(verifiedMailToSend.getFrom().getName(), null))); } else { assertThat(sentMessage.getHeader("Reply-To"), nullValue()); assertThat(sentMessage.getReplyTo()[0].toString(), is(internetAddressFrom.toString())); } }
From source file:org.silverpeas.core.mail.TestSmtpMailSending.java
private void assertMailSent(MailToSend verifiedMailToSend) throws Exception { assertThat("assertMailSent is compatible with one receiver only...", verifiedMailToSend.getTo(), hasSize(1));/* www . j a v a 2 s .c o m*/ assertThat(verifiedMailToSend.getTo().getRecipientType().getTechnicalType(), is(Message.RecipientType.TO)); MimeMessage[] messages = greenMailRule.getReceivedMessages(); assertThat(messages, arrayWithSize(1)); MimeMessage sentMessage = messages[0]; MailAddress originalReceiverMailAddress = verifiedMailToSend.getTo().iterator().next(); assertThat(sentMessage.getFrom().length, is(1)); assertThat(sentMessage.getFrom()[0], instanceOf(InternetAddress.class)); InternetAddress internetAddressFrom = (InternetAddress) sentMessage.getFrom()[0]; assertThat(internetAddressFrom.getAddress(), is(verifiedMailToSend.getFrom().getEmail())); assertThat(internetAddressFrom.getPersonal(), is(StringUtil.defaultStringIfNotDefined(verifiedMailToSend.getFrom().getName(), null))); assertThat(sentMessage.getRecipients(Message.RecipientType.TO).length, is(1)); assertThat(sentMessage.getRecipients(Message.RecipientType.CC), nullValue()); assertThat(sentMessage.getRecipients(Message.RecipientType.BCC), nullValue()); assertThat(sentMessage.getRecipients(Message.RecipientType.TO)[0], instanceOf(InternetAddress.class)); InternetAddress internetAddressTo = (InternetAddress) sentMessage .getRecipients(Message.RecipientType.TO)[0]; assertThat(internetAddressTo.getAddress(), is(originalReceiverMailAddress.getEmail())); assertThat(internetAddressTo.getPersonal(), nullValue()); assertThat(sentMessage.getSubject(), is(verifiedMailToSend.getSubject())); if (verifiedMailToSend.getContent().getValue() instanceof Multipart) { assertThat(sentMessage.getContent(), instanceOf(verifiedMailToSend.getContent().getValue().getClass())); } else { assertThat(sentMessage.getContent().toString().replaceAll("[\n\r]*$", ""), is(verifiedMailToSend.getContent().getValue())); } assertThat(DateUtils.addSeconds(sentMessage.getSentDate(), 10), greaterThanOrEqualTo(new Date())); assertThat(sentMessage.getReplyTo().length, is(1)); if (verifiedMailToSend.isReplyToRequired()) { assertThat(sentMessage.getHeader("Reply-To"), notNullValue()); assertThat(sentMessage.getReplyTo()[0], instanceOf(InternetAddress.class)); InternetAddress internetAddressReplyTo = (InternetAddress) sentMessage.getReplyTo()[0]; assertThat(internetAddressReplyTo.getAddress(), is(verifiedMailToSend.getFrom().getEmail())); assertThat(internetAddressReplyTo.getPersonal(), is(StringUtil.defaultStringIfNotDefined(verifiedMailToSend.getFrom().getName(), null))); } else { assertThat(sentMessage.getHeader("Reply-To"), nullValue()); assertThat(sentMessage.getReplyTo()[0].toString(), is(internetAddressFrom.toString())); } }