Search in sources :

Example 41 with Message

use of javax.mail.Message in project camel by apache.

the class MailSorterTest method testSortMessagesMulti.

@Test
public void testSortMessagesMulti() throws Exception {
    Message[] expected = new Message[] { MESSAGES[0], MESSAGES[1], MESSAGES[2] };
    // should be ignored since it is already the decider.
    for (SortTerm term1 : POSSIBLE_TERMS) {
        for (SortTerm term2 : POSSIBLE_TERMS) {
            Message[] actual = MESSAGES.clone();
            MailSorter.sortMessages(actual, new SortTerm[] { term1, SortTerm.REVERSE, term2 });
            try {
                assertArrayEquals(actual, expected);
            } catch (AssertionError ex) {
                throw new AssertionError(String.format("Terms: %s, %s", term1.toString(), term2.toString()), ex);
            }
        }
    }
}
Also used : SortTerm(com.sun.mail.imap.SortTerm) Message(javax.mail.Message) Test(org.junit.Test)

Example 42 with Message

use of javax.mail.Message in project camel by apache.

the class MailMultipleRecipientsUsingHeadersTest method testMailMultipleRecipientUsingHeaders.

@Test
public void testMailMultipleRecipientUsingHeaders() throws Exception {
    Mailbox.clearAll();
    // START SNIPPET: e1
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("To", new String[] { "davsclaus@apache.org", "janstey@apache.org" });
    map.put("From", "jstrachan@apache.org");
    map.put("Subject", "Camel rocks");
    String body = "Hello Riders.\nYes it does.\n\nRegards James.";
    template.sendBodyAndHeaders("smtp://davsclaus@apache.org", body, map);
    // END SNIPPET: e1
    Mailbox box = Mailbox.get("davsclaus@apache.org");
    Message msg = box.get(0);
    assertEquals("davsclaus@apache.org", msg.getRecipients(Message.RecipientType.TO)[0].toString());
    assertEquals("janstey@apache.org", msg.getRecipients(Message.RecipientType.TO)[1].toString());
    assertEquals("jstrachan@apache.org", msg.getFrom()[0].toString());
    assertEquals("Camel rocks", msg.getSubject());
}
Also used : Mailbox(org.jvnet.mock_javamail.Mailbox) Message(javax.mail.Message) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 43 with Message

use of javax.mail.Message in project remusic by aa112901.

the class CommonUtils method sendTextMail.

/**
     * 以文本格式发送邮件
     * @param title 待发送的邮件的信息
     */
public static boolean sendTextMail(String title, String content) {
    try {
        Properties props = System.getProperties();
        props.put("mail.smtp.host", "smtp.163.com");
        props.put("mail.smtp.auth", "true");
        Session session = Session.getInstance(props, null);
        Transport transport = session.getTransport("smtp");
        transport.connect("smtp.163.com", 25, "remusic_log@163.com", "remusiclog1");
        Message mailMessage = new SMTPMessage(session);
        Address from = new InternetAddress("remusic_log@163.com");
        mailMessage.setFrom(from);
        Address to = new InternetAddress("remusic_log@163.com");
        mailMessage.setRecipient(Message.RecipientType.TO, to);
        mailMessage.setSubject(title);
        mailMessage.setSentDate(new Date());
        mailMessage.setText(content);
        transport.sendMessage(mailMessage, mailMessage.getAllRecipients());
        return true;
    } catch (MessagingException ex) {
        ex.printStackTrace();
    }
    return false;
}
Also used : SMTPMessage(com.sun.mail.smtp.SMTPMessage) InternetAddress(javax.mail.internet.InternetAddress) Message(javax.mail.Message) SMTPMessage(com.sun.mail.smtp.SMTPMessage) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) MessagingException(javax.mail.MessagingException) Properties(java.util.Properties) Transport(javax.mail.Transport) Date(java.util.Date) Session(javax.mail.Session)

Example 44 with Message

use of javax.mail.Message in project jodd by oblac.

the class ReceiveMailSession method receive.

