SendOut.java

1
package de.aikiit.mailversendala;
2
3
/*
4
 * Copyright (c) P. Ottlinger
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *   https://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing,
13
 * software distributed under the License is distributed on an
14
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
 * KIND, either express or implied.  See the License for the
16
 * specific language governing permissions and limitations
17
 * under the License.
18
 */
19
20
21
import com.google.common.base.Strings;
22
import de.aikiit.mailversendala.csv.Mailing;
23
import de.aikiit.mailversendala.template.MailResources;
24
import de.aikiit.mailversendala.template.MailTemplate;
25
import de.aikiit.mailversendala.template.VelocityMailTemplate;
26
import org.apache.commons.mail2.core.EmailException;
27
import org.apache.commons.mail2.jakarta.DefaultAuthenticator;
28
import org.apache.commons.mail2.jakarta.Email;
29
import org.apache.commons.mail2.jakarta.HtmlEmail;
30
import org.apache.commons.mail2.jakarta.SimpleEmail;
31
import org.apache.logging.log4j.Logger;
32
33
import java.io.IOException;
34
import java.net.MalformedURLException;
35
import java.net.URL;
36
import java.nio.charset.StandardCharsets;
37
import java.util.Date;
38
39
/**
40
 * This class performs the actual mail sending and encapsulates all the technicalities to create and send out an email.
41
 */
