Example usage for javax.mail.internet MimeMessage getRecipients

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

Introduction

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

Prototype

@Override
public Address[] getRecipients(Message.RecipientType type) throws MessagingException 

Source Link

Document

Returns the recepients specified by the type.

Usage

From source file:it.ozimov.springboot.templating.mail.utils.EmailToMimeMessageTest.java

public static void validateCc(final Email email, final MimeMessage sentMessage)
        throws MessagingException, IOException {
    final List<Address> ccs = asList(sentMessage.getRecipients(CC));
    assertThat(ccs, everyItem(isIn(toAddress(email.getCc()))));
}

From source file:it.ozimov.springboot.templating.mail.utils.EmailToMimeMessageTest.java

public static void validateTo(final Email email, final MimeMessage sentMessage)
        throws MessagingException, IOException {
    final List<Address> tos = asList(sentMessage.getRecipients(TO));
    assertThat(tos.get(0), is((Address) (new ArrayList<>(email.getTo()).get(0))));
    assertThat(tos, everyItem(isIn(toAddress(email.getTo()))));
}

From source file:it.ozimov.springboot.templating.mail.utils.EmailToMimeMessageTest.java

public static void validateBcc(final Email email, final MimeMessage sentMessage)
        throws MessagingException, IOException {
    final List<Address> bccs = asList(sentMessage.getRecipients(BCC));
    assertThat(bccs, everyItem(isIn(toAddress(email.getBcc()))));
}

From source file:eu.forgestore.ws.util.EmailUtil.java

