List of usage examples for org.apache.commons.mail Email setSubject
public Email setSubject(final String aSubject)
From source file:org.pepstock.jem.notify.engine.EmailNotifier.java
/** * This method sends an <code>Email</code>. <br> * It sets in the parameter <code>Email</code> the properties of the * parameter <code>JemEmail</code>: From User Email Address, From User Name, * subject, text, email destination addresses. <br> * It sets in the parameter <code>Email</code> the Email Server property and * the optional <code>SMTP</code> port, the properties that indicates if it * must use <code>SSL</code> and <code>TLS</code> protocol, the useirid and * password for <code>SMTP</code> server authentication if needed, the * optional bounce address, the subject, the text, and the recipients of the * email.//from w w w . j ava 2s. c o m * * @param email the <code>JemEmail</code> with the properties of the email * to be sent. * @param sendingEmail the real <code>Email</code> that will be sent. * @see JemEmail * @see Email * @throws SendMailException if an error occurs. */ private void sendEmail(JemEmail email, Email sendingEmail) throws SendMailException { try { sendingEmail.setHostName(this.emailServer); if (this.smtpPort != NO_SMTP_PORT) { sendingEmail.setSmtpPort(this.smtpPort); } sendingEmail.setFrom(email.getFromUserEmailAddress(), email.getFromUserName()); if (email.hasSubject()) { sendingEmail.setSubject(email.getSubject()); } else { // log no subject LogAppl.getInstance().emit(NotifyMessage.JEMN014W, "Subject"); } if (email.hasText()) { sendingEmail.setMsg(email.getText()); } else { // log no text message LogAppl.getInstance().emit(NotifyMessage.JEMN014W, "Text Message"); } sendingEmail.setTo(email.getAllToEmailAddresses()); if (null != this.bounceAddress) { sendingEmail.setBounceAddress(this.bounceAddress); } sendingEmail.setSentDate(new Date()); sendingEmail.setSSL(this.isSSL); sendingEmail.setTLS(this.isTLS); if (null != this.authenticationUserId && null != this.authenticationPassword) { sendingEmail.setAuthenticator( new DefaultAuthenticator(this.authenticationUserId, this.authenticationPassword)); } sendingEmail.send(); LogAppl.getInstance().emit(NotifyMessage.JEMN015I, email); } catch (EmailException eEx) { LogAppl.getInstance().emit(NotifyMessage.JEMN016E, eEx, email); throw new SendMailException(NotifyMessage.JEMN016E.toMessage().getFormattedMessage(email), eEx); } }
From source file:org.ploin.pmf.impl.MailSender.java
private void setMailProperties(Email email, MailConfig mailConfig) throws MailFactoryException { try {//from w ww. j av a 2 s. co m email.setSubject(mailConfig.getSubject()); for (Recipient toRecipient : mailConfig.getToRecipients()) { if (toRecipient.getName() != null) { email.addTo(toRecipient.getEmail(), toRecipient.getName()); } else { email.addTo(toRecipient.getEmail()); } } if (!mailConfig.isCcRecipientEmpty()) { for (Recipient ccRecipient : mailConfig.getCcRecipients()) { if (ccRecipient.getName() != null) { email.addCc(ccRecipient.getEmail(), ccRecipient.getName()); } else { email.addCc(ccRecipient.getEmail()); } } } if (!mailConfig.isBccRecipientEmpty()) { for (Recipient bccRecipient : mailConfig.getBccRecipients()) { if (bccRecipient.getName() != null) { email.addBcc(bccRecipient.getEmail(), bccRecipient.getName()); } else { email.addBcc(bccRecipient.getEmail()); } } } } catch (Exception e) { throw new MailFactoryException(e); } }
From source file:org.sakaiproject.kernel.messaging.activemq.ActiveMQEmailDeliveryT.java
public void testCommonsEmailOneWaySeparateSessions() { Queue emailQueue = null;//from w w w . ja v a 2s.co m MessageConsumer consumer = null; MessageProducer producer = null; Session clientSession = null; Session listenerSession = null; // it is not necessary to use the Email interface here // Email is used here just to allow for multiple types of emails to // occupy // the same varaible. SimpleEmail etc can each be used directly. List<Email> emails = new ArrayList<Email>(); EmailMessagingService messagingService = new EmailMessagingService(vmURL, emailQueueName, emailType, null, null, null, null); emails.add(new SimpleEmail(messagingService)); emails.add(new MultiPartEmail(messagingService)); emails.add(new HtmlEmail(messagingService)); try { listenerSession = listenerConn.createSession(false, Session.AUTO_ACKNOWLEDGE); emailQueue = listenerSession.createQueue(emailQueueName); consumer = listenerSession.createConsumer(emailQueue); consumer.setMessageListener(new EmailListener()); listenerConn.start(); listenerSession.run(); } catch (JMSException e2) { e2.printStackTrace(); Assert.assertTrue(false); } Wiser smtpServer = new Wiser(); smtpServer.setPort(smtpTestPort); smtpServer.start(); try { clientSession = clientConn.createSession(false, Session.AUTO_ACKNOWLEDGE); emailQueue = clientSession.createQueue(emailQueueName); producer = clientSession.createProducer(emailQueue); clientConn.start(); clientSession.run(); } catch (JMSException e) { e.printStackTrace(); Assert.assertTrue(false); } for (Email em : emails) { try { em.addTo(TEST_EMAIL_TO); em.setFrom(TEST_EMAIL_FROM_ADDRESS, TEST_EMAIL_FROM_LABEL); // host and port will be ignored since the email session is // established // by // the listener em.setHostName("localhost"); em.setSmtpPort(smtpTestPort); em.setSubject(TEST_EMAIL_SUBJECT); if (em instanceof HtmlEmail) { em.setMsg(TEST_EMAIL_BODY_HTMLEMAIL); } else if (em instanceof MultiPartEmail) { em.setMsg(TEST_EMAIL_BODY_MULTIPARTEMAIL); } else if (em instanceof SimpleEmail) { em.setMsg(TEST_EMAIL_BODY_SIMPLEEMAIL); } } catch (EmailException e1) { Assert.assertTrue(false); e1.printStackTrace(); } try { em.buildMimeMessage(); } catch (EmailException e1) { e1.printStackTrace(); Assert.assertTrue(false); } ByteArrayOutputStream os = new ByteArrayOutputStream(); try { em.getMimeMessage().writeTo(os); } catch (javax.mail.MessagingException e) { e.printStackTrace(); Assert.assertTrue(false); } catch (IOException e) { e.printStackTrace(); Assert.assertTrue(false); } String content = os.toString(); ObjectMessage om; try { om = clientSession.createObjectMessage(content); om.setJMSType(emailType); LOG.info("Client: Sending test message...."); producer.send(om); } catch (JMSException e) { e.printStackTrace(); Assert.assertTrue(false); } } long start = System.currentTimeMillis(); while (listenerMessagesProcessed < 3 && System.currentTimeMillis() - start < 10000L) { // wait for transport } Assert.assertTrue(listenerMessagesProcessed == 3); List<WiserMessage> messages = smtpServer.getMessages(); Assert.assertTrue(messages.size() + " != expected value of 3", messages.size() == 3); for (WiserMessage wisermsg : messages) { String body = null; String subject = null; MimeMessage testmail = null; try { testmail = wisermsg.getMimeMessage(); } catch (MessagingException e) { Assert.assertTrue(false); e.printStackTrace(); } if (testmail != null) { LOG.info("SMTP server: test email received: "); try { LOG.info("To: " + testmail.getHeader("To", ",")); LOG.info("Subject: " + testmail.getHeader("Subject", ",")); body = getBodyAsString(testmail.getContent()); subject = testmail.getHeader("Subject", ","); } catch (MessagingException e) { Assert.assertTrue(false); e.printStackTrace(); } catch (IOException e) { Assert.assertTrue(false); e.printStackTrace(); } LOG.info("Body: " + body); Assert.assertTrue(subject.contains(TEST_EMAIL_SUBJECT)); Assert.assertTrue(body.contains("This is a Commons")); } else { Assert.assertTrue(false); } } if (clientSession != null) { try { clientSession.close(); } catch (JMSException e) { e.printStackTrace(); Assert.assertTrue(false); } clientSession = null; } if (listenerSession != null) { try { listenerSession.close(); } catch (JMSException e) { e.printStackTrace(); Assert.assertTrue(false); } listenerSession = null; } smtpServer.stop(); }
From source file:org.sonatype.nexus.internal.email.EmailManagerImpl.java
/** * Apply server configuration to email./*from w w w . jav a2s . c o m*/ */ @VisibleForTesting Email apply(final EmailConfiguration configuration, final Email mail) throws EmailException { mail.setHostName(configuration.getHost()); mail.setSmtpPort(configuration.getPort()); mail.setAuthentication(configuration.getUsername(), configuration.getPassword()); mail.setStartTLSEnabled(configuration.isStartTlsEnabled()); mail.setStartTLSRequired(configuration.isStartTlsRequired()); mail.setSSLOnConnect(configuration.isSslOnConnectEnabled()); mail.setSSLCheckServerIdentity(configuration.isSslCheckServerIdentityEnabled()); mail.setSslSmtpPort(Integer.toString(configuration.getPort())); // default from address if (mail.getFromAddress() == null) { mail.setFrom(configuration.getFromAddress()); } // apply subject prefix if configured String subjectPrefix = configuration.getSubjectPrefix(); if (subjectPrefix != null) { String subject = mail.getSubject(); mail.setSubject(String.format("%s %s", subjectPrefix, subject)); } // do this last (mail properties are set up from the email fields when you get the mail session) if (configuration.isNexusTrustStoreEnabled()) { SSLContext context = trustStore.getSSLContext(); Session session = mail.getMailSession(); Properties properties = session.getProperties(); properties.remove(EmailConstants.MAIL_SMTP_SOCKET_FACTORY_CLASS); properties.put(EmailConstants.MAIL_SMTP_SSL_ENABLE, true); properties.put("mail.smtp.ssl.socketFactory", context.getSocketFactory()); } return mail; }
From source file:org.sonatype.nexus.internal.email.EmailManagerImpl.java
@Override public void sendVerification(final EmailConfiguration configuration, final String address) throws EmailException { checkNotNull(configuration);/* w w w .java2 s . c o m*/ checkNotNull(address); Email mail = new SimpleEmail(); mail.setSubject("Email configuration verification"); mail.addTo(address); mail.setMsg("Verification successful"); mail = apply(configuration, mail); mail.send(); }
From source file:org.sonatype.nexus.internal.scheduling.NexusTaskFailureAlertEmailSender.java
private void sendEmail(final String address, final String taskId, final String taskName, final Throwable cause) throws Exception { Email mail = new SimpleEmail(); mail.setSubject("Task execution failure"); mail.addTo(address);/*w w w .j av a 2s .c o m*/ // FIXME: This should ideally render a user-configurable template StringWriter buff = new StringWriter(); PrintWriter out = new PrintWriter(buff); if (taskId != null) { out.format("Task ID: %s%n", taskId); } if (taskName != null) { out.format("Task Name: %s%n", taskName); } if (cause != null) { out.println("Stack-trace:"); cause.printStackTrace(out); } mail.setMsg(buff.toString()); emailManager.get().send(mail); }
From source file:org.structr.common.MailHelper.java
private static void setup(final Email mail, final String to, final String toName, final String from, final String fromName, final String cc, final String bcc, final String bounce, final String subject) throws EmailException { // FIXME: this might be slow if the config file is read each time final Properties config = Services.getInstance().getCurrentConfig(); final String smtpHost = config.getProperty(Services.SMTP_HOST, "localhost"); final String smtpPort = config.getProperty(Services.SMTP_PORT, "25"); final String smtpUser = config.getProperty(Services.SMTP_USER); final String smtpPassword = config.getProperty(Services.SMTP_PASSWORD); final String smtpUseTLS = config.getProperty(Services.SMTP_USE_TLS, "true"); final String smtpRequireTLS = config.getProperty(Services.SMTP_REQUIRE_TLS, "false"); mail.setCharset(charset);/*from ww w.j a va2 s. com*/ mail.setHostName(smtpHost); mail.setSmtpPort(Integer.parseInt(smtpPort)); mail.setStartTLSEnabled(Boolean.parseBoolean(smtpUseTLS)); mail.setStartTLSRequired(Boolean.parseBoolean(smtpRequireTLS)); mail.setCharset(charset); mail.setHostName(smtpHost); mail.setSmtpPort(Integer.parseInt(smtpPort)); if (StringUtils.isNotBlank(smtpUser) && StringUtils.isNotBlank(smtpPassword)) { mail.setAuthentication(smtpUser, smtpPassword); } mail.addTo(to, toName); mail.setFrom(from, fromName); if (StringUtils.isNotBlank(cc)) { mail.addCc(cc); } if (StringUtils.isNotBlank(bcc)) { mail.addBcc(bcc); } if (StringUtils.isNotBlank(bounce)) { mail.setBounceAddress(bounce); } mail.setSubject(subject); }
From source file:org.thousandturtles.gmail_demo.Main.java
public static void main(String[] args) { MailInfoRetriever retriever = new CLIMailInfoRetriever(); MailInfo mailInfo = retriever.retrieveMailInfo(); if (mailInfo.isNoAuth()) { System.out.println("Using No auth, mail with aspmx.l.google.com"); } else {/* w w w.jav a2s.c o m*/ System.out.printf("Username: %s%n", mailInfo.getUsername()); System.out.printf("Password: %c***%c%n", mailInfo.getPassword().charAt(0), mailInfo.getPassword().charAt(mailInfo.getPassword().length() - 1)); } System.out.printf("Mail target: %s%n", mailInfo.getMailTo()); try { Email mail = new SimpleEmail(); if (mailInfo.isNoAuth()) { mail.setHostName("aspmx.l.google.com"); mail.setSmtpPort(25); mail.setFrom("no-reply@thousandturtles.org"); } else { mail.setHostName("smtp.gmail.com"); mail.setSmtpPort(465); mail.setAuthentication(mailInfo.getUsername(), mailInfo.getPassword()); mail.setSSLOnConnect(true); mail.setFrom(mailInfo.getMailer()); } mail.setCharset("UTF-8"); mail.addTo(mailInfo.getMailTo(), "White Mouse"); mail.setSubject(""); mail.setMsg("?\nThis is a test mail!\n"); mail.send(); System.out.println("Mail sent successfully."); } catch (EmailException ee) { ee.printStackTrace(); } }
From source file:org.vas.mail.MailWorker.java
void send(Mail mail) { if (logger.isTraceEnabled()) { logger.trace("New mail to send - {}", mail.subject); }/* w w w . j av a 2 s . com*/ Email email = smtp.emptyEmail(); email.setSubject(mail.subject); try { email.setFrom(mail.from); email.setTo(Arrays.asList(new InternetAddress(mail.to))); email.setMsg(mail.body); if (logger.isDebugEnabled()) { logger.debug("Send mail {}", mail.subject); } email.send(); } catch (EmailException | AddressException e) { throw new RuntimeException(e); } }
From source file:org.xerela.server.birt.ReportJob.java
private void setupEmail(JobExecutionContext executionContext, String emailTo, String emailCc, Email email) throws AddressException, EmailException { InternetAddress[] toAddrs = InternetAddress.parse(emailTo); email.setTo(Arrays.asList(toAddrs)); if (emailCc != null && emailCc.trim().length() > 0) { InternetAddress[] ccAddrs = InternetAddress.parse(emailCc); email.setCc(Arrays.asList(ccAddrs)); }//from w ww .j a v a 2 s. c o m email.setCharset("utf-8"); //$NON-NLS-1$ email.setHostName(System.getProperty(MAIL_HOST_PROP, "mail")); //$NON-NLS-1$ String authUser = System.getProperty(MAIL_AUTH_USER_PROP); if (authUser != null) { email.setAuthentication(authUser, System.getProperty(MAIL_AUTH_PASSWORD_PROP)); } email.setFrom(System.getProperty(MAIL_FROM_PROP), System.getProperty(MAIL_FROM_NAME_PROP)); email.setSubject( Messages.bind(Messages.ReportJob_emailSubject, executionContext.getJobDetail().getFullName())); email.addHeader("X-Mailer", "Xerela Mailer"); //$NON-NLS-1$ //$NON-NLS-2$ email.setDebug(Boolean.getBoolean("org.xerela.mail.debug")); //$NON-NLS-1$ }