Skip to content

Commit

Permalink
#18: Add attachments to the 'javax.mail.Message'
Browse files Browse the repository at this point in the history
  • Loading branch information
dgroup committed Mar 31, 2019
1 parent 77f72a7 commit 6b04890
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 46 deletions.
3 changes: 2 additions & 1 deletion src/main/java/io/github/dgroup/mbox4j/Msg.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package io.github.dgroup.mbox4j;

import java.io.File;
import java.util.Collection;
import java.util.Set;
import org.cactoos.Text;

Expand Down Expand Up @@ -77,5 +78,5 @@ public interface Msg {
* The attachments within the email.
* @return The attachments.
*/
Iterable<File> attachments();
Collection<File> attachments();
}
74 changes: 47 additions & 27 deletions src/main/java/io/github/dgroup/mbox4j/msg/FakeMsg.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,37 +26,41 @@

import io.github.dgroup.mbox4j.Msg;
import java.io.File;
import java.util.Collection;
import java.util.Set;
import org.cactoos.Text;
import org.cactoos.collection.CollectionOf;

/**
* The fake implementation of {@link Msg} for unit testing purposes.
*
* @since 0.1.0
* @checkstyle MemberNameCheck (200 lines)
* @checkstyle ParameterNameCheck (200 lines)
* @checkstyle ParameterNumberCheck (200 lines)
*/
@SuppressWarnings("PMD.ShortMethodName")
@SuppressWarnings({"PMD.ShortMethodName", "PMD.AvoidFieldNameMatchingMethodName"})
public final class FakeMsg implements Msg {

/**
* The sender's email address.
*/
private final Text frm;
private final Text from;

/**
* The target email recipients.
*/
private final Set<Text> recipients;
private final Set<Text> to;

/**
* The target email recipients for the `CC` (carbon copy).
*/
private final Set<Text> copy;
private final Set<Text> cc;

/**
* The target email recipients for the `BCC` (blind carbon copy).
*/
private final Set<Text> bcopy;
private final Set<Text> bcc;

/**
* The subject of the email message.
Expand All @@ -71,50 +75,67 @@ public final class FakeMsg implements Msg {
/**
* The message attachments.
*/
private final Iterable<File> attachs;
private final Collection<File> attachments;

/**
* Ctor.
* @param frm The sender's email address.
* @param recipients The target email recipients.
* @param copy The target email recipients for the `CC` (carbon copy).
* @param bcopy The target email recipients for the `BCC` (blind carbon copy).
* @param from The sender's email address.
* @param to The target email recipients.
* @param cc The target email recipients for the `CC` (carbon copy).
* @param bcc The target email recipients for the `BCC` (blind carbon copy).
* @param subj The subject of the email message.
* @param content The message content.
* @param attachs The message attachments.
* @param body The message content.
* @param attachments The message attachments.
* @checkstyle ParameterNumberCheck (5 lines)
*/
public FakeMsg(
final Text frm, final Set<Text> recipients, final Set<Text> copy, final Set<Text> bcopy,
final Text subj, final Text content, final Iterable<File> attachs
final Text from, final Set<Text> to, final Set<Text> cc, final Set<Text> bcc,
final Text subj, final Text body, final File... attachments
) {
this.frm = frm;
this.recipients = recipients;
this.copy = copy;
this.bcopy = bcopy;
this(from, to, cc, bcc, subj, body, new CollectionOf<>(attachments));
}

/**
* Ctor.
* @param from The sender's email address.
* @param to The target email recipients.
* @param cc The target email recipients for the `CC` (carbon copy).
* @param bcc The target email recipients for the `BCC` (blind carbon copy).
* @param subj The subject of the email message.
* @param body The message content.
* @param attachments The message attachments.
*/
public FakeMsg(
final Text from, final Set<Text> to, final Set<Text> cc, final Set<Text> bcc,
final Text subj, final Text body, final Collection<File> attachments
) {
this.from = from;
this.to = to;
this.cc = cc;
this.bcc = bcc;
this.subj = subj;
this.content = content;
this.attachs = attachs;
this.content = body;
this.attachments = attachments;
}

@Override
public Text from() {
return this.frm;
return this.from;
}

@Override
public Set<Text> to() {
return this.recipients;
return this.to;
}

@Override
public Set<Text> cc() {
return this.copy;
return this.cc;
}

@Override
public Set<Text> bcc() {
return this.bcopy;
return this.bcc;
}

@Override
Expand All @@ -128,8 +149,7 @@ public Text body() {
}

@Override
public Iterable<File> attachments() {
return this.attachs;
public Collection<File> attachments() {
return this.attachments;
}

}
3 changes: 2 additions & 1 deletion src/main/java/io/github/dgroup/mbox4j/msg/MsgEnvelope.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import io.github.dgroup.mbox4j.Msg;
import java.io.File;
import java.util.Collection;
import java.util.Objects;
import java.util.Set;
import org.cactoos.Text;
Expand Down Expand Up @@ -85,7 +86,7 @@ public final Text body() {
}

@Override
public final Iterable<File> attachments() {
public final Collection<File> attachments() {
return this.origin.attachments();
}

Expand Down
32 changes: 23 additions & 9 deletions src/main/java/io/github/dgroup/mbox4j/msg/MsgOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@

import io.github.dgroup.mbox4j.Msg;
import java.io.File;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import org.cactoos.Text;
import org.cactoos.collection.CollectionOf;
import org.cactoos.iterable.Mapped;
import org.cactoos.set.SetOf;
import org.cactoos.text.TextOf;
Expand All @@ -37,6 +39,8 @@
* The immutable email message.
*
* @since 0.1.0
* @checkstyle ParameterNameCheck (200 lines)
* @checkstyle ParameterNumberCheck (200 lines)
*/
@SuppressWarnings("PMD.ShortMethodName")
public final class MsgOf extends MsgEnvelope {
Expand All @@ -47,8 +51,6 @@ public final class MsgOf extends MsgEnvelope {
* @param to The target email recipients.
* @param subj The subject of the email message.
* @param body The message content.
* @checkstyle ParameterNameCheck (5 lines)
* @checkstyle ParameterNumberCheck (5 lines)
*/
public MsgOf(final String from, final String to, final String subj, final String body) {
this(
Expand All @@ -71,9 +73,6 @@ public MsgOf(final String from, final String to, final String subj, final String
* @param subj The subject of the email message.
* @param body The message content.
* @param attachments The message attachments.
* @checkstyle ParameterNameCheck (10 lines)
* @checkstyle ParameterNumberCheck (5 lines)
* @checkstyle AnonInnerLengthCheck (30 lines)
*/
public MsgOf(
final String from, final Set<String> to, final Set<String> cc, final Set<String> bcc,
Expand All @@ -99,13 +98,28 @@ public MsgOf(
* @param subj The subject of the email message.
* @param body The message content.
* @param attachments The message attachments.
* @checkstyle ParameterNameCheck (10 lines)
* @checkstyle ParameterNumberCheck (5 lines)
* @checkstyle AnonInnerLengthCheck (30 lines)
*/
public MsgOf(
final Text from, final Set<Text> to, final Set<Text> cc, final Set<Text> bcc,
final Text subj, final Text body, final Iterable<File> attachments
) {
this(from, to, cc, bcc, subj, body, new CollectionOf<>(attachments));
}

/**
* Ctor.
* @param from The sender's email address.
* @param to The target email recipients.
* @param cc The target email recipients for the `CC` (carbon copy).
* @param bcc The target email recipients for the `BCC` (blind carbon copy).
* @param subj The subject of the email message.
* @param body The message content.
* @param attachments The message attachments.
* @checkstyle AnonInnerLengthCheck (30 lines)
*/
private MsgOf(
final Text from, final Set<Text> to, final Set<Text> cc, final Set<Text> bcc,
final Text subj, final Text body, final Collection<File> attachments
) {
super(
new Msg() {
Expand Down Expand Up @@ -140,7 +154,7 @@ public Text body() {
}

@Override
public Iterable<File> attachments() {
public Collection<File> attachments() {
return attachments;
}
}
Expand Down
20 changes: 18 additions & 2 deletions src/main/java/io/github/dgroup/mbox4j/outbox/javax/MimeMsg.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,24 @@
package io.github.dgroup.mbox4j.outbox.javax;

import io.github.dgroup.mbox4j.Msg;
import java.io.File;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import org.cactoos.BiFunc;

/**
* The function to evaluate the {@link javax.mail.internet.MimeMessage}.
* from {@link Msg}.
*
* @since 0.1.0
* @todo #/DEV Add attachments to the {@link javax.mail.Message}.
* For now it sends the 'body' only thus files are skipped.
*/
public final class MimeMsg implements BiFunc<Session, Msg, Message> {

Expand All @@ -50,6 +55,17 @@ public Message apply(final Session session, final Msg msg) throws Exception {
email.setRecipients(Message.RecipientType.BCC, new Addresses(msg.bcc()).value());
email.setSubject(msg.subject().asString());
email.setText(msg.body().asString());
if (!msg.attachments().isEmpty()) {
final Multipart multipart = new MimeMultipart();
for (final File attachment : msg.attachments()) {
final MimeBodyPart part = new MimeBodyPart();
final DataSource source = new FileDataSource(attachment);
part.setDataHandler(new DataHandler(source));
part.setFileName(attachment.getName());
multipart.addBodyPart(part);
}
email.setContent(multipart);
}
return email;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,18 @@
import io.github.dgroup.mbox4j.msg.MsgOf;
import io.github.dgroup.term4j.arg.ArgNotFoundException;
import io.github.dgroup.term4j.arg.PropOf;
import java.io.File;
import java.util.Collections;
import java.util.Set;
import org.cactoos.set.SetOf;
import org.junit.Test;

/**
* Test case for {@link JavaxMailOutbox}.
*
* @since 0.1.0
* @checkstyle JavadocMethodCheck (500 lines)
* @checkstyle LocalFinalVariableNameCheck (500 lines)
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public final class JavaxMailOutboxTestIT {
Expand All @@ -48,15 +53,15 @@ public final class JavaxMailOutboxTestIT {
*/
@Test
public void sendFromYandexAccount() throws EmailException, ArgNotFoundException {
final String from = new PropOf("LL.yandex.user").value();
final Set<String> to = new SetOf<>(new PropOf("LL.yandex.to.user").value());
final Set<String> cc = Collections.emptySet();
final Set<String> bcc = Collections.emptySet();
final Set<File> attachments = new SetOf<>(new File(".gitignore"), new File(".pdd"));
new JavaxMailOutbox(
new YandexOutgoingSmtpProperties()
).send(
new MsgOf(
new PropOf("LL.yandex.user").value(),
new PropOf("LL.yandex.to.user").value(),
"Testing subj",
"I'm simple and i know it."
)
new MsgOf(from, to, cc, bcc, "Testing subj", "I'm simple and i know it.", attachments)
);
}
}

1 comment on commit 6b04890

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 6b04890 Mar 31, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle DEV-b732933e disappeared from src/main/java/io/github/dgroup/mbox4j/outbox/javax/MimeMsg.java, that's why I closed #18. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

Please sign in to comment.