public static void SendRegistrationActivationEmail(String email, String messageBody) {

    Properties props = new Properties();

    // Session session = Session.getDefaultInstance(props, null);

    props.setProperty("mail.transport.protocol", "smtp");
    if ((FStoreRepository.getPropertyByName("mailhost").getValue() != null)
            && (!FStoreRepository.getPropertyByName("mailhost").getValue().isEmpty()))
        props.setProperty("mail.host", FStoreRepository.getPropertyByName("mailhost").getValue());
    if ((FStoreRepository.getPropertyByName("mailuser").getValue() != null)
            && (!FStoreRepository.getPropertyByName("mailuser").getValue().isEmpty()))
        props.setProperty("mail.user", FStoreRepository.getPropertyByName("mailuser").getValue());
    if ((FStoreRepository.getPropertyByName("mailpassword").getValue() != null)
            && (!FStoreRepository.getPropertyByName("mailpassword").getValue().isEmpty()))
        props.setProperty("mail.password", FStoreRepository.getPropertyByName("mailpassword").getValue());

    String adminemail = FStoreRepository.getPropertyByName("adminEmail").getValue();
    String subj = FStoreRepository.getPropertyByName("activationEmailSubject").getValue();
    logger.info("adminemail = " + adminemail);
    logger.info("subj = " + subj);

    Session mailSession = Session.getDefaultInstance(props, null);
    Transport transport;/*from  ww  w.  ja  va 2s  . c om*/
    try {
        transport = mailSession.getTransport();

        MimeMessage msg = new MimeMessage(mailSession);
        msg.setSentDate(new Date());
        msg.setFrom(new InternetAddress(adminemail, adminemail));
        msg.setSubject(subj);
        msg.setContent(messageBody, "text/html; charset=ISO-8859-1");
        msg.addRecipient(Message.RecipientType.TO, new InternetAddress(email, email));

        transport.connect();
        transport.sendMessage(msg, msg.getRecipients(Message.RecipientType.TO));

        transport.close();

    } catch (NoSuchProviderException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (MessagingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:gr.upatras.ece.nam.baker.util.EmailUtil.java

public static void SendRegistrationActivationEmail(String email, String messageBody) {

    Properties props = new Properties();

    // Session session = Session.getDefaultInstance(props, null);

    props.setProperty("mail.transport.protocol", "smtp");
    if ((BakerRepository.getPropertyByName("mailhost").getValue() != null)
            && (!BakerRepository.getPropertyByName("mailhost").getValue().isEmpty()))
        props.setProperty("mail.host", BakerRepository.getPropertyByName("mailhost").getValue());
    if ((BakerRepository.getPropertyByName("mailuser").getValue() != null)
            && (!BakerRepository.getPropertyByName("mailuser").getValue().isEmpty()))
        props.setProperty("mail.user", BakerRepository.getPropertyByName("mailuser").getValue());
    if ((BakerRepository.getPropertyByName("mailpassword").getValue() != null)
            && (!BakerRepository.getPropertyByName("mailpassword").getValue().isEmpty()))
        props.setProperty("mail.password", BakerRepository.getPropertyByName("mailpassword").getValue());

    String adminemail = BakerRepository.getPropertyByName("adminEmail").getValue();
    String subj = BakerRepository.getPropertyByName("activationEmailSubject").getValue();
    logger.info("adminemail = " + adminemail);
    logger.info("subj = " + subj);

    Session mailSession = Session.getDefaultInstance(props, null);
    Transport transport;/*from  w  w  w.j  a  v a 2 s.c  om*/
    try {
        transport = mailSession.getTransport();

        MimeMessage msg = new MimeMessage(mailSession);
        msg.setSentDate(new Date());
        msg.setFrom(new InternetAddress(adminemail, adminemail));
        msg.setSubject(subj);
        msg.setContent(messageBody, "text/html; charset=ISO-8859-1");
        msg.addRecipient(Message.RecipientType.TO, new InternetAddress(email, email));

        transport.connect();
        transport.sendMessage(msg, msg.getRecipients(Message.RecipientType.TO));

        transport.close();

    } catch (NoSuchProviderException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (MessagingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:it.greenvulcano.gvesb.virtual.utils.MimeMessageHelper.java

public static Map<String, List<String>> getMessageRecipients(MimeMessage mimeMessage)
        throws MessagingException {

    Map<String, List<String>> recipients = new LinkedHashMap<>();

    Optional.ofNullable(mimeMessage.getRecipients(RecipientType.TO))
            .ifPresent(a -> recipients.put("TO", Stream.of(a).map(InternetAddress.class::cast)
                    .map(InternetAddress::getAddress).collect(Collectors.toList())));

    Optional.ofNullable(mimeMessage.getRecipients(RecipientType.CC))
            .ifPresent(a -> recipients.put("CC", Stream.of(a).map(InternetAddress.class::cast)
                    .map(InternetAddress::getAddress).collect(Collectors.toList())));

    Optional.ofNullable(mimeMessage.getRecipients(RecipientType.BCC))
            .ifPresent(a -> recipients.put("BCC", Stream.of(a).map(InternetAddress.class::cast)
                    .map(InternetAddress::getAddress).collect(Collectors.toList())));

    return recipients;

}

From source file:org.parancoe.test.LoggingMailSender.java

@Override
protected void doSend(MimeMessage[] mimeMessages, Object[] originalMessages) {
    if (mimeMessages != null) {
        for (MimeMessage mimeMessage : mimeMessages) {
            try {
                logger.info("from: {}", (Object[]) mimeMessage.getFrom());
                logger.info("to: {}", (Object[]) mimeMessage.getRecipients(Message.RecipientType.TO));
                logger.info("cc: {}", (Object[]) mimeMessage.getRecipients(Message.RecipientType.CC));
                logger.info("bcc: {}", (Object[]) mimeMessage.getRecipients(Message.RecipientType.BCC));
                logger.info("subject: {}", mimeMessage.getSubject());
                logger.info("content: {}", mimeMessage.getContent().toString());
                logger.info("content type: {}", mimeMessage.getContentType());
            } catch (Exception ex) {
                logger.error("Can't get message content", ex);
            }//from   w ww.j  av a2s. c  o  m
        }
    }
}

From source file:ca.airspeed.demo.testingemail.EmailServiceTest.java

/**
 * When we send out an email with just a text body, et expect to get a
 * Multipart email having only a plain text body.
 */// w  ww  . j  a v  a 2  s.  c o  m
@Test
public void testSimpleEmail() throws Exception {
    // Given:
    EmailService service = makeALocalMailer();

    InternetAddress expectedTo = new InternetAddress("Indiana.Jones@domain.com", "Indiana Jones");
    String expectedSubject = "This is a Test Email";
    String expectedTextBody = "This is a simple test.";

    // When:
    service.sendSimpleEmail(expectedTo, expectedSubject, expectedTextBody);

    // Then:
    List<WiserMessage> messages = wiser.getMessages();
    assertEquals("Number of messages sent;", 1, messages.size());

    WiserMessage message = messages.get(0);
    assertNotNull("No message was actually sent.", message);
    MimeMessage mimeMessage = message.getMimeMessage();
    Address[] toRecipients = mimeMessage.getRecipients(RecipientType.TO);
    assertEquals("Number of To: Recipient;", 1, toRecipients.length);
    Address toRecipient = toRecipients[0];
    assertEquals("To: Recipient;", expectedTo, toRecipient);

    InternetAddress expectedFrom = new InternetAddress("admin@domain.com", "Domain Admin");
    Address[] fromArr = mimeMessage.getFrom();
    assertEquals("From: email addresses;", 1, fromArr.length);
    assertEquals("Email From: address,", expectedFrom, fromArr[0]);

    assertEquals("Subject;", expectedSubject, mimeMessage.getSubject());

    assertNotNull("The date of the email cannot be null.", mimeMessage.getSentDate());

    MimeMultipart body = ((MimeMultipart) mimeMessage.getContent());
    assertEquals("Number of MIME Parts in the body;", 1, body.getCount());
    MimeMultipart textPart = ((MimeMultipart) body.getBodyPart(0).getContent());
    assertEquals("Number of MIME parts in the text body;", 1, textPart.getCount());
    MimeBodyPart plainTextPart = ((MimeBodyPart) textPart.getBodyPart(0));
    assertTrue("Expected the plain text content to be text/plain.", plainTextPart.isMimeType("text/plain"));
    assertEquals("Text Body;", expectedTextBody, plainTextPart.getContent());
}

From source file:com.example.securelogin.domain.service.mail.PasswordReissueMailSharedServiceImpl.java

@Override
public List<ReceivedMail> getReceivedMessages() {
    List<ReceivedMail> mails = new ArrayList<>();
    MimeMessage[] messages = greenMailBean.getReceivedMessages();
    for (MimeMessage message : messages) {
        ReceivedMail mail = new ReceivedMail();
        try {/*from w  w  w . j  a v  a2  s  .  c  om*/
            mail.setTo(message.getFrom()[0].toString());
            mail.setFrom(message.getRecipients(Message.RecipientType.TO)[0].toString());
            mail.setSubject(message.getSubject());
            mail.setText(message.getContent().toString().replace("\r\n", ""));
            mails.add(mail);
        } catch (MessagingException e) {
            throw new SystemException(MessageKeys.E_SL_FW_9001, e);
        } catch (IOException e) {
            throw new SystemException(MessageKeys.E_SL_FW_9001, e);
        }

    }
    return mails;
}

From source file:gmailclientfx.models.FetchMessageCallable.java

public MyMessage fetchMessage(MimeMessage m, int tblIndex, String lbl) throws MessagingException, Exception {
    MimeMessage msg = new MimeMessage(m);
    MimeMessageParser parser = new MimeMessageParser(msg);
    parser.parse();//  w  ww  .  j av  a 2  s . c om

    String naslov = parser.getSubject();
    String from = parser.getFrom();
    Address[] to = msg.getRecipients(Message.RecipientType.TO);
    String toStr = "";
    if (to.length > 1) {
        for (int k = 0; k < to.length; k++) {
            if (k == to.length - 1)
                toStr += to[k].toString();
            else
                toStr += to[k].toString() + ",";
        }
    } else {
        toStr = to[0].toString();
    }
    String body = parser.getHtmlContent();
    if (body.equals(""))
        body = parser.getPlainContent();
    String date = msg.getSentDate().toString();
    String label = lbl;

    MyMessage myMsg = new MyMessage(User.getUserId(GmailClient.getEmail()), naslov, from, toStr, body, date,
            label);
    myMsg.setTblId(tblIndex);
    return myMsg;
}