@Test public void testSendEmailMessage() throws Exception { // create message with from, subject, content EmailMessage msg = new EmailMessage(from.getAddress(), subject + " with attachments", content); // add message recipients that appear in the header HashMap<RecipientType, List<EmailAddress>> tos = new HashMap<RecipientType, List<EmailAddress>>(); for (RecipientType type : headerToMap.keySet()) { ArrayList<EmailAddress> addrs = new ArrayList<EmailAddress>(); for (InternetAddress iaddr : headerToMap.get(type)) { addrs.add(new EmailAddress(iaddr.getAddress(), iaddr.getPersonal())); } tos.put(type, addrs); } // add the actual recipients LinkedList<EmailAddress> addys = new LinkedList<EmailAddress>(); for (InternetAddress t : to) { addys.add(new EmailAddress(t.getAddress(), t.getPersonal())); } tos.put(RecipientType.ACTUAL, addys); msg.setRecipients(tos); // add additional headers msg.setHeaders(additionalHeaders); // add attachments msg.setAttachments(attachments); // send message emailService.send(msg); }