Example usage for javax.mail.internet MimeMessage getAllRecipients

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

Introduction

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

Prototype

@Override
public Address[] getAllRecipients() throws MessagingException 

Source Link

Document

Get all the recipient addresses for the message.

Usage

From source file:com.duroty.utils.mail.MessageUtilities.java

/**
 * DOCUMENT ME!/*  ww w  . j  av a 2s .co m*/
 *
 * @param args DOCUMENT ME!
 *
 * @throws UnsupportedEncodingException DOCUMENT ME!
 * @throws EmailException DOCUMENT ME!
 * @throws MessagingException DOCUMENT ME!
 */
public static void main(String[] args) throws UnsupportedEncodingException, EmailException, MessagingException {
    InternetAddress[] aux1 = new InternetAddress[5];
    aux1[0] = new InternetAddress("duroty@iigov.net", "Jordi Marqus");
    aux1[1] = new InternetAddress("duroty@iigov.net", "Jordi Marqus");
    aux1[2] = new InternetAddress("duroty@iigov.net", "Jordi Marqus");
    aux1[3] = new InternetAddress("duroty@iigov.net", "Jordi Marqus");
    aux1[4] = new InternetAddress("duroty@iigov.net", "Jordi Marqus");

    System.out.println(MessageUtilities.decodeAddressesEmail(aux1));

    HtmlEmail email = new HtmlEmail();
    email.setHostName("10.0.0.68");
    email.setFrom("duroty@iigov.net");
    email.addReplyTo("duroty@iigov.net");

    email.addTo("cagao@ii.org");

    email.addCc("raul1@iigov.org");
    email.addCc("raul2@iigov.org");
    email.addCc("raul3@iigov.org");
    email.addCc("raul4@iigov.org");
    email.addCc("raul5@iigov.org");

    email.addBcc("caca1@iigov.org");

    email.setHtmlMsg("<html>la merda fa pudor</html>");

    email.buildMimeMessage();

    MimeMessage mime = email.getMimeMessage();

    System.out.println(MessageUtilities.decodeAddressesEmail(mime.getAllRecipients()));
}

From source file:com.zxy.commons.email.MailMessageUtils.java

/**
 * inputStream??//from  w  w  w.j  a v a  2 s  .co m
 * @param inputStream InputStream
 * @throws EmailException EmailException
 * @throws MessagingException MessagingException
*/
public static void sendEml(InputStream inputStream) throws EmailException, MessagingException {
    try {
        Session session = getEmail().getMailSession();
        MimeMessage message = new MimeMessage(session, inputStream);

        Transport.send(message, message.getAllRecipients());
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
}

From source file:ee.cyber.licensing.service.MailService.java

private static void sendMail(String email, String password, String host, Session getMailSession,
        MimeMessage mailMessage) throws MessagingException {
    Transport transport = getMailSession.getTransport("smtp");

    // if you have 2FA enabled then provide App Specific Password
    transport.connect(host, email, password);
    transport.sendMessage(mailMessage, mailMessage.getAllRecipients());
    transport.close();/*from   ww  w  .jav  a 2s.co m*/
    logger.info("6th ===> Email Sent Successfully With Image Attachment");
    logger.info("7th ===> generateAndSendEmail() ended");
}

From source file:org.elasticsearch.river.email.EmailToJson.java

public static String getReceiveAddress(MimeMessage msg, Message.RecipientType type) throws MessagingException {
    StringBuilder receiveAddress = new StringBuilder();
    Address[] addresss = null;//from w  w  w  .ja v a 2 s . com
    if (type == null) {
        addresss = msg.getAllRecipients();
    } else {
        addresss = msg.getRecipients(type);
    }

    if (addresss == null || addresss.length < 1)
        return "";
    // throw new MessagingException("!");
    for (Address address : addresss) {
        InternetAddress internetAddress = (InternetAddress) address;
        receiveAddress.append(internetAddress.toUnicodeString()).append(",");
    }

    receiveAddress.deleteCharAt(receiveAddress.length() - 1); //??

    return receiveAddress.toString();
}

From source file:quickforms.sme.UseFulMethods.java

static public void sendEmail(String d_email, String pwd, String m_to, String m_subject, String message)
        throws Exception {
    final String from = d_email;
    final String password = pwd;
    class SMTPAuthenticator extends Authenticator {

        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(from, password);
        }//from   w ww.  j  a  v a 2 s  .  c  om
    }

    //String d_uname = "email";
    //String d_password = "password";
    String d_host = "smtp.gmail.com";
    String d_port = "465"; //465,587

    Properties props = new Properties();
    props.put("mail.smtp.user", from);
    props.put("mail.smtp.host", d_host);
    props.put("mail.smtp.port", d_port);
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.debug", "true");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.socketFactory.port", d_port);
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.socketFactory.fallback", "false");

    SMTPAuthenticator auth = new SMTPAuthenticator();
    Session session = Session.getInstance(props, auth);
    session.setDebug(true);

    MimeMessage msg = new MimeMessage(session);

    //msg.setText(message);
    // Send the actual HTML message, as big as you like

    msg.setContent(message, "text/html");
    msg.setSubject(m_subject);
    msg.setFrom(new InternetAddress(from));
    msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to));

    Transport transport = session.getTransport("smtps");
    transport.connect(d_host, 465, from, password);
    transport.sendMessage(msg, msg.getAllRecipients());
    transport.close();

}

From source file:org.masukomi.aspirin.core.Bouncer.java

