| 1 | package de.aikiit.mailversendala; | |
| 2 | ||
| 3 | import com.google.common.base.Charsets; | |
| 4 | import com.google.common.base.Strings; | |
| 5 | import de.aikiit.mailversendala.csv.Mailing; | |
| 6 | import org.apache.commons.mail.*; | |
| 7 | import org.apache.logging.log4j.LogManager; | |
| 8 | import org.apache.logging.log4j.Logger; | |
| 9 | ||
| 10 | import java.net.MalformedURLException; | |
| 11 | import java.net.URL; | |
| 12 | import java.util.Date; | |
| 13 | import java.util.Locale; | |
| 14 | ||
| 15 | /** | |
| 16 |  * This class performs the actual mail sending and encapsulates all the technicalities to create and send out an email. | |
| 17 |  */ | |
| 18 | public class SendOut { | |
| 19 |     private static final Logger LOG = | |
| 20 |             LogManager.getLogger(SendOut.class); | |
| 21 | ||
| 22 |     // is transformed into User-Agent-Header | |
| 23 |     private static final String SENT_BY = "X-Mailer"; | |
| 24 |     private static final String SENT_BY_ME = "Mailversendala - https://github.com/ottlinger/mailversendala"; | |
| 25 | ||
| 26 |     private final Email email; | |
| 27 |     private final HtmlEmail htmlEmail; | |
| 28 |     private final MailConfig mailConfig; | |
| 29 |     private final Mailing recipient; | |
| 30 | ||
| 31 |     /** | |
| 32 |      * Create mail based on the given mailing recipient. | |
| 33 |      * @param recipient mailing contents and parameters. | |
| 34 |      */ | |
| 35 |     public SendOut(Mailing recipient) { | |
| 36 |         this.recipient = recipient; | |
| 37 |         this.mailConfig = new MailConfig(); | |
| 38 | ||
| 39 | 1
1. <init> : negated conditional → SURVIVED |         if (mailConfig.sendOutMails()) { | 
| 40 |             LOG.warn("Running in DEMO mode, thus no mails will be sent out."); | |
| 41 |         } | |
| 42 | ||
| 43 |         LOG.info("Initialized authentication."); | |
| 44 |         DefaultAuthenticator authenticator = new DefaultAuthenticator(mailConfig.getUsername(), mailConfig.getPassword()); | |
| 45 | ||
| 46 |         int port = mailConfig.getPort(); | |
| 47 | ||
| 48 |         this.email = new SimpleEmail(); | |
| 49 | 1
1. <init> : removed call to org/apache/commons/mail/Email::setHostName → SURVIVED |         email.setHostName(mailConfig.getHost()); | 
| 50 | 1
1. <init> : removed call to org/apache/commons/mail/Email::setSmtpPort → SURVIVED |         email.setSmtpPort(port); | 
| 51 | 1
1. <init> : removed call to org/apache/commons/mail/Email::setAuthenticator → SURVIVED |         email.setAuthenticator(authenticator); | 
| 52 |         email.setSSLCheckServerIdentity(true); | |
| 53 | 1
1. <init> : negated conditional → SURVIVED |         if (465 == port) { | 
| 54 |             LOG.info("Using SSL."); | |
| 55 |             email.setSSLOnConnect(true); | |
| 56 |         } else { | |
| 57 |             LOG.info("Using TLS."); | |
| 58 |             email.setStartTLSRequired(true); | |
| 59 |         } | |
| 60 |         email.setBounceAddress(mailConfig.getFrom()); | |
| 61 | 1
1. <init> : removed call to org/apache/commons/mail/Email::addHeader → SURVIVED |         email.addHeader(SENT_BY, SENT_BY_ME); | 
| 62 |         LOG.info("Simple email initialized."); | |
| 63 | ||
| 64 |         this.htmlEmail = new HtmlEmail(); | |
| 65 | 1
1. <init> : removed call to org/apache/commons/mail/HtmlEmail::setHostName → SURVIVED |         htmlEmail.setHostName(mailConfig.getHost()); | 
| 66 | 1
1. <init> : removed call to org/apache/commons/mail/HtmlEmail::setSmtpPort → SURVIVED |         htmlEmail.setSmtpPort(port); | 
| 67 |         htmlEmail.setSSLCheckServerIdentity(true); | |
| 68 | 1
1. <init> : negated conditional → SURVIVED |         if (465 == port) { | 
| 69 |             LOG.info("Using SSL."); | |
| 70 |             htmlEmail.setSSLOnConnect(true); | |
| 71 |         } else { | |
| 72 |             LOG.info("Using TLS."); | |
| 73 |             htmlEmail.setStartTLSRequired(true); | |
| 74 |         } | |
| 75 |         htmlEmail.setBounceAddress(mailConfig.getFrom()); | |
| 76 | 1
1. <init> : removed call to org/apache/commons/mail/HtmlEmail::setAuthenticator → SURVIVED |         htmlEmail.setAuthenticator(authenticator); | 
| 77 | 1
1. <init> : removed call to org/apache/commons/mail/HtmlEmail::addHeader → SURVIVED |         htmlEmail.addHeader(SENT_BY, SENT_BY_ME); | 
| 78 |         LOG.info("HTML and text email initialized."); | |
| 79 |     } | |
| 80 | ||
| 81 |     /** | |
| 82 |      * Test method to verify the actual mail creation and send out. | |
| 83 |      * @param args allows sending an email to a given mail address. One parameter is required. | |
| 84 |      * @throws Exception in case of errors. | |
| 85 |      */ | |
| 86 |     public static void main(String... args) throws Exception { | |
| 87 | 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])) { | 
| 88 |             LOG.error("Please call this method with a mail address to send to."); | |
| 89 |         } else { | |
| 90 |             String email = args[0]; | |
| 91 |             LOG.warn("Init: Using mail address from runtime parameter: {}", email); | |
| 92 | ||
| 93 |             Mailing mailing = Mailing.builder().email(email).firstname("Your name").surname("Is my name").language(Locale.GERMAN.getLanguage()).build(); | |
| 94 |             SendOut sendOut = new SendOut(mailing); | |
| 95 |             LOG.info("Init: DONE"); | |
| 96 | 1
1. main : removed call to de/aikiit/mailversendala/SendOut::send → NO_COVERAGE |             sendOut.send(); | 
| 97 |             LOG.info("Send simple text-based message: DONE"); | |
| 98 | 1
1. main : removed call to de/aikiit/mailversendala/SendOut::sendComplex → NO_COVERAGE |             sendOut.sendComplex(); | 
| 99 |             LOG.info("Send complex HTML and text-based message: DONE"); | |
| 100 |         } | |
| 101 |     } | |
| 102 | ||
| 103 |     /** | |
| 104 |      * Send out an email that contains only text contents. | |
| 105 |      * @throws EmailException in case of errors. | |
| 106 |      */ | |
| 107 |     public void send() throws EmailException { | |
| 108 |         email.setFrom(mailConfig.getFrom()); | |
| 109 |         email.addTo(this.recipient.getEmail()); | |
| 110 | ||
| 111 | 1
1. send : removed call to org/apache/commons/mail/Email::setCharset → SURVIVED |         email.setCharset(Charsets.UTF_8.name()); | 
| 112 | ||
| 113 |         email.setSubject(mailConfig.getSubject() + " " + new Date()); | |
| 114 |         email.setMsg("This is a test mail from Mailversendala... :-)"); | |
| 115 | 1
1. send : negated conditional → KILLED |         if (mailConfig.sendOutMails()) { | 
| 116 |             email.send(); | |
| 117 |         } | |
| 118 |     } | |
| 119 | ||
| 120 |     /** | |
| 121 |      * Send out an email with plain text and HTML contents. | |
| 122 |      * | |
| 123 |      * @throws EmailException in case of mail-related errors. | |
| 124 |      * @throws MalformedURLException in case of problem while generating the HTML-part of. | |
| 125 |      */ | |
| 126 |     public void sendComplex() throws EmailException, MalformedURLException { | |
| 127 |         htmlEmail.addTo(this.recipient.getEmail(), this.recipient.getFirstname() + " " + this.recipient.getSurname()); | |
| 128 |         htmlEmail.setFrom(mailConfig.getFrom(), "Me"); | |
| 129 |         htmlEmail.setSubject("HTML" + mailConfig.getSubject() + " " + new Date()); | |
| 130 | ||
| 131 |         // embed the image and get the content id | |
| 132 |         URL url = new URL("https://www.apache.org/images/asf_logo_wide.gif"); | |
| 133 |         String cid = htmlEmail.embed(url, "ASF logo"); | |
| 134 | ||
| 135 |         // set the html message | |
| 136 |         htmlEmail.setHtmlMsg("<html>The ASF logo - <img src=\"cid:" + cid + "\"></html>"); | |
| 137 | ||
| 138 |         // set the alternative message | |
| 139 |         htmlEmail.setTextMsg("Your email client does not support HTML messages - thus no ASF logo visible."); | |
| 140 | ||
| 141 | 1
1. sendComplex : negated conditional → KILLED |         if (mailConfig.sendOutMails()) { | 
| 142 |             htmlEmail.send(); | |
| 143 |         } | |
| 144 |     } | |
| 145 | } | |
| Mutations | ||
| 39 | 1.1 | |
| 49 | 1.1 | |
| 50 | 1.1 | |
| 51 | 1.1 | |
| 53 | 1.1 | |
| 61 | 1.1 | |
| 65 | 1.1 | |
| 66 | 1.1 | |
| 68 | 1.1 | |
| 76 | 1.1 | |
| 77 | 1.1 | |
| 87 | 1.1 2.2 3.3 4.4 | |
| 96 | 1.1 | |
| 98 | 1.1 | |
| 111 | 1.1 | |
| 115 | 1.1 | |
| 141 | 1.1 |