diff --git a/src/main/java/io/github/dgroup/mbox4j/Msg.java b/src/main/java/io/github/dgroup/mbox4j/Msg.java index 0065573..5fb98bc 100644 --- a/src/main/java/io/github/dgroup/mbox4j/Msg.java +++ b/src/main/java/io/github/dgroup/mbox4j/Msg.java @@ -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; @@ -77,5 +78,5 @@ public interface Msg { * The attachments within the email. * @return The attachments. */ - Iterable attachments(); + Collection attachments(); } diff --git a/src/main/java/io/github/dgroup/mbox4j/msg/FakeMsg.java b/src/main/java/io/github/dgroup/mbox4j/msg/FakeMsg.java index d16c091..4796e6a 100644 --- a/src/main/java/io/github/dgroup/mbox4j/msg/FakeMsg.java +++ b/src/main/java/io/github/dgroup/mbox4j/msg/FakeMsg.java @@ -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 recipients; + private final Set to; /** * The target email recipients for the `CC` (carbon copy). */ - private final Set copy; + private final Set cc; /** * The target email recipients for the `BCC` (blind carbon copy). */ - private final Set bcopy; + private final Set bcc; /** * The subject of the email message. @@ -71,50 +75,67 @@ public final class FakeMsg implements Msg { /** * The message attachments. */ - private final Iterable attachs; + private final Collection 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 recipients, final Set copy, final Set bcopy, - final Text subj, final Text content, final Iterable attachs + final Text from, final Set to, final Set cc, final Set 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 to, final Set cc, final Set bcc, + final Text subj, final Text body, final Collection 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 to() { - return this.recipients; + return this.to; } @Override public Set cc() { - return this.copy; + return this.cc; } @Override public Set bcc() { - return this.bcopy; + return this.bcc; } @Override @@ -128,8 +149,7 @@ public Text body() { } @Override - public Iterable attachments() { - return this.attachs; + public Collection attachments() { + return this.attachments; } - } diff --git a/src/main/java/io/github/dgroup/mbox4j/msg/MsgEnvelope.java b/src/main/java/io/github/dgroup/mbox4j/msg/MsgEnvelope.java index 9d186c6..1f3e3e3 100644 --- a/src/main/java/io/github/dgroup/mbox4j/msg/MsgEnvelope.java +++ b/src/main/java/io/github/dgroup/mbox4j/msg/MsgEnvelope.java @@ -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; @@ -85,7 +86,7 @@ public final Text body() { } @Override - public final Iterable attachments() { + public final Collection attachments() { return this.origin.attachments(); } diff --git a/src/main/java/io/github/dgroup/mbox4j/msg/MsgOf.java b/src/main/java/io/github/dgroup/mbox4j/msg/MsgOf.java index 6381c70..525379c 100644 --- a/src/main/java/io/github/dgroup/mbox4j/msg/MsgOf.java +++ b/src/main/java/io/github/dgroup/mbox4j/msg/MsgOf.java @@ -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; @@ -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 { @@ -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( @@ -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 to, final Set cc, final Set bcc, @@ -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 to, final Set cc, final Set bcc, final Text subj, final Text body, final Iterable 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 to, final Set cc, final Set bcc, + final Text subj, final Text body, final Collection attachments ) { super( new Msg() { @@ -140,7 +154,7 @@ public Text body() { } @Override - public Iterable attachments() { + public Collection attachments() { return attachments; } } diff --git a/src/main/java/io/github/dgroup/mbox4j/outbox/javax/MimeMsg.java b/src/main/java/io/github/dgroup/mbox4j/outbox/javax/MimeMsg.java index c959048..5b66bc2 100644 --- a/src/main/java/io/github/dgroup/mbox4j/outbox/javax/MimeMsg.java +++ b/src/main/java/io/github/dgroup/mbox4j/outbox/javax/MimeMsg.java @@ -25,10 +25,17 @@ 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; /** @@ -36,8 +43,6 @@ * 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 { @@ -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; } } diff --git a/src/test/java/io/github/dgroup/mbox4j/outbox/javax/JavaxMailOutboxTestIT.java b/src/test/java/io/github/dgroup/mbox4j/outbox/javax/JavaxMailOutboxTestIT.java index fc6f88d..03dd04e 100644 --- a/src/test/java/io/github/dgroup/mbox4j/outbox/javax/JavaxMailOutboxTestIT.java +++ b/src/test/java/io/github/dgroup/mbox4j/outbox/javax/JavaxMailOutboxTestIT.java @@ -29,6 +29,10 @@ 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; /** @@ -36,6 +40,7 @@ * * @since 0.1.0 * @checkstyle JavadocMethodCheck (500 lines) + * @checkstyle LocalFinalVariableNameCheck (500 lines) */ @SuppressWarnings("PMD.AvoidDuplicateLiterals") public final class JavaxMailOutboxTestIT { @@ -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 to = new SetOf<>(new PropOf("LL.yandex.to.user").value()); + final Set cc = Collections.emptySet(); + final Set bcc = Collections.emptySet(); + final Set 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) ); } }