1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.tradefed.util;
18 
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.Collection;
23 
24 /**
25  * Interface for sending email.
26  */
27 public interface IEmail {
28 
29     /**
30      * A method to send a {@link Message}.  Verifies that the to, subject, and body fields of the
31      * {@link Message} are not null, but does no verification beyond the null checks.
32      *
33      * Note that any SMTP-level errors are undetectable at this stage.  Because of the asynchronous
34      * nature of email, they will generally be reported to the envelope-sender of the message.  In
35      * that case, the envelope-sender will typically receive an email from MAILER-DAEMON with the
36      * details of the error.
37      *
38      * @param msg The {@link IEmail.Message} to try to send
39      * @throws IllegalArgumentException if any of the to, subject, or body fields of {@code msg} is
40      *         null
41      * @throws IOException if sending email failed in a synchronously-detectable way
42      */
send(Message msg)43     public void send(Message msg) throws IllegalArgumentException, IOException;
44 
45     /**
46      * Container for email message data.
47      */
48     public static class Message {
49         static final String PLAIN = "text/plain";
50         static final String HTML = "text/html";
51 
52         private Collection<String> mToAddrs = null;
53         private Collection<String> mCcAddrs = null;
54         private Collection<String> mBccAddrs = null;
55         private String mSubject = null;
56         private String mBody = null;
57         private String mSender = null;
58         private String mContentType = PLAIN;
59 
Message()60         public Message() {}
61 
62         /**
63          * Convenience constructor: create a simple message
64          *
65          * @param to Single destination address
66          * @param subject Subject
67          * @param body Message body
68          */
Message(String to, String subject, String body)69         public Message(String to, String subject, String body) {
70             addTo(to);
71             setSubject(subject);
72             setBody(body);
73         }
74 
addTo(String address)75         public void addTo(String address) {
76             if (mToAddrs == null) {
77                 mToAddrs = new ArrayList<String>();
78             }
79             mToAddrs.add(address);
80         }
addCc(String address)81         public void addCc(String address) {
82             if (mCcAddrs == null) {
83                 mCcAddrs = new ArrayList<String>();
84             }
85             mCcAddrs.add(address);
86         }
addBcc(String address)87         public void addBcc(String address) {
88             if (mBccAddrs == null) {
89                 mBccAddrs = new ArrayList<String>();
90             }
91             mBccAddrs.add(address);
92         }
setSubject(String subject)93         public void setSubject(String subject) {
94             mSubject = subject;
95         }
96         /**
97          * Set the recipients. All previously added recipients will be replaced.
98          * {@link #addTo(String)} to append to the recipients list.
99          *
100          * @param recipients an array of recipient email addresses
101          */
setTos(String[] recipients)102         public void setTos(String[] recipients){
103             mToAddrs = Arrays.asList(recipients);
104         }
setBody(String body)105         public void setBody(String body) {
106             mBody = body;
107         }
setSender(String sender)108         public void setSender(String sender) {
109             mSender = sender;
110         }
setContentType(String contentType)111         public void setContentType(String contentType) {
112             if (contentType == null) throw new NullPointerException();
113             mContentType = contentType;
114         }
115 
setHtml(boolean html)116         public void setHtml(boolean html) {
117             if (html) {
118                 setContentType(HTML);
119             } else {
120                 setContentType(PLAIN);
121             }
122         }
123 
getTo()124         public Collection<String> getTo() {
125             return mToAddrs;
126         }
getCc()127         public Collection<String> getCc() {
128             return mCcAddrs;
129         }
getBcc()130         public Collection<String> getBcc() {
131             return mBccAddrs;
132         }
getSubject()133         public String getSubject() {
134             return mSubject;
135         }
getBody()136         public String getBody() {
137             return mBody;
138         }
139 
getSender()140         public String getSender() {
141             return mSender;
142         }
143 
getContentType()144         public String getContentType() {
145             return mContentType;
146         }
147 
isHtml()148         public boolean isHtml() {
149             return HTML.equals(mContentType);
150         }
151     }
152 }
153