Example usage for javax.mail.internet MimeMessage setSubject

List of usage examples for javax.mail.internet MimeMessage setSubject

Introduction

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

Prototype

@Override
public void setSubject(String subject) throws MessagingException 

Source Link

Document

Set the "Subject" header field.

Usage

From source file:org.tomitribe.tribestream.registryng.service.monitoring.MailAlerter.java

private void sendMail(final Alert alert) throws MessagingException {
    final String subject = StrSubstitutor.replace(subjectTemplate, new HashMap<String, String>() {
        {/* w ww. java2 s .  c  o m*/
            put("hostname", hostname);
            put("date", LocalDateTime.now().toString());
        }
    });
    final String body = alert.text();

    final MimeMessage message = new MimeMessage(session);
    message.setFrom(new InternetAddress(from));
    message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
    message.setSubject(subject);
    message.setText(body);
    Transport.send(message);
}

From source file:davmail.smtp.TestSmtp.java

public void testBccMessage() throws IOException, MessagingException, InterruptedException {
    MimeMessage mimeMessage = new MimeMessage((Session) null);
    mimeMessage.addHeader("to", Settings.getProperty("davmail.to"));
    mimeMessage.setSubject("Test subject dav");
    mimeMessage.setText("Test message");
    sendAndCheckMessage(mimeMessage, Settings.getProperty("davmail.bcc"));
}

From source file:davmail.smtp.TestSmtp.java

public void testSendSimpleMessage() throws IOException, MessagingException, InterruptedException {
    String body = "Test message";
    MimeMessage mimeMessage = new MimeMessage((Session) null);
    mimeMessage.addHeader("To", Settings.getProperty("davmail.to"));
    mimeMessage.setSubject("Test subject");
    mimeMessage.setText(body);/* w  ww .  j ava 2s  . co  m*/
    sendAndCheckMessage(mimeMessage);
}

From source file:davmail.smtp.TestSmtp.java

public void testDotMessage() throws IOException, MessagingException, InterruptedException {
    String body = "First line\r\n.\r\nSecond line";
    MimeMessage mimeMessage = new MimeMessage((Session) null);
    mimeMessage.addHeader("to", Settings.getProperty("davmail.to"));
    mimeMessage.setSubject("Test subject");
    mimeMessage.setText(body);//from w ww .  ja v  a 2s.  c om
    sendAndCheckMessage(mimeMessage);
}

From source file:davmail.smtp.TestSmtp.java

public void testSendPlainTextMessage() throws IOException, MessagingException, InterruptedException {
    String body = "Test plain text message";
    MimeMessage mimeMessage = new MimeMessage((Session) null);
    mimeMessage.addHeader("To", Settings.getProperty("davmail.to"));
    mimeMessage.setSubject("Test text/plain message");
    mimeMessage.setText(body);//from   w ww .  ja v a 2s .co  m
    sendAndCheckMessage(mimeMessage);
}

From source file:davmail.smtp.TestSmtp.java

public void testSendHtmlMessage() throws IOException, MessagingException, InterruptedException {
    String body = "Test html message <font color=\"#ff0000\">red</font>";
    MimeMessage mimeMessage = new MimeMessage((Session) null);
    mimeMessage.addHeader("To", Settings.getProperty("davmail.to"));
    mimeMessage.setSubject("Test html message");
    mimeMessage.setContent(body, "text/html");
    sendAndCheckMessage(mimeMessage);/*w ww. ja v a 2 s. c o  m*/
}

From source file:eagle.common.email.EagleMailClient.java

private boolean _send(String from, String to, String cc, String title, String content,
        List<MimeBodyPart> attachments) {
    MimeMessage mail = new MimeMessage(session);
    try {//from w  w w  .j  a v  a2s  . com
        mail.setFrom(new InternetAddress(from));
        mail.setSubject(title);
        if (to != null) {
            mail.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
        }
        if (cc != null) {
            mail.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc));
        }

        //mail.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(DEFAULT_BCC_ADDRESS));

        MimeBodyPart mimeBodyPart = new MimeBodyPart();
        mimeBodyPart.setContent(content, "text/html;charset=utf-8");

        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(mimeBodyPart);

        for (MimeBodyPart attachment : attachments) {
            multipart.addBodyPart(attachment);
        }

        mail.setContent(multipart);
        //         mail.setContent(content, "text/html;charset=utf-8");
        LOG.info(String.format("Going to send mail: from[%s], to[%s], cc[%s], title[%s]", from, to, cc, title));
        Transport.send(mail);
        return true;
    } catch (AddressException e) {
        LOG.info("Send mail failed, got an AddressException: " + e.getMessage(), e);
        return false;
    } catch (MessagingException e) {
        LOG.info("Send mail failed, got an AddressException: " + e.getMessage(), e);
        return false;
    }
}

