Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / api / mail / MailService.java
bloba1ca673593843fde4d6a853270a5c0b381bd203d
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;
9 /**
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;
20 /**
21 * Attachments are an optional part of messages, but if present, all
22 * information about them must be provided.
24 * @param fileName The attachment must have a filename associated with it.
25 * The extension on that filename must be present and white-listed, or
26 * there will be a failure at send time.
28 * @param data An array with arbitrary byte content. The array must be
29 * be present, but may be of zero length.
31 * @throws IllegalArgumentException if either fileName or data are missing.
33 public Attachment (String fileName, byte[] data) {
34 if (data == null || fileName == null || fileName.length() == 0) {
35 throw new IllegalArgumentException("Attachment needs content and name");
37 this.fileName = fileName;
38 this.data = data;
41 /**
42 * Gets the file name of this attachment.
44 * @return The file name of this attachment.
46 public String getFileName() {
47 return fileName;
50 /**
51 * Gets the content of this attachment.
53 * @return The raw data of this attachment.
55 public byte[] getData() {
56 return data;
60 public class Header {
61 private final String name;
62 private final String value;
64 /**
65 * Headers are an optional part of messages, but if present, all
66 * information about them must be provided.
68 * @param name The name of the header. It must be present and white-listed.
70 * @param value The value of the header. It must be present, and it's
71 * content cannot be of zero length.
73 * @throws IllegalArgumentException if either name or data are missing.
75 public Header(String name, String value) {
76 if (name == null || value == null ||
77 name.length() == 0 || value.length() == 0) {
78 throw new IllegalArgumentException("Header needs name and value");
80 this.name = name;
81 this.value = value;
84 /**
85 * Gets the name of this header.
87 * @return The name of this header.
89 public String getName() {
90 return name;
93 /**
94 * Gets the value of this header.
96 * @return The value of this header.
98 public String getValue() {
99 return value;
104 * Messages are prepared by the caller, and then submitted to the Mail service
105 * for sending. Different fields are subject to different constraints, as
106 * enumerated in the {@code send} and {@code sendToAdmins} methods.
108 public class Message {
109 private String sender;
110 private String replyTo;
111 private Collection<String> to;
112 private Collection<String> cc;
113 private Collection<String> bcc;
114 private String subject;
115 private String textBody;
116 private String htmlBody;
117 private Collection<MailService.Attachment> attachments;
118 private Collection<MailService.Header> headers;
120 public Message() {}
123 * Convenience constructor for simple messages
124 * @param sender The sender's email address.
125 * @param to The recipient's email address or null for empty to address.
126 * @param subject The message subject.
127 * @param textBody The text body of the message.
129 public Message(String sender, String to, String subject, String textBody) {
130 this.sender = sender;
131 if (to == null) {
132 this.to = Arrays.asList();
133 } else {
134 this.to = Arrays.asList(to);
136 this.subject = subject;
137 this.textBody = textBody;
141 * Gets the sender of this message.
143 * @return The sender of this message.
145 public String getSender() {
146 return sender;
150 * {@code sender} must correspond to the valid email address of one of
151 * the admins for this application, or to the email address of the
152 * currently logged-in user. Sender is really the From: field of the email.
154 public void setSender(String sender) {
155 this.sender = sender;
159 * Gets the reply to field of this message.
161 * @return The reply to field of this message.
163 public String getReplyTo() {
164 return replyTo;
168 * {@code replyTo} may be {@code null}, or must be a valid email
169 * address otherwise.
171 public void setReplyTo(String replyTo) {
172 this.replyTo = replyTo;
176 * Gets the recipients in the 'to' field of this message.
178 * @return A collection containing the 'to' field recipients.
180 public Collection<String> getTo() {
181 return to;
185 * Sets the 'to' field of this message. Each string in the collection
186 * represents exactly one email address. Having null (or invalid addresses)
187 * will lead to eventual failure during the send process.
188 * @param to A collection containing the email addresses to set as the 'to'
189 * field.
191 public void setTo(Collection<String> to) {
192 this.to = to;
196 * Sets the 'to' field of this message. Each string represents exactly one
197 * email address. Having null (or invalid addresses) will lead to eventual
198 * failure during the send process.
200 * @param to The email addresses to set as the 'to' field.
202 public void setTo(String... to) {
203 this.to = Arrays.asList(to);
207 * Gets the recipients in the 'cc' field of this message.
209 * @return A collection containing the 'cc' field recipients.
211 public Collection<String> getCc() {
212 return cc;
216 * Sets the 'cc' field of this message. Each string in the collection
217 * represents exactly one email address. Having null (or invalid addresses)
218 * will lead cc eventual failure during the send process.
219 * @param cc A collection containing the email addresses cc set as the 'cc'
220 * field.
222 public void setCc(Collection<String> cc) {
223 this.cc = cc;
227 * Sets the 'cc' field of this message. Each string represents exactly one
228 * email address. Having null (or invalid addresses) will lead cc eventual
229 * failure during the send process.
231 * @param cc The email addresses cc set as the 'cc' field.
233 public void setCc(String... cc) {
234 this.cc = Arrays.asList(cc);
238 * Gets the recipients in the 'bcc' field of this message.
240 * @return A collection containing the 'bcc' field recipients.
242 public Collection<String> getBcc() {
243 return bcc;
247 * Sets the 'bcc' field of this message. Each string in the collection
248 * represents exactly one email address. Having null (or invalid addresses)
249 * will lead bcc eventual failure during the send process.
250 * @param bcc A collection containing the email addresses bcc set as the
251 * 'bcc' field.
253 public void setBcc(Collection<String> bcc) {
254 this.bcc = bcc;
258 * Sets the 'bcc' field of this message. Each string represents exactly one
259 * email address. Having null (or invalid addresses) will lead bcc eventual
260 * failure during the send process.
262 * @param bcc The email addresses bcc set as the 'bcc' field.
264 public void setBcc(String... bcc) {
265 this.bcc = Arrays.asList(bcc);
269 * Gets the subject of this message.
271 * @return The subject of this message.
273 public String getSubject() {
274 return subject;
278 * Sets the subject of this message. A null or empty subject will lead to
279 * eventual failure during the send process.
281 * @param subject A string containing the new subject of this message.
283 public void setSubject(String subject) {
284 this.subject = subject;
288 * Gets the text body of this message.
290 * @return The text body.
292 public String getTextBody() {
293 return textBody;
297 * Sets the text body of this message. At least one of {@code textBody} and
298 * {@code htmlBody} must not be {@code null}.
299 * @param textBody A string containing the new text body of this message.
301 public void setTextBody(String textBody) {
302 this.textBody = textBody;
306 * Gets the html body of this message.
308 * @return The html body.
310 public String getHtmlBody() {
311 return htmlBody;
315 * Sets the html body of this message. At least one of {@code textBody} and
316 * {@code htmlBody} must not be {@code null}.
317 * @param htmlBody A string containing the new text body of this message.
319 public void setHtmlBody(String htmlBody) {
320 this.htmlBody = htmlBody;
324 * Gets the attachments of this message.
326 * @return A collection containing the attachments of this message.
328 public Collection<MailService.Attachment> getAttachments() {
329 return attachments;
333 * Sets the attachments of this message. {@code attachments} may be
334 * {@code null}, otherwise each attachment must have a corresponding file
335 * name with one of the white-listed extensions.
336 * @param attachments A collection of attachments.
338 public void setAttachments(Collection<MailService.Attachment> attachments) {
339 this.attachments = attachments;
343 * Sets the attachments of this message. {@code attachments} may be
344 * {@code null}, otherwise each attachment must have a corresponding file
345 * name with one of the white-listed extensions.
346 * @param attachments Attachments to attach to this message.
348 public void setAttachments(MailService.Attachment... attachments) {
349 this.attachments = Arrays.asList(attachments);
353 * Gets the headers of this message.
355 * @return A collection containing the headers of this message.
357 public Collection<MailService.Header> getHeaders() {
358 return headers;
362 * Sets the headers of this message. {@code headers} may be {@code null},
363 * otherwise each header name must be one of the white-listed names.
364 * @param headers A collection of headers.
366 public void setHeaders(Collection<MailService.Header> headers) {
367 this.headers = headers;
371 * Sets the headers of this message. {@code headers} may be {@code null},
372 * otherwise each header name must be one of the white-listed names.
373 * @param headers A collection of headers.
375 public void setHeaders(MailService.Header... headers) {
376 this.headers = Arrays.asList(headers);
381 * Sends a mail that has been prepared in a MailService.Message.
382 * <p>
383 * The message will be delivered asynchronously, and delivery problems
384 * will result in a bounce to the specified sender.
385 * <p>
386 * {@coder Sender} and at least one of the collections for {@code to, cc, bcc}
387 * must not be {@code null}.
389 * @param message The message to be sent.
390 * @throws IllegalArgumentException when incorrect arguments are passed.
391 * @throws IOException on internal delivery errors.
393 public void send(MailService.Message message) throws IOException;
396 * Send an email alert to all admins of an application.
397 * <p>
398 * The message will be delivered asynchronously, and delivery problems
399 * will result in a bounce to the admins.
400 * <p>
401 * The content of the {@code to, cc, bcc} fields should be {@code
402 * null}.
404 * @param message The message to be sent.
405 * @throws IllegalArgumentException when incorrect arguments are passed.
406 * @throws IOException on internal delivery errors.
408 public void sendToAdmins(MailService.Message message) throws IOException;