42
public class SendOut {
43
    private static final Logger LOG =
44
            RunLogging.getLogger(SendOut.class);
45
46
    // is transformed into User-Agent-Header
47
    private static final String SENT_BY = "X-Mailer";
48
    private static final String SENT_BY_ME = "Mailversendala - https://github.com/ottlinger/mailversendala";
49
50
    private final Email email;
51
    private final HtmlEmail htmlEmail;
52
    private final MailConfig mailConfig;
53
    private final Mailing recipient;
54
55
    /**
56
     * Create mail based on the given mailing recipient.
57
     * @param recipient mailing contents and parameters.
58
     */
59
    public SendOut(Mailing recipient) {
60
        this(recipient, new MailConfig());
61
    }
62
63
    /**
64
     * Create a sender using the same configuration as the mailing run.
65
     * @param recipient mailing recipient.
66
     * @param mailConfig SMTP and message configuration.
67
     */
68
    public SendOut(Mailing recipient, MailConfig mailConfig) {
69
        this(recipient, mailConfig, new SimpleEmail(), new HtmlEmail());
70
    }
71
72
    /**
73
     * Configure supplied mail clients, allowing tests to use isolated mail sessions.
74
     */
75
    SendOut(Mailing recipient, MailConfig mailConfig, Email email, HtmlEmail htmlEmail) {
76
        this.recipient = recipient;
77
        this.mailConfig = mailConfig;
78
        this.email = email;
79
        this.htmlEmail = htmlEmail;
80
81 1 1. <init> : negated conditional → SURVIVED
        if (!mailConfig.sendOutMails()) {
82
            LOG.debug("Running in DEMO mode, thus no mails will be sent out.");
83
        }
84
85
        LOG.debug("Initializing SMTP clients with {}.", mailConfig.getPort() == 465 ? "SMTPS" : "STARTTLS");
86
        DefaultAuthenticator authenticator = new DefaultAuthenticator(mailConfig.getUsername(), mailConfig.getPassword());
87
88
        int port = mailConfig.getPort();
89
90 1 1. <init> : removed call to org/apache/commons/mail2/jakarta/Email::setHostName → KILLED
        email.setHostName(mailConfig.getHost());
91 1 1. <init> : removed call to org/apache/commons/mail2/jakarta/Email::setSmtpPort → KILLED
        email.setSmtpPort(port);
92 1 1. <init> : removed call to org/apache/commons/mail2/jakarta/Email::setAuthenticator → KILLED
        email.setAuthenticator(authenticator);
93
        email.setSSLCheckServerIdentity(true);
94 1 1. <init> : negated conditional → KILLED
        if (465 == port) {
95
            email.setSSLOnConnect(true);
96
        } else {
97
            email.setStartTLSEnabled(true);
98
            email.setStartTLSRequired(true);
99
        }
100
        email.setBounceAddress(mailConfig.getFrom());
101 1 1. <init> : removed call to org/apache/commons/mail2/jakarta/Email::addHeader → KILLED
        email.addHeader(SENT_BY, SENT_BY_ME);
102
        LOG.debug("Simple email initialized.");
103
104 1 1. <init> : removed call to org/apache/commons/mail2/jakarta/HtmlEmail::setHostName → KILLED
        htmlEmail.setHostName(mailConfig.getHost());
105 1 1. <init> : removed call to org/apache/commons/mail2/jakarta/HtmlEmail::setSmtpPort → SURVIVED
        htmlEmail.setSmtpPort(port);
106
        htmlEmail.setSSLCheckServerIdentity(true);
107 1 1. <init> : negated conditional → KILLED
        if (465 == port) {
108
            htmlEmail.setSSLOnConnect(true);
109
        } else {
110
            htmlEmail.setStartTLSEnabled(true);
111
            htmlEmail.setStartTLSRequired(true);
112
        }
113
        htmlEmail.setBounceAddress(mailConfig.getFrom());
114 1 1. <init> : removed call to org/apache/commons/mail2/jakarta/HtmlEmail::setAuthenticator → SURVIVED
        htmlEmail.setAuthenticator(authenticator);
115 1 1. <init> : removed call to org/apache/commons/mail2/jakarta/HtmlEmail::addHeader → KILLED
        htmlEmail.addHeader(SENT_BY, SENT_BY_ME);
116
        LOG.debug("HTML and text email initialized.");
117
    }
118
119
    /**
120
     * Test method to verify the actual mail creation and send out.
121
     * @param args allows sending an email to a given mail address. One parameter is required.
122
     * @throws Exception in case of errors.
123
     */
124
    public static void main(String... args) throws Exception {
125 4 1. main : negated conditional → SURVIVED
2. main : changed conditional boundary → SURVIVED
3. main : negated conditional → KILLED
4. main : negated conditional → KILLED
        if (args == null || args.length < 1 || Strings.isNullOrEmpty(args[0])) {
126
            LOG.error("Please call this method with a mail address to send to.");
127
        } else {
128
            String email = args[0];
129
            LOG.warn("Init: Using mail address from runtime parameter: {}", email);
130
131
            Mailing mailing = Mailing.builder().email(email).firstname("Your name").surname("Is my name").build();
132
            MailConfig configuration = new MailConfig();
133
            LOG.info("SMTP transport: {}", configuration.getPort() == 465 ? "SMTPS" : "STARTTLS");
134 1 1. main : negated conditional → NO_COVERAGE
            if (!configuration.sendOutMails()) {
135
                LOG.warn("Running in DEMO mode, thus no mails will be sent out.");
136
            }
137
            VelocityMailTemplate templates = new VelocityMailTemplate(configuration);
138
            SendOut sendOut = new SendOut(mailing, configuration);
139 1 1. main : removed call to de/aikiit/mailversendala/SendOut::send → NO_COVERAGE
            sendOut.send(templates.render(mailing));
140
            LOG.info("Send HTML and plain-text message: DONE");
141
        }
142
    }
143
144
    /**
145
     * Send a rendered message with HTML and plain-text alternatives in UTF-8.
146
     * @param template rendered message bodies.
147
     * @throws EmailException if message construction or delivery fails.
148
     */
149
    public void send(MailTemplate template) throws EmailException {
150
        try {
151 1 1. send : removed call to de/aikiit/mailversendala/SendOut::send → KILLED
            send(template, new MailResources(mailConfig));
152
        } catch (IOException e) {
153
            throw new EmailException("Cannot load mail resources", e);
154
        }
155
    }
156
157
    /** Send using the resources validated and snapshotted for the entire run. */
158
    void send(MailTemplate template, MailResources resources) throws EmailException {
159 1 1. send : removed call to org/apache/commons/mail2/jakarta/HtmlEmail::setCharset → KILLED
        htmlEmail.setCharset(StandardCharsets.UTF_8.name());
160
        htmlEmail.setFrom(mailConfig.getFrom());
161 1 1. send : removed call to de/aikiit/mailversendala/SendOut::configureReplyTo → KILLED
        configureReplyTo(htmlEmail);
162
        htmlEmail.addTo(recipient.email());
163
        htmlEmail.setSubject(mailConfig.getSubject());
164 1 1. send : removed call to de/aikiit/mailversendala/template/MailResources::applyTo → KILLED
        resources.applyTo(htmlEmail, template);
165 1 1. send : negated conditional → KILLED
        if (mailConfig.sendOutMails()) {
166
            htmlEmail.send();
167
        }
168
    }
169
170
    private void configureReplyTo(Email message) throws EmailException {
171
        String replyTo = mailConfig.getReplyTo();
172 2 1. configureReplyTo : negated conditional → KILLED
2. configureReplyTo : negated conditional → KILLED
        if (replyTo != null && !replyTo.isBlank()) {
173
            message.addReplyTo(replyTo.trim());
174
        }
175
    }
176
177
    /**
178
     * Send out an email that contains only text contents.
179
     * @throws EmailException in case of errors.
180
     */
181
    public void send() throws EmailException {
182
        email.setFrom(mailConfig.getFrom());
183 1 1. send : removed call to de/aikiit/mailversendala/SendOut::configureReplyTo → KILLED
        configureReplyTo(email);
184
        email.addTo(this.recipient.email());
185
186 1 1. send : removed call to org/apache/commons/mail2/jakarta/Email::setCharset → KILLED
        email.setCharset(StandardCharsets.UTF_8.name());
187
188
        email.setSubject(mailConfig.getSubject() + " " + new Date());
189
        email.setMsg("This is a test mail from Mailversendala... :-)");
190 1 1. send : negated conditional → KILLED
        if (mailConfig.sendOutMails()) {
191
            email.send();
192
        }
193
    }
194
195
    /**
196
     * Send out an email with plain text and HTML contents.
197
     *
198
     * @throws EmailException in case of mail-related errors.
199
     * @throws MalformedURLException in case of problem while generating the HTML-part of.
200
     */
201
    public void sendComplex() throws EmailException, MalformedURLException {
202 1 1. sendComplex : removed call to de/aikiit/mailversendala/SendOut::sendComplex → NO_COVERAGE
        sendComplex(new URL("https://www.apache.org/images/asf_logo_wide.gif"));
203
    }
204
205
    /**
206
     * Send HTML mail using the supplied inline image, without requiring a remote resource.
207
     */
208
    void sendComplex(URL imageUrl) throws EmailException {
209
        htmlEmail.addTo(this.recipient.email(), this.recipient.firstname() + " " + this.recipient.surname());
210
        htmlEmail.setFrom(mailConfig.getFrom(), "Me");
211 1 1. sendComplex : removed call to de/aikiit/mailversendala/SendOut::configureReplyTo → KILLED
        configureReplyTo(htmlEmail);
212
        htmlEmail.setSubject("HTML" + mailConfig.getSubject() + " " + new Date());
213
214
        // embed the image and get the content id
215
        String cid = htmlEmail.embed(imageUrl, "ASF logo");
216
217
        // set the html message
218
        htmlEmail.setHtmlMsg("<html>The ASF logo - <img src=\"cid:" + cid + "\"></html>");
219
220
        // set the alternative message
221
        htmlEmail.setTextMsg("Your email client does not support HTML messages - thus no ASF logo visible.");
222
223 1 1. sendComplex : negated conditional → KILLED
        if (mailConfig.sendOutMails()) {
224
            htmlEmail.send();
225
        }
226
    }
227
}

