Example usage for javax.mail Message setSubject

List of usage examples for javax.mail Message setSubject

Introduction

In this page you can find the example usage for javax.mail Message setSubject.

Prototype

public abstract void setSubject(String subject) throws MessagingException;

Source Link

Document

Set the subject of this message.

Usage

From source file:ste.xtest.mail.BugFreeFileTransport.java

@Test
public void send_multipart_message() throws Exception {
    Session session = Session.getInstance(config);

    Message message = new MimeMessage(Session.getInstance(config));
    message.setFrom(new InternetAddress("from@domain.com"));
    message.addRecipient(Message.RecipientType.TO, new InternetAddress("to@domain.com"));
    message.setSubject("the subject");
    MimeMultipart multipart = new MimeMultipart("related");

    BodyPart messageBodyPart = new MimeBodyPart();
    String htmlText = "<H1>hello world</H1><img src=\"cid:image\">";
    messageBodyPart.setContent(htmlText, "text/html");
    multipart.addBodyPart(messageBodyPart);
    messageBodyPart = new MimeBodyPart();
    DataSource fds = new FileDataSource("src/test/resources/images/6096.png");

    messageBodyPart.setDataHandler(new DataHandler(fds));
    messageBodyPart.setHeader("Content-ID", "<image>");

    multipart.addBodyPart(messageBodyPart);
    message.setContent(multipart);/*from www  .j a v  a 2 s.c om*/

    session.getTransport().sendMessage(message, message.getAllRecipients());

    then(FileUtils.readFileToString(new File(TMP.getRoot(), "message"))).contains("From: from@domain.com\r")
            .contains("To: to@domain.com\r").contains("Subject: the subject\r").contains("hello world")
            .contains("Content-ID: <image>");
}

From source file:com.assetmanager.service.mail.MailService.java

/**
 * Sends the activation e-mail to the given user.
 *
 * @param user the user//from  w w w. j ava  2s.  com
 * @param locale the locale
 * @throws MessagingException messaging exception
 */
public final void sendActivationEmail(final UserAccount user, final String locale) throws MessagingException {
    final Properties props = new Properties();
    final Session session = Session.getDefaultInstance(props, null);
    final Message message = new MimeMessage(session);
    final Multipart multipart = new MimeMultipart();
    final MimeBodyPart htmlPart = new MimeBodyPart();
    final MimeBodyPart textPart = new MimeBodyPart();

    message.setFrom(new InternetAddress(getFromAddress()));
    message.addRecipient(Message.RecipientType.TO, new InternetAddress(user.getEmail()));

    message.setSubject(messageSource.getMessage("mail.subject", null, new Locale(locale)));

    textPart.setContent(messageSource.getMessage("mail.body.txt",
            new Object[] { getHostname(), user.getActivationKey() }, new Locale(locale)), "text/plain");

    htmlPart.setContent(messageSource.getMessage("mail.body.html",
            new Object[] { getHostname(), user.getActivationKey() }, new Locale(locale)), "text/html");

    multipart.addBodyPart(textPart);
    multipart.addBodyPart(htmlPart);
    message.setContent(multipart);

    Transport.send(message);
}

From source file:org.sventon.mail.MailNotifier.java

/**
 * @param logEntry       Log entry//w  ww . ja  va 2  s.  c om
 * @param repositoryName Name
 * @param mailTemplate   Template
 * @return Message
 * @throws MessagingException If a message exception occurs.
 * @throws IOException        if a IO exception occurs while creating the data source.
 */
private Message createMessage(final LogEntry logEntry, RepositoryName repositoryName, String mailTemplate)
        throws MessagingException, IOException {
    final Message msg = new MimeMessage(session);
    msg.setFrom(new InternetAddress(from));
    msg.setRecipients(Message.RecipientType.BCC, receivers.toArray(new InternetAddress[receivers.size()]));
    msg.setSubject(formatSubject(subject, logEntry.getRevision(), repositoryName));

    msg.setDataHandler(
            new DataHandler(new ByteArrayDataSource(HTMLCreator.createRevisionDetailBody(mailTemplate, logEntry,
                    baseURL, repositoryName, dateFormat, null), "text/html")));

    msg.setHeader("X-Mailer", "sventon");
    msg.setSentDate(new Date());
    return msg;
}

From source file:org.geoserver.wps.mail.SendMail.java

/**
 * Send an EMail to a specified address.
 * //w  w  w  .  j  a  v a2s . c  om
 * @param address the to address
 * @param subject the email address
 * @param body message to send
 * @throws MessagingException the messaging exception
 * @throws IOException Signals that an I/O exception has occurred.
 */
public void send(String address, String subject, String body) {
    try {
        // Session session = Session.getDefaultInstance(props, null);
        Session session = Session.getDefaultInstance(props,
                (conf.getMailSmtpAuth().equalsIgnoreCase("true") ? new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(conf.getUserName(), conf.getPassword());
                    }
                } : null));

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(conf.getFromAddress(), conf.getFromAddressname()));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(address));
        message.setSubject(subject);
        message.setText(body.toString());

        Transport.send(message);

    } catch (Exception e) {
        if (LOGGER.isLoggable(Level.SEVERE)) {
            LOGGER.log(Level.SEVERE, e.getLocalizedMessage(), e);
        }
    }

}

From source file:org.sventon.repository.observer.MailNotifier.java

/**
 * @param logEntry       Log entry/*from   w  ww .j  a v  a  2  s. c  o  m*/
 * @param repositoryName Name
 * @param mailTemplate   Template
 * @return Message
 * @throws MessagingException If a message exception occurs.
 * @throws IOException        if a IO exception occurs while creating the data source.
 */
