Search in sources :

Example 76 with Message

use of javax.mail.Message in project Lucee by lucee.

the class MailClient method deleteMails.

/**
 * delete all message in ibox that match given criteria
 * @param messageNumbers
 * @param uIds
 * @throws MessagingException
 * @throws IOException
 */
public void deleteMails(String[] as, String[] as1) throws MessagingException, IOException {
    Folder folder;
    Message[] amessage;
    folder = _store.getFolder("INBOX");
    folder.open(2);
    Map<String, Message> map = getMessages(null, folder, as1, as, startrow, maxrows, false);
    Iterator<String> iterator = map.keySet().iterator();
    amessage = new Message[map.size()];
    int i = 0;
    while (iterator.hasNext()) {
        amessage[i++] = map.get(iterator.next());
    }
    try {
        folder.setFlags(amessage, new Flags(javax.mail.Flags.Flag.DELETED), true);
    } finally {
        folder.close(true);
    }
}
Also used : Message(javax.mail.Message) Flags(javax.mail.Flags) Folder(javax.mail.Folder)

Example 77 with Message

use of javax.mail.Message in project blogwt by billy1380.

the class EmailHelper method sendEmail.

public static boolean sendEmail(String to, String name, String subject, String body, boolean isHtml, Map<String, byte[]> attachments) {
    boolean sent = false;
    Property email = PropertyServiceProvider.provide().getNamedProperty(PropertyHelper.OUTGOING_EMAIL);
    if (!PropertyHelper.isEmpty(email) && !PropertyHelper.NONE_VALUE.equals(email.value)) {
        Properties props = new Properties();
        Session session = Session.getDefaultInstance(props, null);
        try {
            Message msg = new MimeMessage(session);
            InternetAddress address = new InternetAddress(email.value, PropertyServiceProvider.provide().getNamedProperty(PropertyHelper.TITLE).value);
            msg.setFrom(address);
            msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to, name));
            msg.setSubject(subject);
            if (attachments == null || attachments.size() == 0) {
                if (isHtml) {
                    msg.setContent(body, "text/html");
                } else {
                    msg.setText(body);
                }
            } else {
                Multipart mp = new MimeMultipart();
                MimeBodyPart content = new MimeBodyPart();
                if (isHtml) {
                    content.setContent(body, "text/html");
                } else {
                    content.setText(body);
                }
                mp.addBodyPart(content);
                for (String key : attachments.keySet()) {
                    MimeBodyPart attachment = new MimeBodyPart();
                    InputStream attachmentDataStream = new ByteArrayInputStream(attachments.get(key));
                    attachment.setFileName(key);
                    attachment.setContent(attachmentDataStream, "application/pdf");
                    mp.addBodyPart(attachment);
                }
                msg.setContent(mp);
            }
            Transport.send(msg);
            if (LOG.isLoggable(Level.INFO)) {
                LOG.info("Email sent successfully.");
            }
            sent = true;
        } catch (MessagingException | UnsupportedEncodingException e) {
            LOG.log(Level.WARNING, "Error sending email [" + content(to, name, subject, body) + "]", e);
        }
    } else {
        if (LOG.isLoggable(Level.INFO)) {
            LOG.info("Property [" + PropertyHelper.OUTGOING_EMAIL + "] not configured.");
        }
    }
    if (LOG.isLoggable(Level.FINE)) {
        LOG.fine("Email [" + content(to, name, subject, body) + "]");
    }
    return sent;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Properties(java.util.Properties) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) ByteArrayInputStream(java.io.ByteArrayInputStream) MimeBodyPart(javax.mail.internet.MimeBodyPart) Property(com.willshex.blogwt.shared.api.datatype.Property) Session(javax.mail.Session)

Example 78 with Message

use of javax.mail.Message in project suite by stupidsing.

the class SmtpSslGmail method send.

public void send(String to, String subject, String body) {
    Constants.bindSecrets("gmail .0 .1").map((username, enc) -> {
        String password = decode(System.getenv("USER").toCharArray(), enc);
        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "465");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class", SSLSocketFactory.class.getName());
        Session session = Session.getDefaultInstance(props, new Authenticator() {

            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });
        try {
            String sender = username + "@gmail.com";
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(sender));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to != null ? to : sender));
            message.setSubject(subject);
            message.setText(body);
            Transport.send(message);
        } catch (MessagingException e) {
            Fail.t(e);
        }
        return true;
    });
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) Properties(java.util.Properties) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) Authenticator(javax.mail.Authenticator) Session(javax.mail.Session) PasswordAuthentication(javax.mail.PasswordAuthentication)

Example 79 with Message

use of javax.mail.Message in project wso2-axis2-transports by wso2.

the class MailRequestResponseClient method waitForReply.

private Message waitForReply(String msgId) throws Exception {
    Thread.yield();
    Thread.sleep(100);
    Message reply = null;
    boolean replyNotFound = true;
    int retryCount = 50;
    while (replyNotFound) {
        log.debug("Checking for response ... with MessageID : " + msgId);
        reply = getMessage(msgId);
        if (reply != null) {
            replyNotFound = false;
        } else {
            if (retryCount-- > 0) {
                Thread.sleep(100);
            } else {
                break;
            }
        }
    }
    return reply;
}
Also used : Message(javax.mail.Message) IncomingMessage(org.apache.axis2.transport.testkit.message.IncomingMessage) MimeMessage(javax.mail.internet.MimeMessage)

Example 80 with Message

use of javax.mail.Message in project gocd by gocd.

the class Pop3MailClient method matchMessages.

private String matchMessages(MessageMatcher messageContains) throws MessagingException, IOException {
    Folder folder = null;
    try {
        folder = getInboxFolder();
        final Set<String> contents = new HashSet<>();
        Message[] messagesInBox = folder.getMessages();
        List<Message> messages = new ArrayList<>();
        for (Message message : messagesInBox) {
            if (messageContains.matches(message)) {
                messages.add(message);
            }
        }
        if (messages == null || messages.size() == 0) {
            throw new RuntimeException("No message found matching :" + messageContains + ", the actual messages are " + contents);
        }
        if (messages.size() > 1) {
            throw new RuntimeException("Multiple message matched content");
        }
        return (String) messages.get(0).getContent();
    } finally {
        if (folder != null && folder.isOpen()) {
            try {
                folder.close(true);
            } catch (MessagingException e) {
            }
        }
    }
}
Also used : Message(javax.mail.Message) MessagingException(javax.mail.MessagingException) ArrayList(java.util.ArrayList) Folder(javax.mail.Folder) HashSet(java.util.HashSet)

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