Mutations

81

1.1
Location : <init>
Killed by : none
negated conditional → SURVIVED
Covering tests

90

1.1
Location : <init>
Killed by : de.aikiit.mailversendala.SendOutGreenMailTest.[engine:junit-jupiter]/[class:de.aikiit.mailversendala.SendOutGreenMailTest]/[method:demoModeDoesNotDeliverEitherMessage()]
removed call to org/apache/commons/mail2/jakarta/Email::setHostName → KILLED

91

1.1
Location : <init>
Killed by : de.aikiit.mailversendala.SendOutGreenMailTest.[engine:junit-jupiter]/[class:de.aikiit.mailversendala.SendOutGreenMailTest]/[method:refusesPlaintextFallbackWhenServerDoesNotSupportStartTls()]
removed call to org/apache/commons/mail2/jakarta/Email::setSmtpPort → KILLED

92

1.1
Location : <init>
Killed by : de.aikiit.mailversendala.SendOutGreenMailTest.[engine:junit-jupiter]/[class:de.aikiit.mailversendala.SendOutGreenMailTest]/[method:rejectsInvalidSmtpCredentials()]
removed call to org/apache/commons/mail2/jakarta/Email::setAuthenticator → KILLED

94

1.1
Location : <init>
Killed by : de.aikiit.mailversendala.SendOutGreenMailTest.[engine:junit-jupiter]/[class:de.aikiit.mailversendala.SendOutGreenMailTest]/[method:rejectsInvalidSmtpCredentials()]
negated conditional → KILLED

