Search in sources :

Example 81 with Message

use of javax.mail.Message in project vcell by virtualcell.

the class ListservMail method getBouncedEmails.

private static void getBouncedEmails(String emailPassword) throws Exception {
    IMAPSSLStore store = null;
    IMAPFolder[] inboxs = null;
    BufferedWriter bw = null;
    try {
        // connect to mail server using imaps protocol
        Properties properties = System.getProperties();
        Session session = Session.getDefaultInstance(properties);
        // Use this instead of pop3s to access non 'Inbox' folders
        store = (IMAPSSLStore) session.getStore("imaps");
        store.connect(mailServer, mailUser, emailPassword);
        inboxs = new IMAPFolder[] { (IMAPFolder) store.getFolder(mailFolder) };
        inboxs[0].open(Folder.READ_ONLY);
        // get the list of messages from folder
        Message[] messages = inboxs[0].getMessages();
        if (messages.length == 0)
            System.out.println("No messages found.");
        for (int i = 0; i < messages.length; i++) {
            // // stop after listing ten messages
            // if (i > 10) {
            // break;
            // }
            System.out.println("Message " + (i + 1));
            IMAPMessage message = (IMAPMessage) messages[i];
            System.out.println("From : " + message.getFrom()[0]);
            System.out.println("Subject : " + message.getSubject());
            System.out.println("Sent Date : " + message.getSentDate());
            System.out.println("Type : " + message.getContentType());
            if (message.getSubject().contains(subjectSearchForBounced)) {
                readMessage(message, 0, 0);
            }
            System.out.println();
        }
        System.out.println("Found " + bouncedEMails.size() + " bounced emails");
        bw = new BufferedWriter(new FileWriter(rawBounceFile));
        for (String s : bouncedEMails) {
            bw.write(s + "\n");
        }
    } finally {
        if (bw != null) {
            try {
                bw.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (inboxs != null) {
            for (int i = 0; i < inboxs.length; i++) {
                if (inboxs[i] != null && inboxs[i].isOpen()) {
                    try {
                        inboxs[i].close(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        if (store != null) {
            try {
                store.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : Message(javax.mail.Message) IMAPNestedMessage(com.sun.mail.imap.IMAPNestedMessage) IMAPMessage(com.sun.mail.imap.IMAPMessage) IMAPFolder(com.sun.mail.imap.IMAPFolder) FileWriter(java.io.FileWriter) Properties(java.util.Properties) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) BufferedWriter(java.io.BufferedWriter) IMAPSSLStore(com.sun.mail.imap.IMAPSSLStore) IMAPMessage(com.sun.mail.imap.IMAPMessage) Session(javax.mail.Session)

Example 82 with Message

use of javax.mail.Message in project quickstart by wildfly.

the class Email method send.

/**
 * Method to send the email based upon values entered in the JSF view. Exception should be handled in a production usage but
 * is not handled in this example.
 *
 * @throws Exception
 */
public void send() throws Exception {
    Message message = new MimeMessage(mySession);
    message.setFrom(new InternetAddress(from));
    Address toAddress = new InternetAddress(to);
    message.addRecipient(Message.RecipientType.TO, toAddress);
    message.setSubject(subject);
    message.setContent(body, "text/plain");
    Transport.send(message);
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage)

Example 83 with Message

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

the class MailIdempotentRepositoryDuplicateTest method prepareMailbox.

private void prepareMailbox() throws Exception {
    // connect to mailbox
    Mailbox.clearAll();
    JavaMailSender sender = new DefaultJavaMailSender();
    Store store = sender.getSession().getStore("pop3");
    store.connect("localhost", 25, "jones", "secret");
    Folder folder = store.getFolder("INBOX");
    folder.open(Folder.READ_WRITE);
    folder.expunge();
    // inserts 5 new messages
    Message[] messages = new Message[5];
    for (int i = 0; i < 5; i++) {
        messages[i] = new MimeMessage(sender.getSession());
        messages[i].setText("Message " + i);
        messages[i].setHeader("Message-ID", "myuid-" + i);
    }
    folder.appendMessages(messages);
    folder.close(true);
}
Also used : Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MimeMessage(javax.mail.internet.MimeMessage) Store(javax.mail.Store) Folder(javax.mail.Folder) MockEndpoint(org.apache.camel.component.mock.MockEndpoint)

Example 84 with Message

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

the class MailMaxMessagesPerPollTest method prepareMailbox.

private void prepareMailbox() throws Exception {
    // connect to mailbox
    Mailbox.clearAll();
    JavaMailSender sender = new DefaultJavaMailSender();
    Store store = sender.getSession().getStore("pop3");
    store.connect("localhost", 25, "jones", "secret");
    Folder folder = store.getFolder("INBOX");
    folder.open(Folder.READ_WRITE);
    folder.expunge();
    // inserts 5 new messages
    Message[] messages = new Message[5];
    for (int i = 0; i < 5; i++) {
        messages[i] = new MimeMessage(sender.getSession());
        messages[i].setHeader("Message-ID", "" + i);
        messages[i].setText("Message " + i);
    }
    folder.appendMessages(messages);
    folder.close(true);
}
Also used : Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MimeMessage(javax.mail.internet.MimeMessage) Store(javax.mail.Store) Folder(javax.mail.Folder) MockEndpoint(org.apache.camel.component.mock.MockEndpoint)

Example 85 with Message

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

the class MailBatchConsumerTest method prepareMailbox.

private void prepareMailbox() throws Exception {
    // connect to mailbox
    Mailbox.clearAll();
    JavaMailSender sender = new DefaultJavaMailSender();
    Store store = sender.getSession().getStore("pop3");
    store.connect("localhost", 25, "jones", "secret");
    Folder folder = store.getFolder("INBOX");
    folder.open(Folder.READ_WRITE);
    folder.expunge();
    // inserts 5 new messages
    Message[] messages = new Message[5];
    for (int i = 0; i < 5; i++) {
        messages[i] = new MimeMessage(sender.getSession());
        messages[i].setHeader("Message-ID", "" + i);
        messages[i].setText("Message " + i);
    }
    folder.appendMessages(messages);
    folder.close(true);
}
Also used : Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MimeMessage(javax.mail.internet.MimeMessage) Store(javax.mail.Store) Folder(javax.mail.Folder) MockEndpoint(org.apache.camel.component.mock.MockEndpoint)

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