From source file:davmail.smtp.TestSmtp.java

public void testSendMessage() throws IOException, MessagingException, InterruptedException {
    String body = "Test message\r\n" + "Special characters: \r\n" + "Chinese: " + ((char) 0x604F)
            + ((char) 0x7D59);
    MimeMessage mimeMessage = new MimeMessage((Session) null);
    mimeMessage.addHeader("To", Settings.getProperty("davmail.to"));
    mimeMessage.setSubject("Test subject");
    mimeMessage.setText(body, "UTF-8");
    sendAndCheckMessage(mimeMessage);// w  w w.  j a va 2 s . c om
}

From source file:davmail.smtp.TestSmtp.java

public void testSendMessageTwice() throws IOException, MessagingException, InterruptedException {
    Settings.setProperty("davmail.smtpCheckDuplicates", "true");
    String body = "First line\r\n.\r\nSecond line";
    MimeMessage mimeMessage = new MimeMessage((Session) null);
    mimeMessage.addHeader("to", Settings.getProperty("davmail.to"));
    mimeMessage.setSubject("Test subject");
    mimeMessage.setText(body);//from  ww w  .  j a v a  2 s.co m
    sendAndCheckMessage(mimeMessage);
    sendAndCheckMessage(mimeMessage);
}

From source file:org.xwiki.mail.integration.JavaIntegrationTest.java

@Test
public void sendTextMail() throws Exception {
    // Step 1: Create a JavaMail Session
    Session session = Session.getInstance(this.configuration.getAllProperties());

    // Step 2: Create the Message to send
    MimeMessage message = new MimeMessage(session);
    message.setSubject("subject");
    message.setRecipient(RecipientType.TO, new InternetAddress("john@doe.com"));

    // Step 3: Add the Message Body
    Multipart multipart = new MimeMultipart("mixed");
    // Add text in the body
    multipart.addBodyPart(this.defaultBodyPartFactory.create("some text here",
            Collections.<String, Object>singletonMap("mimetype", "text/plain")));
    message.setContent(multipart);//from   w w  w.j  a  va 2 s .c o  m

    // We also test using some default BCC addresses from configuration in this test
    this.configuration.setBCCAddresses(Arrays.asList("bcc1@doe.com", "bcc2@doe.com"));

    // Ensure we do not reuse the same message identifier for multiple similar messages in this test
    MimeMessage message2 = new MimeMessage(message);
    message2.saveChanges();
    MimeMessage message3 = new MimeMessage(message);
    message3.saveChanges();

    // Step 4: Send the mail and wait for it to be sent
    // Send 3 mails (3 times the same mail) to verify we can send several emails at once.
    MailListener memoryMailListener = this.componentManager.getInstance(MailListener.class, "memory");
    this.sender.sendAsynchronously(Arrays.asList(message, message2, message3), session, memoryMailListener);

    // Note: we don't test status reporting from the listener since this is already tested in the
    // ScriptingIntegrationTest test class.

    // Verify that the mails have been received (wait maximum 30 seconds).
    this.mail.waitForIncomingEmail(30000L, 3);
    MimeMessage[] messages = this.mail.getReceivedMessages();

    // Note: we're receiving 9 messages since we sent 3 with 3 recipients (2 BCC and 1 to)!
    assertEquals(9, messages.length);

    // Assert the email parts that are the same for all mails
    assertEquals("subject", messages[0].getHeader("Subject", null));
    assertEquals(1, ((MimeMultipart) messages[0].getContent()).getCount());
    BodyPart textBodyPart = ((MimeMultipart) messages[0].getContent()).getBodyPart(0);
    assertEquals("text/plain", textBodyPart.getHeader("Content-Type")[0]);
    assertEquals("some text here", textBodyPart.getContent());
    assertEquals("john@doe.com", messages[0].getHeader("To", null));

    // Note: We cannot assert that the BCC worked since by definition BCC information are not visible in received
    // messages ;) But we checked that we received 9 emails above so that's good enough.
}