List of usage examples for org.apache.commons.mail Email getCcAddresses
public List<InternetAddress> getCcAddresses()
From source file:org.sigmah.server.mail.DummyMailSender.java
@Override public void send(Email email) throws EmailException { if (email == null || ArrayUtils.isEmpty(email.getToAddresses())) { // does nothing. throw new EmailException("Email object null or invalid."); }//from w w w .j ava2s. c o m Assert.assertNotNull(email.getFromAddress()); Assert.assertFalse(email.getFromAddress().trim().isEmpty()); Assert.assertNotNull(email.getSubject()); Assert.assertFalse(email.getSubject().isEmpty()); Assert.assertNotNull(email.getContent()); Assert.assertFalse(email.getContent().isEmpty()); Assert.assertNotNull(email.getContentType()); Assert.assertFalse(email.getContentType().isEmpty()); Assert.assertNotNull(email.getEncoding()); Assert.assertFalse(email.getEncoding().isEmpty()); // Building the output System.out.println("From: \"" + email.getFromName() + "\" <" + email.getFromAddress() + '>'); final StringBuilder toAddressesBuilder = new StringBuilder(); for (final String address : email.getToAddresses()) { toAddressesBuilder.append(address).append(", "); } toAddressesBuilder.setLength(toAddressesBuilder.length() - 2); System.out.println("To: " + toAddressesBuilder); if (ArrayUtils.isNotEmpty(email.getCcAddresses())) { final StringBuilder ccAddressesBuilder = new StringBuilder(); for (final String address : email.getCcAddresses()) { ccAddressesBuilder.append(address).append(", "); } ccAddressesBuilder.setLength(toAddressesBuilder.length() - 2); System.out.println("Cc: " + ccAddressesBuilder); } System.out.println("Subject: " + email.getSubject()); final SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.US); System.out.println("Date: " + dateFormat.format(new Date())); System.out.println("Content-Type: " + email.getContentType() + ";charset=" + email.getEncoding()); System.out.println(); System.out.println(email.getContent()); }
From source file:org.sigmah.server.mail.MailSenderImpl.java
/** * {@inheritDoc}//from w w w . j a v a2 s . c o m */ @Override public void send(final Email email) throws EmailException { if (email == null || ArrayUtils.isEmpty(email.getToAddresses())) { // does nothing. throw new EmailException("Email object null or invalid."); } // Simple email. final SimpleEmail simpleEmail = new SimpleEmail(); // Mail content parameters. simpleEmail.setFrom(email.getFromAddress(), email.getFromName()); for (final String address : email.getToAddresses()) { simpleEmail.addTo(address); } if (ArrayUtils.isNotEmpty(email.getCcAddresses())) { for (final String address : email.getCcAddresses()) { simpleEmail.addCc(address); } } simpleEmail.setSubject(email.getSubject()); simpleEmail.setContent(email.getContent(), email.getContentType()); // Mail sending parameters. simpleEmail.setCharset(email.getEncoding()); simpleEmail.setHostName(email.getHostName()); simpleEmail.setSmtpPort(email.getSmtpPort()); // Authentication is needed. final String userName = email.getAuthenticationUserName(); final String password = email.getAuthenticationPassword(); if (userName != null && password != null) { simpleEmail.setAuthentication(userName, password); } // Sends the mail. simpleEmail.send(); }
From source file:org.sigmah.server.mail.MailSenderImpl.java
@Override public void sendFile(Email email, String fileName, InputStream fileStream) throws EmailException { final String user = email.getAuthenticationUserName(); final String password = email.getAuthenticationPassword(); final Properties properties = new Properties(); properties.setProperty(MAIL_TRANSPORT_PROTOCOL, TRANSPORT_PROTOCOL); properties.setProperty(MAIL_SMTP_HOST, email.getHostName()); properties.setProperty(MAIL_SMTP_PORT, Integer.toString(email.getSmtpPort())); final StringBuilder toBuilder = new StringBuilder(); for (final String to : email.getToAddresses()) { if (toBuilder.length() > 0) { toBuilder.append(','); }/* www . jav a 2s . c om*/ toBuilder.append(to); } final StringBuilder ccBuilder = new StringBuilder(); if (email.getCcAddresses().length > 0) { for (final String cc : email.getCcAddresses()) { if (ccBuilder.length() > 0) { ccBuilder.append(','); } ccBuilder.append(cc); } } final Session session = javax.mail.Session.getInstance(properties); try { final DataSource attachment = new ByteArrayDataSource(fileStream, FileType.fromExtension(FileType.getExtension(fileName), FileType._DEFAULT).getContentType()); final Transport transport = session.getTransport(); if (password != null) { transport.connect(user, password); } else { transport.connect(); } final MimeMessage message = new MimeMessage(session); // Configures the headers. message.setFrom(new InternetAddress(email.getFromAddress(), false)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toBuilder.toString(), false)); if (email.getCcAddresses().length > 0) { message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(ccBuilder.toString(), false)); } message.setSubject(email.getSubject(), email.getEncoding()); // Html body part. final MimeMultipart textMultipart = new MimeMultipart("alternative"); final MimeBodyPart htmlBodyPart = new MimeBodyPart(); htmlBodyPart.setContent(email.getContent(), "text/html; charset=\"" + email.getEncoding() + "\""); textMultipart.addBodyPart(htmlBodyPart); final MimeBodyPart textBodyPart = new MimeBodyPart(); textBodyPart.setContent(textMultipart); // Attachment body part. final MimeBodyPart attachmentPart = new MimeBodyPart(); attachmentPart.setDataHandler(new DataHandler(attachment)); attachmentPart.setFileName(fileName); attachmentPart.setDescription(fileName); // Mail multipart content. final MimeMultipart contentMultipart = new MimeMultipart("related"); contentMultipart.addBodyPart(textBodyPart); contentMultipart.addBodyPart(attachmentPart); message.setContent(contentMultipart); message.saveChanges(); // Sends the mail. transport.sendMessage(message, message.getAllRecipients()); } catch (UnsupportedEncodingException ex) { throw new EmailException( "An error occured while encoding the mail content to '" + email.getEncoding() + "'.", ex); } catch (IOException ex) { throw new EmailException("An error occured while reading the attachment of an email.", ex); } catch (MessagingException ex) { throw new EmailException("An error occured while sending an email.", ex); } }