Java Code Examples for javax.mail.internet.MimeMessage#addFrom()

The following examples show how to use javax.mail.internet.MimeMessage#addFrom() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DKIMSignTest.java    From james-project with Apache License 2.0 4 votes vote down vote up
@ParameterizedTest
@ValueSource(strings = {PKCS1_PEM_FILE, PKCS8_PEM_FILE})
void testDKIMSignMessageAsText(String pemFile) throws MessagingException,
        IOException, FailException {
    MimeMessage mm = new MimeMessage(Session
            .getDefaultInstance(new Properties()));
    mm.addFrom(new Address[]{new InternetAddress("[email protected]")});
    mm.addRecipient(RecipientType.TO, new InternetAddress("[email protected]"));
    mm.setText("An 8bit encoded body with €uro symbol.", "ISO-8859-15");

    Mailet mailet = new DKIMSign();

    FakeMailetConfig mci = FakeMailetConfig.builder()
            .mailetName("Test")
            .mailetContext(FAKE_MAIL_CONTEXT)
            .setProperty(
                    "signatureTemplate",
                    "v=1; s=selector; d=example.com; h=from:to:received:received; a=rsa-sha256; bh=; b=;")
            .setProperty("privateKeyFilepath", pemFile)
            .build();

    mailet.init(mci);

    Mail mail = FakeMail.builder()
        .name("test")
        .mimeMessage(mm)
        .build();

    Mailet m7bit = new ConvertTo7Bit();
    m7bit.init(mci);

    mailet.service(mail);

    m7bit.service(mail);

    ByteArrayOutputStream rawMessage = new ByteArrayOutputStream();
    mail.getMessage().writeTo(rawMessage);

    MockPublicKeyRecordRetriever mockPublicKeyRecordRetriever = new MockPublicKeyRecordRetriever(
            "v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDYDaYKXzwVYwqWbLhmuJ66aTAN8wmDR+rfHE8HfnkSOax0oIoTM5zquZrTLo30870YMfYzxwfB6j/Nz3QdwrUD/t0YMYJiUKyWJnCKfZXHJBJ+yfRHr7oW+UW3cVo9CG2bBfIxsInwYe175g9UjyntJpWueqdEIo1c2bhv9Mp66QIDAQAB;",
            "selector", "example.com");

    verify(rawMessage, mockPublicKeyRecordRetriever);
}
 
Example 2
Source File: DKIMSignTest.java    From james-project with Apache License 2.0 4 votes vote down vote up
@ParameterizedTest
@ValueSource(strings = {PKCS1_PEM_FILE, PKCS8_PEM_FILE})
void testDKIMSignMessageAsObjectConvertedTo7Bit(String pemFile)
        throws MessagingException, IOException, FailException {
    MimeMessage mm = new MimeMessage(Session
            .getDefaultInstance(new Properties()));
    mm.addFrom(new Address[]{new InternetAddress("[email protected]")});
    mm.addRecipient(RecipientType.TO, new InternetAddress("[email protected]"));
    mm.setContent("An 8bit encoded body with €uro symbol.",
            "text/plain; charset=iso-8859-15");
    mm.setHeader("Content-Transfer-Encoding", "8bit");
    mm.saveChanges();

    FAKE_MAIL_CONTEXT.getServerInfo();

    FakeMailetConfig mci = FakeMailetConfig.builder()
            .mailetName("Test")
            .mailetContext(FAKE_MAIL_CONTEXT)
            .setProperty(
                    "signatureTemplate",
                    "v=1; s=selector; d=example.com; h=from:to:received:received; a=rsa-sha256; bh=; b=;")
            .setProperty("privateKeyFilepath", pemFile)
            .build();

    Mail mail = FakeMail.builder()
        .name("test")
        .mimeMessage(mm)
        .build();

    Mailet mailet = new DKIMSign();
    mailet.init(mci);

    Mailet m7bit = new ConvertTo7Bit();
    m7bit.init(mci);
    m7bit.service(mail);

    mailet.service(mail);

    m7bit.service(mail);

    ByteArrayOutputStream rawMessage = new ByteArrayOutputStream();
    mail.getMessage().writeTo(rawMessage);

    MockPublicKeyRecordRetriever mockPublicKeyRecordRetriever = new MockPublicKeyRecordRetriever(
            "v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDYDaYKXzwVYwqWbLhmuJ66aTAN8wmDR+rfHE8HfnkSOax0oIoTM5zquZrTLo30870YMfYzxwfB6j/Nz3QdwrUD/t0YMYJiUKyWJnCKfZXHJBJ+yfRHr7oW+UW3cVo9CG2bBfIxsInwYe175g9UjyntJpWueqdEIo1c2bhv9Mp66QIDAQAB;",
            "selector", "example.com");
    verify(rawMessage, mockPublicKeyRecordRetriever);
}
 
