Example usage for javax.mail.internet MimeMessage getFrom

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

Introduction

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

Prototype

@Override
public Address[] getFrom() throws MessagingException 

Source Link

Document

Returns the value of the RFC 822 "From" header fields.

Usage

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

public static void validateFrom(final Email email, final MimeMessage sentMessage)
        throws MessagingException, IOException {
    final List<Address> froms = asList(sentMessage.getFrom());
    assertThat(froms, hasSize(1)); // redundant with contains
    assertThat(froms, contains((Address) email.getFrom()));
}

From source file:mitm.common.mail.EmailAddressUtils.java

/**
 * Returns the From not throwing any exception. If the From header cannot be retrieved, null will be
 * returned./*from  w w w .j a  v  a2 s . c o  m*/
 */
public static Address[] getFromQuietly(MimeMessage message) {
    Address[] froms = null;

    if (message != null) {
        try {
            froms = message.getFrom();
        } catch (Exception e) {
            logger.debug("From is not valid", e);
        }
    }

    return froms;
}

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

public static String getFrom(MimeMessage msg) throws MessagingException, UnsupportedEncodingException {
    String from = "";
    Address[] froms = msg.getFrom();
    if (froms.length < 1)
        throw new MessagingException("?!");

    InternetAddress address = (InternetAddress) froms[0];
    String person = address.getPersonal();
    if (person != null) {
        person = MimeUtility.decodeText(person) + " ";
    } else {//from w  w  w.  j a v  a2 s . c om
        person = "";
    }
    from = person + "<" + address.getAddress() + ">";

    return from;
}

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

private static MailAddress getSender(MimeMessage mimeMessage) throws MessagingException {
    Address[] sender = mimeMessage.getFrom();
    Preconditions.checkArgument(sender.length == 1);
    return castToMailAddress(sender[0]);
}

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 {/*ww  w .  ja  v  a  2s.  c  o  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:com.it.j2ee.email.MailServiceTest.java

@Test
public void sendSimpleMail() throws MessagingException, InterruptedException, IOException {
    simpleMailService.sendNotificationMail("calvin");

    greenMail.waitForIncomingEmail(2000, 1);

    MimeMessage[] messages = greenMail.getReceivedMessages();
    MimeMessage message = messages[messages.length - 1];

    assertThat(message.getFrom()[0].toString()).isEqualTo("springside3.demo@gmail.com");
    assertThat(message.getSubject()).isEqualTo("");
    // text?/*from  w w  w . j  av  a  2  s  . co  m*/
    System.out.println(message.getContent());
    assertThat(((String) message.getContent())).contains("");
}

From source file:com.it.j2ee.email.MailServiceTest.java

@Test
public void sendMimeMail() throws InterruptedException, MessagingException, IOException {
    mimeMailService.sendNotificationMail("calvin");

    greenMail.waitForIncomingEmail(2000, 1);
    MimeMessage[] messages = greenMail.getReceivedMessages();
    MimeMessage message = messages[messages.length - 1];

    assertThat(message.getFrom()[0].toString()).isEqualTo("springside3.demo@gmail.com");
    assertThat(message.getSubject()).isEqualTo("");

    MimeMultipart mimeMultipart = (MimeMultipart) message.getContent();

    assertThat(mimeMultipart.getCount()).isEqualTo(2);

    // Html?/*from www  . j a  v a  2 s  . c o  m*/
    String mainPartText = getMainPartText(mimeMultipart.getBodyPart(0));
    System.out.println(mainPartText);
    assertThat(mainPartText).contains("<h1>calvin.</h1>");

    // 
    assertThat(GreenMailUtil.getBody(mimeMultipart.getBodyPart(1)).trim())
            .isEqualTo("Hello,i am a attachment.");
}

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   ww w .j  av a2s . c  o  m
        }
    }
}

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 {//w w w .ja v  a  2 s. co  m
            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:org.craftercms.commons.mail.impl.EmailFactoryImplTest.java

@Test
public void testGetEmailWithBodyParam() throws Exception {
    EmailImpl email = (EmailImpl) emailFactory.getEmail(FROM, TO, CC, BCC, REPLY_TO, SUBJECT, BODY, false);

    assertNotNull(email);/*w w w .j av  a2 s. c o m*/

    MimeMessage msg = email.message;

    assertArrayEquals(InternetAddress.parse(FROM), msg.getFrom());
    assertArrayEquals(InternetAddress.parse(StringUtils.join(TO)), msg.getRecipients(Message.RecipientType.TO));
    assertArrayEquals(InternetAddress.parse(StringUtils.join(CC)), msg.getRecipients(Message.RecipientType.CC));
    assertArrayEquals(InternetAddress.parse(StringUtils.join(BCC)),
            msg.getRecipients(Message.RecipientType.BCC));
    assertArrayEquals(InternetAddress.parse(REPLY_TO), msg.getReplyTo());
    assertEquals(SUBJECT, msg.getSubject());
    assertEquals(BODY, msg.getContent());
}