Search in sources :

Example 1 with MimeMessage

use of javax.mail.internet.MimeMessage in project Openfire by igniterealtime.

the class SendMail method sendMessage.

public boolean sendMessage(String message, String host, String port, String username, String password) {
    boolean ok = false;
    String uidString = "";
    try {
        // Set the email properties necessary to send email
        final Properties props = System.getProperties();
        props.put("mail.smtp.host", host);
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.server", host);
        if (ModelUtil.hasLength(port)) {
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.port", port);
        }
        Session sess;
        if (ModelUtil.hasLength(password) && ModelUtil.hasLength(username)) {
            sess = Session.getInstance(props, new MailAuthentication(username, password));
        } else {
            sess = Session.getDefaultInstance(props, null);
        }
        Message msg = new MimeMessage(sess);
        StringTokenizer toST = new StringTokenizer(toField, ",");
        if (toST.countTokens() > 1) {
            InternetAddress[] address = new InternetAddress[toST.countTokens()];
            int addrIndex = 0;
            String addrString = "";
            while (toST.hasMoreTokens()) {
                addrString = toST.nextToken();
                address[addrIndex] = (new InternetAddress(addrString));
                addrIndex = addrIndex + 1;
            }
            msg.setRecipients(Message.RecipientType.TO, address);
        } else {
            InternetAddress[] address = { new InternetAddress(toField) };
            msg.setRecipients(Message.RecipientType.TO, address);
        }
        InternetAddress from = new InternetAddress(myAddress);
        msg.setFrom(from);
        msg.setSubject(subjectField);
        UID msgUID = new UID();
        uidString = msgUID.toString();
        msg.setHeader("X-Mailer", uidString);
        msg.setSentDate(new Date());
        MimeMultipart mp = new MimeMultipart();
        // create body part for textarea
        MimeBodyPart mbp1 = new MimeBodyPart();
        if (getCustomerName() != null) {
            messageText = "From: " + getCustomerName() + "\n" + messageText;
        }
        if (isHTML) {
            mbp1.setContent(messageText, "text/html");
        } else {
            mbp1.setContent(messageText, "text/plain");
        }
        mp.addBodyPart(mbp1);
        try {
            if (!isHTML) {
                msg.setContent(messageText, "text/plain");
            } else {
                msg.setContent(messageText, "text/html");
            }
            Transport.send(msg);
            ok = true;
        } catch (SendFailedException sfe) {
            Log.warn("Could not connect to SMTP server.");
        }
    } catch (Exception eq) {
        Log.warn("Could not connect to SMTP server.");
    }
    return ok;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) SendFailedException(javax.mail.SendFailedException) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) Properties(java.util.Properties) Date(java.util.Date) SendFailedException(javax.mail.SendFailedException) UID(java.rmi.server.UID) StringTokenizer(java.util.StringTokenizer) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart) Session(javax.mail.Session)

Example 2 with MimeMessage

use of javax.mail.internet.MimeMessage in project camel by apache.

the class MailConsumerUnsupportedCharsetTest method prepareMailbox.

private void prepareMailbox() throws Exception {
    // connect to mailbox
    Mailbox.clearAll();
    JavaMailSender sender = new DefaultJavaMailSender();
    Store store = sender.getSession().getStore("imap");
    store.connect("localhost", 25, "jones", "secret");
    Folder folder = store.getFolder("INBOX");
    folder.open(Folder.READ_WRITE);
    folder.expunge();
    Message[] msg = new Message[1];
    MimeMessage mime = new MimeMessage(sender.getSession());
    mime.setContent("Bye World", "text/plain; charset=ThisIsNotAKnownCharset");
    msg[0] = mime;
    folder.appendMessages(msg);
    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)

Example 3 with MimeMessage

use of javax.mail.internet.MimeMessage in project camel by apache.

the class MailDoNotDeleteIfProcessFailsTest method prepareMailbox.