101

1.1
Location : <init>
Killed by : de.aikiit.mailversendala.SendOutGreenMailTest.[engine:junit-jupiter]/[class:de.aikiit.mailversendala.SendOutGreenMailTest]/[method:deliversPlainTextOverAuthenticatedSmtps()]
removed call to org/apache/commons/mail2/jakarta/Email::addHeader → KILLED

104

1.1
Location : <init>
Killed by : de.aikiit.mailversendala.SendOutGreenMailTest.[engine:junit-jupiter]/[class:de.aikiit.mailversendala.SendOutGreenMailTest]/[method:demoModeDoesNotDeliverEitherMessage()]
removed call to org/apache/commons/mail2/jakarta/HtmlEmail::setHostName → KILLED

105

1.1
Location : <init>
Killed by : none
removed call to org/apache/commons/mail2/jakarta/HtmlEmail::setSmtpPort → SURVIVED
Covering tests

107

1.1
Location : <init>
Killed by : de.aikiit.mailversendala.SendOutGreenMailTest.[engine:junit-jupiter]/[class:de.aikiit.mailversendala.SendOutGreenMailTest]/[test-template:deliversConfiguredReplyToForEveryMessageType(java.lang.String, java.nio.file.Path)]/[test-template-invocation:#2]
negated conditional → KILLED

114

1.1
Location : <init>
Killed by : none
removed call to org/apache/commons/mail2/jakarta/HtmlEmail::setAuthenticator → SURVIVED
Covering tests

115

1.1
Location : <init>
Killed by : de.aikiit.mailversendala.SendOutGreenMailTest.[engine:junit-jupiter]/[class:de.aikiit.mailversendala.SendOutGreenMailTest]/[test-template:deliversConfiguredReplyToForEveryMessageType(java.lang.String, java.nio.file.Path)]/[test-template-invocation:#2]
removed call to org/apache/commons/mail2/jakarta/HtmlEmail::addHeader → KILLED

125

1.1
Location : main
Killed by : de.aikiit.mailversendala.SendOutTest.[engine:junit-jupiter]/[class:de.aikiit.mailversendala.SendOutTest]/[method:whenSendOutLacksRuntimeParameterNothingHappens()]
negated conditional → KILLED

2.2
Location : main
Killed by : de.aikiit.mailversendala.SendOutTest.[engine:junit-jupiter]/[class:de.aikiit.mailversendala.SendOutTest]/[method:whenSendOutLacksRuntimeParameterNothingHappens()]
negated conditional → KILLED

3.3
Location : main
Killed by : none
negated conditional → SURVIVED
Covering tests

4.4
Location : main
Killed by : none
changed conditional boundary → SURVIVED Covering tests

134

1.1
Location : main
Killed by : none
negated conditional → NO_COVERAGE

139

1.1
Location : main
Killed by : none
removed call to de/aikiit/mailversendala/SendOut::send → NO_COVERAGE

151

1.1
Location : send
Killed by : de.aikiit.mailversendala.SendOutGreenMailTest.[engine:junit-jupiter]/[class:de.aikiit.mailversendala.SendOutGreenMailTest]/[test-template:rejectsInvalidReplyToBeforeDelivery(java.lang.String, java.nio.file.Path)]/[test-template-invocation:#3]
removed call to de/aikiit/mailversendala/SendOut::send → KILLED

159

1.1
Location : send
Killed by : de.aikiit.mailversendala.SendOutGreenMailTest.[engine:junit-jupiter]/[class:de.aikiit.mailversendala.SendOutGreenMailTest]/[method:deliversExternalVelocityTemplatesAsUtf8Alternatives(java.nio.file.Path)]
removed call to org/apache/commons/mail2/jakarta/HtmlEmail::setCharset → KILLED

161

1.1
Location : send
Killed by : de.aikiit.mailversendala.SendOutGreenMailTest.[engine:junit-jupiter]/[class:de.aikiit.mailversendala.SendOutGreenMailTest]/[test-template:rejectsInvalidReplyToBeforeDelivery(java.lang.String, java.nio.file.Path)]/[test-template-invocation:#3]
removed call to de/aikiit/mailversendala/SendOut::configureReplyTo → KILLED

164

1.1
Location : send
Killed by : de.aikiit.mailversendala.SendOutGreenMailTest.[engine:junit-jupiter]/[class:de.aikiit.mailversendala.SendOutGreenMailTest]/[test-template:deliversConfiguredReplyToForEveryMessageType(java.lang.String, java.nio.file.Path)]/[test-template-invocation:#3]
removed call to de/aikiit/mailversendala/template/MailResources::applyTo → KILLED

165

1.1
Location : send
Killed by : de.aikiit.mailversendala.SendOutGreenMailTest.[engine:junit-jupiter]/[class:de.aikiit.mailversendala.SendOutGreenMailTest]/[method:demoModeDoesNotDeliverRenderedTemplates(java.nio.file.Path)]
negated conditional → KILLED

172

1.1
Location : configureReplyTo
Killed by : de.aikiit.mailversendala.SendOutTest.[engine:junit-jupiter]/[class:de.aikiit.mailversendala.SendOutTest]/[method:sendOutWithoutSendingInTestMode()]
negated conditional → KILLED

2.2
Location : configureReplyTo
Killed by : de.aikiit.mailversendala.SendOutGreenMailTest.[engine:junit-jupiter]/[class:de.aikiit.mailversendala.SendOutGreenMailTest]/[test-template:rejectsInvalidReplyToBeforeDelivery(java.lang.String, java.nio.file.Path)]/[test-template-invocation:#2]
negated conditional → KILLED

183

1.1
Location : send
Killed by : de.aikiit.mailversendala.SendOutGreenMailTest.[engine:junit-jupiter]/[class:de.aikiit.mailversendala.SendOutGreenMailTest]/[test-template:rejectsInvalidReplyToBeforeDelivery(java.lang.String, java.nio.file.Path)]/[test-template-invocation:#1]
removed call to de/aikiit/mailversendala/SendOut::configureReplyTo → KILLED

186

1.1
Location : send
Killed by : de.aikiit.mailversendala.SendOutGreenMailTest.[engine:junit-jupiter]/[class:de.aikiit.mailversendala.SendOutGreenMailTest]/[method:deliversPlainTextOverAuthenticatedSmtps()]
removed call to org/apache/commons/mail2/jakarta/Email::setCharset → KILLED

190

1.1
Location : send
Killed by : de.aikiit.mailversendala.SendOutTest.[engine:junit-jupiter]/[class:de.aikiit.mailversendala.SendOutTest]/[method:sendOutWithoutSendingInTestMode()]
negated conditional → KILLED

202

1.1
Location : sendComplex
Killed by : none
removed call to de/aikiit/mailversendala/SendOut::sendComplex → NO_COVERAGE

211

1.1
Location : sendComplex
Killed by : de.aikiit.mailversendala.SendOutGreenMailTest.[engine:junit-jupiter]/[class:de.aikiit.mailversendala.SendOutGreenMailTest]/[test-template:rejectsInvalidReplyToBeforeDelivery(java.lang.String, java.nio.file.Path)]/[test-template-invocation:#2]
removed call to de/aikiit/mailversendala/SendOut::configureReplyTo → KILLED

223

1.1
Location : sendComplex
Killed by : de.aikiit.mailversendala.SendOutTest.[engine:junit-jupiter]/[class:de.aikiit.mailversendala.SendOutTest]/[method:sendOutHtmlMessageWithoutSendingInTestMode()]
negated conditional → KILLED

Active mutators

Tests examined


Report generated by PIT 1.30.2 support