/**
 * This generates a response to the Return-Path address, or the address of
 * the message's sender if the Return-Path is not available. Note that this
 * is different than a mail-client's reply, which would use the Reply-To or
 * From header.//  w w w .j a  v  a2 s  .co  m
 * 
 * @param mail
 *            DOCUMENT ME!
 * @param message
 *            DOCUMENT ME!
 * @param bouncer
 *            DOCUMENT ME!
 * 
 * @throws MessagingException
 *             DOCUMENT ME!
 */
static public void bounce(MailQue que, Mail mail, String message, MailAddress bouncer)
        throws MessagingException {
    if (bouncer != null) {
        if (log.isDebugEnabled()) {
            log.debug("bouncing message to postmaster");
        }
        MimeMessage orig = mail.getMessage();
        //Create the reply message
        MimeMessage reply = (MimeMessage) orig.reply(false);
        //If there is a Return-Path header,
        if (orig.getHeader(RFC2822Headers.RETURN_PATH) != null) {
            //Return the message to that address, not to the Reply-To
            // address
            reply.setRecipient(MimeMessage.RecipientType.TO,
                    new InternetAddress(orig.getHeader(RFC2822Headers.RETURN_PATH)[0]));
        }
        //Create the list of recipients in our MailAddress format
        Collection recipients = new HashSet();
        Address[] addresses = reply.getAllRecipients();
        for (int i = 0; i < addresses.length; i++) {
            recipients.add(new MailAddress((InternetAddress) addresses[i]));
        }
        //Change the sender...
        reply.setFrom(bouncer.toInternetAddress());
        try {
            //Create the message body
            MimeMultipart multipart = new MimeMultipart();
            //Add message as the first mime body part
            MimeBodyPart part = new MimeBodyPart();
            part.setContent(message, "text/plain");
            part.setHeader(RFC2822Headers.CONTENT_TYPE, "text/plain");
            multipart.addBodyPart(part);
            //Add the original message as the second mime body part
            part = new MimeBodyPart();
            part.setContent(orig.getContent(), orig.getContentType());
            part.setHeader(RFC2822Headers.CONTENT_TYPE, orig.getContentType());
            multipart.addBodyPart(part);
            reply.setHeader(RFC2822Headers.DATE, rfc822DateFormat.format(new Date()));
            reply.setContent(multipart);
            reply.setHeader(RFC2822Headers.CONTENT_TYPE, multipart.getContentType());
        } catch (IOException ioe) {
            throw new MessagingException("Unable to create multipart body", ioe);
        }
        //Send it off...
        //sendMail( bouncer, recipients, reply );
        que.queMail(reply);
    }
}

From source file:org.apache.james.server.core.MailImpl.java

private static ImmutableList<MailAddress> getRecipients(MimeMessage mimeMessage) throws MessagingException {
    return Arrays.stream(mimeMessage.getAllRecipients())
            .map(Throwing.function(MailImpl::castToMailAddress).sneakyThrow())
            .collect(ImmutableList.toImmutableList());
}

From source file:org.ofbiz.common.CommonServices.java

public static Map<String, Object> mcaTest(DispatchContext dctx, Map<String, ?> context) {
    MimeMessageWrapper wrapper = (MimeMessageWrapper) context.get("messageWrapper");
    MimeMessage message = wrapper.getMessage();
    try {/*from w  w  w. j  av a2  s.  co m*/
        if (message.getAllRecipients() != null) {
            Debug.log("To: " + UtilMisc.toListArray(message.getAllRecipients()), module);
        }
        if (message.getFrom() != null) {
            Debug.log("From: " + UtilMisc.toListArray(message.getFrom()), module);
        }
        Debug.log("Subject: " + message.getSubject(), module);
        if (message.getSentDate() != null) {
            Debug.log("Sent: " + message.getSentDate().toString(), module);
        }
        if (message.getReceivedDate() != null) {
            Debug.log("Received: " + message.getReceivedDate().toString(), module);
        }
    } catch (Exception e) {
        Debug.logError(e, module);
    }
    return ServiceUtil.returnSuccess();
}

From source file:org.tsm.concharto.service.EmailService.java

/**
 *
 * @param message/*  w  w w.j a va2 s .c om*/
 * @throws MessagingException e
 * @throws MailException e
 */
public void sendMessage(MimeMessage message) {
    try {
        if (message.getAllRecipients() != null) {
            mailSender.setHost(smtpHost);
            mailSender.send(message);
        } else {
            log.warn("Email not sent, no recipients specified");
        }
    } catch (MailException e) {
        log.error("send mail exception ", e);
    } catch (MessagingException e) {
        log.error("messagign exception", e);
    }
}

From source file:jenkins.plugins.mailer.tasks.MimeMessageBuilderTest.java

@Test
@Issue("JENKINS-26606")
public void test_addRecipients_tokenizer() throws Exception {
    MimeMessageBuilder messageBuilder = new MimeMessageBuilder();

    messageBuilder.addRecipients("tom.xxxx@gmail.com,tom.yyyy@gmail.com tom.zzzz@gmail.com");
    MimeMessage mimeMessage = messageBuilder.buildMimeMessage();

    Address[] recipients = mimeMessage.getAllRecipients();
    Assert.assertEquals(3, recipients.length);
    Assert.assertEquals("tom.xxxx@gmail.com", recipients[0].toString());
    Assert.assertEquals("tom.yyyy@gmail.com", recipients[1].toString());
    Assert.assertEquals("tom.zzzz@gmail.com", recipients[2].toString());
}