private void prepareMailbox() throws Exception {
    // connect to mailbox
    Mailbox.clearAll();
    JavaMailSender sender = new DefaultJavaMailSender();
    Store store = sender.getSession().getStore("imap");
    store.connect("localhost", 25, "claus", "secret");
    Folder folder = store.getFolder("INBOX");
    folder.open(Folder.READ_WRITE);
    folder.expunge();
    // inserts two new messages
    Message[] msg = new Message[2];
    msg[0] = new MimeMessage(sender.getSession());
    msg[0].setText("Message 1");
    msg[0].setHeader("Message-ID", "0");
    msg[0].setFlag(Flags.Flag.SEEN, false);
    msg[1] = new MimeMessage(sender.getSession());
    msg[1].setText("Message 2");
    msg[0].setHeader("Message-ID", "1");
    msg[1].setFlag(Flags.Flag.SEEN, true);
    folder.appendMessages(msg);
    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)

Example 4 with MimeMessage

use of javax.mail.internet.MimeMessage in project camel by apache.

the class NestedMimeMessageConsumeTest method prepareMailbox.

private void prepareMailbox(String user) throws MessagingException {
    // connect to mailbox
    JavaMailSender sender = new DefaultJavaMailSender();
    Store store = sender.getSession().getStore("pop3");
    store.connect("localhost", 25, user, "secret");
    Folder folder = store.getFolder("INBOX");
    folder.open(Folder.READ_WRITE);
    folder.expunge();
    InputStream is = getClass().getResourceAsStream("/nested-multipart.elm");
    Message hurzMsg = new MimeMessage(sender.getSession(), is);
    Message[] messages = new Message[] { hurzMsg };
    // insert one signed message
    folder.appendMessages(messages);
    folder.close(true);
}
Also used : Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MimeMessage(javax.mail.internet.MimeMessage) InputStream(java.io.InputStream) Store(javax.mail.Store) Folder(javax.mail.Folder)

Example 5 with MimeMessage

use of javax.mail.internet.MimeMessage in project camel by apache.

the class SearchTermBuilderTest method testSearchTermSentLast24Hours.

public void testSearchTermSentLast24Hours() throws Exception {
    SearchTermBuilder build = new SearchTermBuilder();
    long offset = -1 * (24 * 60 * 60 * 1000L);
    SearchTerm st = build.subject("Camel").sentNow(Comparison.GE, offset).build();
    assertNotNull(st);
    // create dummy message
    Mailbox.clearAll();
    JavaMailSender sender = new DefaultJavaMailSender();
    MimeMessage msg = new MimeMessage(sender.getSession());
    msg.setSubject("Yeah Camel rocks");
    msg.setText("Apache Camel is a cool project. Have a fun ride.");
    msg.setFrom(new InternetAddress("someone@somewhere.com"));
    msg.setSentDate(new Date());
    assertTrue("Should match message", st.match(msg));
    MimeMessage msg2 = new MimeMessage(sender.getSession());
    msg2.setSubject("Camel in Action");
    msg2.setText("Hey great book");
    msg2.setFrom(new InternetAddress("dude@apache.org"));
    // mark it as sent 2 days ago
    long twoDays = 2 * 24 * 60 * 60 * 1000L;
    long time = new Date().getTime() - twoDays;
    msg2.setSentDate(new Date(time));
    assertFalse("Should not match message as its too old", st.match(msg2));
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage) SearchTerm(javax.mail.search.SearchTerm) Date(java.util.Date)

Aggregations

MimeMessage (javax.mail.internet.MimeMessage)1146 Test (org.junit.Test)374 InternetAddress (javax.mail.internet.InternetAddress)334 MessagingException (javax.mail.MessagingException)299 Session (javax.mail.Session)222 Properties (java.util.Properties)219 MimeMultipart (javax.mail.internet.MimeMultipart)208 MimeBodyPart (javax.mail.internet.MimeBodyPart)178 Date (java.util.Date)153 IOException (java.io.IOException)137 Message (javax.mail.Message)120 MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)107 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)97 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)83 InputStream (java.io.InputStream)82 ArrayList (java.util.ArrayList)81 Multipart (javax.mail.Multipart)75 DataHandler (javax.activation.DataHandler)73 ByteArrayOutputStream (java.io.ByteArrayOutputStream)72 BodyPart (javax.mail.BodyPart)70