BrightSide Workbench Full Report + Source Code
MailQueueItem.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.mail.pool;
20 
21 import java.net.MalformedURLException;
22 import java.util.function.Consumer;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25 import javax.mail.internet.InternetAddress;
26 import javax.naming.NamingException;
27 import org.apache.commons.mail.EmailException;
28 import org.turro.elephant.context.ElephantContext;
29 import org.turro.mail.message.MailMessage;
30 
35 public class MailQueueItem {
36 
37  private final MailMessage mail;
38  private final Consumer onFinish;
39 
40  public MailQueueItem(MailMessage mail, Consumer onFinish) {
41  this.mail = mail;
42  this.onFinish = onFinish;
43  }
44 
45  public void send() {
46  if(mail == null) return;
47  try {
48  mail.send();
49  Logger.getLogger(MailQueueItem.class.getName()).log(Level.INFO, "Sent mail from {0} to {1}. Subject: {2}",
50  new Object[]{mail.getEmail().getFromAddress().getAddress(),
51  InternetAddress.toString((InternetAddress[]) mail.getEmail().getToAddresses()
52  .toArray(new InternetAddress[0])), mail.getSubject()});
53  } catch (MalformedURLException | NamingException | EmailException ex) {
54  Logger.getLogger(MailQueueItem.class.getName()).log(Level.SEVERE, ElephantContext.logMsg("Sending"), ex);
55  }
56  if(onFinish != null) {
57  try {
58  onFinish.accept(mail);
59  } catch (Exception ex) {
60  Logger.getLogger(MailQueueItem.class.getName()).log(Level.SEVERE, ElephantContext.logMsg("onFinish"), ex);
61  }
62  }
63 
64  }
65 
66 }
MailQueueItem(MailMessage mail, Consumer onFinish)