1 // Copyright 2008 Google Inc. All rights reserved.
3 package com
.google
.appengine
.api
.mail
;
5 import java
.io
.IOException
;
6 import java
.util
.Arrays
;
7 import java
.util
.Collection
;
10 * The {@code MailService} provides a way for user code to send emails
11 * to arbitrary destinations.
14 public interface MailService
{
16 public class Attachment
{
17 private final String fileName
;
18 private final byte[] data
;
19 private final String contentID
;
22 * Attachments are an optional part of messages, but if present, all
23 * information about them must be provided.
25 * @param fileName The attachment must have a filename associated with it.
26 * The extension on that filename must be present and white-listed, or
27 * there will be a failure at send time.
29 * @param data An array with arbitrary byte content. The array must be
30 * be present, but may be of zero length.
32 * @throws IllegalArgumentException if either fileName or data are missing.
34 public Attachment (String fileName
, byte[] data
) {
35 this(fileName
, data
, null);
39 * Attachments are an optional part of messages, but if present, all
40 * information about them must be provided.
42 * @param fileName The attachment must have a filename associated with it.
43 * The extension on that filename must be present and white-listed, or
44 * there will be a failure at send time.
46 * @param data An array with arbitrary byte content. The array must be
47 * be present, but may be of zero length.
49 * @param contentID The attachment's content ID. May be null.
51 * @throws IllegalArgumentException if either fileName or data are missing.
53 public Attachment(String fileName
, byte[] data
, String contentID
) {
54 if (data
== null || fileName
== null || fileName
.length() == 0) {
55 throw new IllegalArgumentException("Attachment needs content and name");
57 this.fileName
= fileName
;
59 this.contentID
= contentID
;
63 * Gets the file name of this attachment.
65 * @return The file name of this attachment.
67 public String
getFileName() {
72 * Gets the content of this attachment.
74 * @return The raw data of this attachment.
76 public byte[] getData() {
80 public String
getContentID() {
86 private final String name
;
87 private final String value
;
90 * Headers are an optional part of messages, but if present, all
91 * information about them must be provided.
93 * @param name The name of the header. It must be present and white-listed.
95 * @param value The value of the header. It must be present, and it's
96 * content cannot be of zero length.
98 * @throws IllegalArgumentException if either name or data are missing.
100 public Header(String name
, String value
) {
101 if (name
== null || value
== null ||
102 name
.length() == 0 || value
.length() == 0) {
103 throw new IllegalArgumentException("Header needs name and value");
110 * Gets the name of this header.
112 * @return The name of this header.
114 public String
getName() {
119 * Gets the value of this header.
121 * @return The value of this header.
123 public String
getValue() {
129 * Messages are prepared by the caller, and then submitted to the Mail service
130 * for sending. Different fields are subject to different constraints, as
131 * enumerated in the {@code send} and {@code sendToAdmins} methods.
133 public class Message
{
134 private String sender
;
135 private String replyTo
;
136 private Collection
<String
> to
;
137 private Collection
<String
> cc
;
138 private Collection
<String
> bcc
;
139 private String subject
;
140 private String textBody
;
141 private String htmlBody
;
142 private Collection
<MailService
.Attachment
> attachments
;
143 private Collection
<MailService
.Header
> headers
;
148 * Convenience constructor for simple messages
149 * @param sender The sender's email address.
150 * @param to The recipient's email address or null for empty to address.
151 * @param subject The message subject.
152 * @param textBody The text body of the message.
154 public Message(String sender
, String to
, String subject
, String textBody
) {
155 this.sender
= sender
;
157 this.to
= Arrays
.asList();
159 this.to
= Arrays
.asList(to
);
161 this.subject
= subject
;
162 this.textBody
= textBody
;
166 * Gets the sender of this message.
168 * @return The sender of this message.
170 public String
getSender() {
175 * {@code sender} must correspond to the valid email address of one of
176 * the admins for this application, or to the email address of the
177 * currently logged-in user. Sender is really the From: field of the email.
179 public void setSender(String sender
) {
180 this.sender
= sender
;
184 * Gets the reply to field of this message.
186 * @return The reply to field of this message.
188 public String
getReplyTo() {
193 * {@code replyTo} may be {@code null}, or must be a valid email
196 public void setReplyTo(String replyTo
) {
197 this.replyTo
= replyTo
;
201 * Gets the recipients in the 'to' field of this message.
203 * @return A collection containing the 'to' field recipients.
205 public Collection
<String
> getTo() {
210 * Sets the 'to' field of this message. Each string in the collection
211 * represents exactly one email address. Having null (or invalid addresses)
212 * will lead to eventual failure during the send process.
213 * @param to A collection containing the email addresses to set as the 'to'
216 public void setTo(Collection
<String
> to
) {
221 * Sets the 'to' field of this message. Each string represents exactly one
222 * email address. Having null (or invalid addresses) will lead to eventual
223 * failure during the send process.
225 * @param to The email addresses to set as the 'to' field.
227 public void setTo(String
... to
) {
228 this.to
= Arrays
.asList(to
);
232 * Gets the recipients in the 'cc' field of this message.
234 * @return A collection containing the 'cc' field recipients.
236 public Collection
<String
> getCc() {
241 * Sets the 'cc' field of this message. Each string in the collection
242 * represents exactly one email address. Having null (or invalid addresses)
243 * will lead cc eventual failure during the send process.
244 * @param cc A collection containing the email addresses cc set as the 'cc'
247 public void setCc(Collection
<String
> cc
) {
252 * Sets the 'cc' field of this message. Each string represents exactly one
253 * email address. Having null (or invalid addresses) will lead cc eventual
254 * failure during the send process.
256 * @param cc The email addresses cc set as the 'cc' field.
258 public void setCc(String
... cc
) {
259 this.cc
= Arrays
.asList(cc
);
263 * Gets the recipients in the 'bcc' field of this message.
265 * @return A collection containing the 'bcc' field recipients.
267 public Collection
<String
> getBcc() {
272 * Sets the 'bcc' field of this message. Each string in the collection
273 * represents exactly one email address. Having null (or invalid addresses)
274 * will lead bcc eventual failure during the send process.
275 * @param bcc A collection containing the email addresses bcc set as the
278 public void setBcc(Collection
<String
> bcc
) {
283 * Sets the 'bcc' field of this message. Each string represents exactly one
284 * email address. Having null (or invalid addresses) will lead bcc eventual
285 * failure during the send process.
287 * @param bcc The email addresses bcc set as the 'bcc' field.
289 public void setBcc(String
... bcc
) {
290 this.bcc
= Arrays
.asList(bcc
);
294 * Gets the subject of this message.
296 * @return The subject of this message.
298 public String
getSubject() {
303 * Sets the subject of this message. A null or empty subject will lead to
304 * eventual failure during the send process.
306 * @param subject A string containing the new subject of this message.
308 public void setSubject(String subject
) {
309 this.subject
= subject
;
313 * Gets the text body of this message.
315 * @return The text body.
317 public String
getTextBody() {
322 * Sets the text body of this message. At least one of {@code textBody} and
323 * {@code htmlBody} must not be {@code null}.
324 * @param textBody A string containing the new text body of this message.
326 public void setTextBody(String textBody
) {
327 this.textBody
= textBody
;
331 * Gets the html body of this message.
333 * @return The html body.
335 public String
getHtmlBody() {
340 * Sets the html body of this message. At least one of {@code textBody} and
341 * {@code htmlBody} must not be {@code null}.
342 * @param htmlBody A string containing the new text body of this message.
344 public void setHtmlBody(String htmlBody
) {
345 this.htmlBody
= htmlBody
;
349 * Gets the attachments of this message.
351 * @return A collection containing the attachments of this message.
353 public Collection
<MailService
.Attachment
> getAttachments() {
358 * Sets the attachments of this message. {@code attachments} may be
359 * {@code null}, otherwise each attachment must have a corresponding file
360 * name with one of the white-listed extensions.
361 * @param attachments A collection of attachments.
363 public void setAttachments(Collection
<MailService
.Attachment
> attachments
) {
364 this.attachments
= attachments
;
368 * Sets the attachments of this message. {@code attachments} may be
369 * {@code null}, otherwise each attachment must have a corresponding file
370 * name with one of the white-listed extensions.
371 * @param attachments Attachments to attach to this message.
373 public void setAttachments(MailService
.Attachment
... attachments
) {
374 this.attachments
= Arrays
.asList(attachments
);
378 * Gets the headers of this message.
380 * @return A collection containing the headers of this message.
382 public Collection
<MailService
.Header
> getHeaders() {
387 * Sets the headers of this message. {@code headers} may be {@code null},
388 * otherwise each header name must be one of the white-listed names.
389 * @param headers A collection of headers.
391 public void setHeaders(Collection
<MailService
.Header
> headers
) {
392 this.headers
= headers
;
396 * Sets the headers of this message. {@code headers} may be {@code null},
397 * otherwise each header name must be one of the white-listed names.
398 * @param headers A collection of headers.
400 public void setHeaders(MailService
.Header
... headers
) {
401 this.headers
= Arrays
.asList(headers
);
406 * Sends a mail that has been prepared in a MailService.Message.
408 * The message will be delivered asynchronously, and delivery problems
409 * will result in a bounce to the specified sender.
411 * {@code Sender} and at least one of the collections for {@code to, cc, bcc}
412 * must not be {@code null}.
414 * @param message The message to be sent.
415 * @throws IllegalArgumentException when incorrect arguments are passed.
416 * @throws IOException on internal delivery errors.
418 public void send(MailService
.Message message
) throws IOException
;
421 * Send an email alert to all admins of an application.
423 * The message will be delivered asynchronously, and delivery problems
424 * will result in a bounce to the admins.
426 * The content of the {@code to, cc, bcc} fields should be {@code
429 * @param message The message to be sent.
430 * @throws IllegalArgumentException when incorrect arguments are passed.
431 * @throws IOException on internal delivery errors.
433 public void sendToAdmins(MailService
.Message message
) throws IOException
;