Example 3
Source File: DKIMSignTest.java    From james-project with Apache License 2.0 4 votes vote down vote up
@ParameterizedTest
@ValueSource(strings = {PKCS1_PEM_FILE, PKCS8_PEM_FILE})
void testDKIMSignMessageAsObjectNotConverted(String pemFile)
        throws MessagingException, IOException, FailException {
    MimeMessage mm = new MimeMessage(Session
            .getDefaultInstance(new Properties()));
    mm.addFrom(new Address[]{new InternetAddress("[email protected]")});
    mm.addRecipient(RecipientType.TO, new InternetAddress("[email protected]"));
    mm.setContent("An 8bit encoded body with €uro symbol.",
            "text/plain; charset=iso-8859-15");
    mm.setHeader("Content-Transfer-Encoding", "8bit");
    mm.saveChanges();

    FAKE_MAIL_CONTEXT.getServerInfo();

    FakeMailetConfig mci = FakeMailetConfig.builder()
            .mailetName("Test")
            .mailetContext(FAKE_MAIL_CONTEXT)
            .setProperty(
                    "signatureTemplate",
                    "v=1; s=selector; d=example.com; h=from:to:received:received; a=rsa-sha256; bh=; b=;")
            .setProperty("privateKeyFilepath", pemFile)
            .build();

    Mail mail = FakeMail.builder()
        .name("test")
        .mimeMessage(mm)
        .build();

    Mailet mailet = new DKIMSign();
    mailet.init(mci);

    Mailet m7bit = new ConvertTo7Bit();
    m7bit.init(mci);
    // m7bit.service(mail);

    mailet.service(mail);

    m7bit.service(mail);

    ByteArrayOutputStream rawMessage = new ByteArrayOutputStream();
    mail.getMessage().writeTo(rawMessage);

    MockPublicKeyRecordRetriever mockPublicKeyRecordRetriever = new MockPublicKeyRecordRetriever(
            "v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDYDaYKXzwVYwqWbLhmuJ66aTAN8wmDR+rfHE8HfnkSOax0oIoTM5zquZrTLo30870YMfYzxwfB6j/Nz3QdwrUD/t0YMYJiUKyWJnCKfZXHJBJ+yfRHr7oW+UW3cVo9CG2bBfIxsInwYe175g9UjyntJpWueqdEIo1c2bhv9Mp66QIDAQAB;",
            "selector", "example.com");
    try {
        verify(rawMessage, mockPublicKeyRecordRetriever);
        fail("Expected PermFail");
    } catch (PermFailException e) {
        // do nothing
    }
}
 
Example 4
Source File: MimeMessageBuilder.java    From james-project with Apache License 2.0 4 votes vote down vote up
public MimeMessage build() throws MessagingException {
    Preconditions.checkState(!(text.isPresent() && content.isPresent()), "Can not get at the same time a text and a content");
    MimeMessage mimeMessage = new MimeMessage(Session.getInstance(new Properties()));
    if (text.isPresent()) {
        mimeMessage.setContent(text.get(), textContentType.orElse(DEFAULT_TEXT_PLAIN_UTF8_TYPE));
    }
    if (content.isPresent()) {
        mimeMessage.setContent(content.get());
    }
    if (sender.isPresent()) {
        mimeMessage.setSender(sender.get());
    }
    if (subject.isPresent()) {
        mimeMessage.setSubject(subject.get());
    }
    ImmutableList<InternetAddress> fromAddresses = from.build();
    if (!fromAddresses.isEmpty()) {
        mimeMessage.addFrom(fromAddresses.toArray(InternetAddress[]::new));
    }
    List<InternetAddress> toAddresses = to.build();
    if (!toAddresses.isEmpty()) {
        mimeMessage.setRecipients(Message.RecipientType.TO, toAddresses.toArray(InternetAddress[]::new));
    }
    List<InternetAddress> ccAddresses = cc.build();
    if (!ccAddresses.isEmpty()) {
        mimeMessage.setRecipients(Message.RecipientType.CC, ccAddresses.toArray(InternetAddress[]::new));
    }
    List<InternetAddress> bccAddresses = bcc.build();
    if (!bccAddresses.isEmpty()) {
        mimeMessage.setRecipients(Message.RecipientType.BCC, bccAddresses.toArray(InternetAddress[]::new));
    }

    MimeMessage wrappedMessage = MimeMessageWrapper.wrap(mimeMessage);

    List<Header> headerList = headers.build();
    for (Header header: headerList) {
        if (header.name.equals("Message-ID") || header.name.equals("Date")) {
            wrappedMessage.setHeader(header.name, header.value);
        } else {
            wrappedMessage.addHeader(header.name, header.value);
        }
    }
    wrappedMessage.saveChanges();

    return wrappedMessage;
}