001/*
002 * JGrapes Event Driven Framework
003 * Copyright (C) 2022 Michael N. Lipp
004 * 
005 * This program is free software; you can redistribute it and/or modify it 
006 * under the terms of the GNU General Public License as published by 
007 * the Free Software Foundation; either version 3 of the License, or 
008 * (at your option) any later version.
009 * 
010 * This program is distributed in the hope that it will be useful, but 
011 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
012 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
013 * for more details.
014 * 
015 * You should have received a copy of the GNU General Public License along 
016 * with this program; if not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jgrapes.mail.events;
020
021import jakarta.mail.Address;
022import jakarta.mail.BodyPart;
023import jakarta.mail.MessagingException;
024import jakarta.mail.internet.MimeMultipart;
025import java.util.HashMap;
026import java.util.List;
027import java.util.Map;
028import org.jgrapes.core.Channel;
029import org.jgrapes.core.Event;
030
031/**
032 * Indicates the arrival of a new message. Handler should delete
033 * the message after successful processing.
034 */
035public class SendMessage extends Event<Void> {
036
037    private Address from;
038    @SuppressWarnings({ "PMD.ShortVariable", "PMD.AvoidDuplicateLiterals" })
039    private Address[] to;
040    @SuppressWarnings("PMD.ShortVariable")
041    private Address[] cc = new Address[0];
042    private Address[] bcc = new Address[0];
043    @SuppressWarnings("PMD.UseConcurrentHashMap")
044    private final Map<String, String> headers = new HashMap<>();
045    private String subject;
046    private MimeMultipart content;
047
048    /**
049     * Creates a new event.
050     *
051     * @param channels the channels
052     */
053    public SendMessage(Channel... channels) {
054        super(channels);
055    }
056
057    /**
058     * Gets the from addresses.
059     *
060     * @return the from
061     */
062    public Address from() {
063        return from;
064    }
065
066    /**
067     * Sets the from.
068     *
069     * @param from the from to set
070     */
071    public SendMessage setFrom(Address from) {
072        this.from = from;
073        return this;
074    }
075
076    /**
077     * Gets the to addresses.
078     *
079     * @return the to
080     */
081    @SuppressWarnings({ "PMD.ShortMethodName",
082        "PMD.MethodReturnsInternalArray" })
083    public Address[] to() {
084        return to;
085    }
086
087    /**
088     * Sets the to addresses.
089     *
090     * @param to the to addresses to set
091     */
092    @SuppressWarnings({ "PMD.ShortVariable", "PMD.ArrayIsStoredDirectly" })
093    public SendMessage setTo(Address... to) {
094        this.to = to;
095        return this;
096    }
097
098    /**
099     * Sets the to addresses.
100     *
101     * @param to the to addresses to set
102     */
103    @SuppressWarnings("PMD.ShortVariable")
104    public SendMessage setTo(List<Address> to) {
105        this.to = to.toArray(new Address[0]);
106        return this;
107    }
108
109    /**
110     * Gets the cc addresses.
111     *
112     * @return the cc
113     */
114    @SuppressWarnings({ "PMD.ShortMethodName",
115        "PMD.MethodReturnsInternalArray" })
116    public Address[] cc() {
117        return cc;
118    }
119
120    /**
121     * Sets the cc addresses.
122     *
123     * @param cc the cc adresses to set
124     */
125    @SuppressWarnings({ "PMD.ShortVariable", "PMD.ArrayIsStoredDirectly" })
126    public SendMessage setCc(Address... cc) {
127        this.cc = cc;
128        return this;
129    }
130
131    /**
132     * Sets the cc addresses.
133     *
134     * @param cc the cc adresses to set
135     */
136    @SuppressWarnings("PMD.ShortVariable")
137    public SendMessage setCc(List<Address> cc) {
138        this.cc = cc.toArray(new Address[0]);
139        return this;
140    }
141
142    /**
143     * Gets the bcc addresses.
144     *
145     * @return the bcc
146     */
147    @SuppressWarnings("PMD.MethodReturnsInternalArray")
148    public Address[] bcc() {
149        return bcc;
150    }
151
152    /**
153     * Sets the bcc addresses.
154     *
155     * @param bcc the bcc addresses to set
156     */
157    @SuppressWarnings("PMD.ArrayIsStoredDirectly")
158    public SendMessage setBcc(Address... bcc) {
159        this.bcc = bcc;
160        return this;
161    }
162
163    /**
164     * Sets the bcc addresses.
165     *
166     * @param bcc the bcc addresses to set
167     */
168    public SendMessage setBcc(List<Address> bcc) {
169        this.bcc = bcc.toArray(new Address[0]);
170        return this;
171    }
172
173    /**
174     * Return the headers.
175     *
176     * @return the headers
177     */
178    public Map<String, String> headers() {
179        return headers;
180    }
181
182    /**
183     * Sets a header.
184     *
185     * @param name the name
186     * @param value the value
187     * @return the send mail message
188     */
189    public SendMessage setHeader(String name, String value) {
190        headers.put(name, value);
191        return this;
192    }
193
194    /**
195     * Gets the subject.
196     *
197     * @return the subject
198     */
199    public String subject() {
200        return subject;
201    }
202
203    /**
204     * Sets the subject.
205     *
206     * @param subject the subject to set
207     */
208    public SendMessage setSubject(String subject) {
209        this.subject = subject;
210        return this;
211    }
212
213    /**
214     * Returns the content.
215     *
216     * @return the mime multipart
217     */
218    public MimeMultipart content() {
219        return content;
220    }
221
222    /**
223     * Sets the content.
224     *
225     * @param content the content
226     * @return the send mail message
227     */
228    public SendMessage setContent(MimeMultipart content) {
229        this.content = content;
230        return this;
231    }
232
233    /**
234     * Adds the part to the content.
235     *
236     * @param part the part
237     * @return the send mail message
238     * @throws MessagingException the messaging exception
239     */
240    public SendMessage addContent(BodyPart part)
241            throws MessagingException {
242        if (content == null) {
243            content = new MimeMultipart();
244        }
245        content.addBodyPart(part);
246        return this;
247    }
248}