private Message createMessage(final SVNLogEntry logEntry, RepositoryName repositoryName, String mailTemplate)
        throws MessagingException, IOException {
    final Message msg = new MimeMessage(session);
    msg.setFrom(new InternetAddress(from));
    msg.setRecipients(Message.RecipientType.BCC, receivers.toArray(new InternetAddress[receivers.size()]));
    msg.setSubject(formatSubject(subject, logEntry.getRevision(), repositoryName));

    msg.setDataHandler(
            new DataHandler(new ByteArrayDataSource(HTMLCreator.createRevisionDetailBody(mailTemplate, logEntry,
                    baseUrl, repositoryName, dateFormat, null), "text/html")));

    msg.setHeader("X-Mailer", "sventon");
    msg.setSentDate(new Date());
    return msg;
}

From source file:com.meg7.emailer.EmailerManager.java

private Message createMessage(List<String> emails, String fromEmail, String fromName, String subject,
        String text, Session session) throws MessagingException, UnsupportedEncodingException {
    Message message = new MimeMessage(session);
    message.setFrom(new InternetAddress(fromEmail, fromName));
    for (String email : emails) {
        message.addRecipient(Message.RecipientType.BCC, new InternetAddress(email, email));
    }/*from w  ww.j a  v a2  s . c  o  m*/
    message.setSubject(subject);
    message.setText(text);
    return message;
}

From source file:com.app.mail.DefaultMailSender.java

private Message _populateMessage(String emailAddress, String subject, String template, Session session)
        throws Exception {

    Message message = new MimeMessage(session);

    message.setFrom(new InternetAddress(PropertiesValues.OUTBOUND_EMAIL_ADDRESS, "Auction Alert"));

    message.addRecipient(Message.RecipientType.TO, new InternetAddress(emailAddress));

    message.setSubject(subject);

    Map<String, Object> rootMap = new HashMap<>();

    rootMap.put("rootDomainName", PropertiesValues.ROOT_DOMAIN_NAME);

    String messageBody = VelocityEngineUtils.mergeTemplateIntoString(_velocityEngine, "template/" + template,
            "UTF-8", rootMap);

    message.setContent(messageBody, "text/html");

    return message;
}

From source file:com.app.mail.DefaultMailSender.java

private Message _populatePasswordResetToken(String emailAddress, String passwordResetToken, Session session)
        throws Exception {

    Message message = new MimeMessage(session);

    message.setFrom(new InternetAddress(PropertiesValues.OUTBOUND_EMAIL_ADDRESS, "Auction Alert"));

    message.addRecipient(Message.RecipientType.TO, new InternetAddress(emailAddress));

    message.setSubject("Password Reset Token");

    Map<String, Object> rootMap = new HashMap<>();

    rootMap.put("passwordResetToken", passwordResetToken);
    rootMap.put("rootDomainName", PropertiesValues.ROOT_DOMAIN_NAME);

    String messageBody = VelocityEngineUtils.mergeTemplateIntoString(_velocityEngine,
            "template/password_token.vm", "UTF-8", rootMap);

    message.setContent(messageBody, "text/html");

    return message;
}

From source file:org.nuxeo.labs.operations.notification.AdvancedSendEmail.java

protected void send(DocumentModel doc) throws Exception {
    try {// w  ww. j  a v a 2  s.  c  om
        Message msg = createContentMessage(doc);
        addToEmails(msg);
        addFromEmails(msg);
        addCcEmails(msg);
        addBccEmails(msg);
        addReplyToEmails(msg);
        msg.setSubject(subject);
        msg.send();
    } catch (Exception e) {
        if (rollbackOnError) {
            throw e;
        } else {
            log.warn(String.format(WARN_ON_SEND_EXCEPTION, ID), e);
        }
    }
}

From source file:org.openmrs.module.reporting.report.processor.EmailReportProcessor.java

/**
 * Performs some action on the given report
 * @param report the Report to process/*from   w w w.ja  va2s  .co  m*/
 */
public void process(Report report, Properties configuration) {

    try {
        Message m = new MimeMessage(getSession());

        m.setFrom(new InternetAddress(configuration.getProperty("from")));
        for (String recipient : configuration.getProperty("to", "").split("\\,")) {
            m.addRecipient(RecipientType.TO, new InternetAddress(recipient));
        }

        // TODO: Make these such that they can contain report information
        m.setSubject(configuration.getProperty("subject"));

        Multipart multipart = new MimeMultipart();

        MimeBodyPart contentBodyPart = new MimeBodyPart();
        String content = configuration.getProperty("content", "");
        if (report.getRenderedOutput() != null
                && "true".equalsIgnoreCase(configuration.getProperty("addOutputToContent"))) {
            content += new String(report.getRenderedOutput());
        }
        contentBodyPart.setContent(content, "text/html");
        multipart.addBodyPart(contentBodyPart);

        if (report.getRenderedOutput() != null
                && "true".equalsIgnoreCase(configuration.getProperty("addOutputAsAttachment"))) {
            MimeBodyPart attachment = new MimeBodyPart();
            Object output = report.getRenderedOutput();
            if (report.getOutputContentType().contains("text")) {
                output = new String(report.getRenderedOutput(), "UTF-8");
            }
            attachment.setDataHandler(new DataHandler(output, report.getOutputContentType()));
            attachment.setFileName(configuration.getProperty("attachmentName"));
            multipart.addBodyPart(attachment);
        }

        m.setContent(multipart);

        Transport.send(m);
    } catch (Exception e) {
        throw new RuntimeException("Error occurred while sending report over email", e);
    }
}