/**
	 * Receives all emails that matches given {@link EmailFilter filter}
	 * and set given flags. Both filter and flags to set are optional.
	 * If flags to set is not provided, it forces 'seen' flag to be unset.
	 */
public ReceivedEmail[] receive(EmailFilter filter, Flags flagsToSet) {
    if (folder == null) {
        useDefaultFolder();
    }
    Message[] messages;
    try {
        if (filter == null) {
            messages = folder.getMessages();
        } else {
            messages = folder.search(filter.getSearchTerm());
        }
        if (messages.length == 0) {
            return null;
        }
        // process messages
        ReceivedEmail[] emails = new ReceivedEmail[messages.length];
        for (int i = 0; i < messages.length; i++) {
            Message msg = messages[i];
            // we need to parse message BEFORE flags are set!
            emails[i] = new ReceivedEmail(msg);
            if (flagsToSet != null) {
                emails[i].setFlags(flagsToSet);
                msg.setFlags(flagsToSet, true);
            }
            if (flagsToSet == null && !emails[i].isSeen()) {
                msg.setFlag(Flags.Flag.SEEN, false);
            }
        }
        return emails;
    } catch (MessagingException msex) {
        throw new MailException("Failed to fetch messages", msex);
    }
}
Also used : Message(javax.mail.Message) MessagingException(javax.mail.MessagingException)

Example 45 with Message

use of javax.mail.Message in project jodd by oblac.

the class SendMailTest method testTextHtml.

@Test
public void testTextHtml() throws MessagingException, IOException {
    Email email = Email.create().from("from@example.com").to("to@example.com").subject("sub").addText("Hello!").addHtml("<html><body><h1>Hey!</h1></body></html>");
    Message message = createMessage(email);
    assertEquals(1, message.getFrom().length);
    assertEquals("from@example.com", message.getFrom()[0].toString());
    assertEquals(1, message.getRecipients(Message.RecipientType.TO).length);
    assertEquals("to@example.com", message.getRecipients(Message.RecipientType.TO)[0].toString());
    assertEquals("sub", message.getSubject());
    // wrapper
    MimeMultipart multipart = (MimeMultipart) message.getContent();
    assertEquals(1, multipart.getCount());
    assertTrue(multipart.getContentType().contains("multipart/mixed"));
    // inner content
    MimeBodyPart mimeBodyPart = (MimeBodyPart) multipart.getBodyPart(0);
    MimeMultipart mimeMultipart = (MimeMultipart) mimeBodyPart.getContent();
    assertEquals(2, mimeMultipart.getCount());
    assertTrue(mimeMultipart.getContentType().contains("multipart/alternative"));
    MimeBodyPart bodyPart = (MimeBodyPart) mimeMultipart.getBodyPart(0);
    assertEquals("Hello!", bodyPart.getContent());
    assertTrue(bodyPart.getDataHandler().getContentType().contains("text/plain"));
    bodyPart = (MimeBodyPart) mimeMultipart.getBodyPart(1);
    assertEquals("<html><body><h1>Hey!</h1></body></html>", bodyPart.getContent());
    assertTrue(bodyPart.getDataHandler().getContentType().contains("text/html"));
}
Also used : Message(javax.mail.Message) MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart) Test(org.junit.Test)

Aggregations

Message (javax.mail.Message)149 MimeMessage (javax.mail.internet.MimeMessage)81 MessagingException (javax.mail.MessagingException)53 InternetAddress (javax.mail.internet.InternetAddress)48 Folder (javax.mail.Folder)41 Test (org.junit.Test)40 Session (javax.mail.Session)37 Properties (java.util.Properties)36 Store (javax.mail.Store)28 Date (java.util.Date)19 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)19 MimeMultipart (javax.mail.internet.MimeMultipart)18 Mailbox (org.jvnet.mock_javamail.Mailbox)18 MimeBodyPart (javax.mail.internet.MimeBodyPart)16 PasswordAuthentication (javax.mail.PasswordAuthentication)15 IOException (java.io.IOException)13 ArrayList (java.util.ArrayList)13 Multipart (javax.mail.Multipart)13 Address (javax.mail.Address)11 HashMap (java.util.HashMap)8