1 // Copyright 2008 Google Inc. All rights reserved.
3 package com
.google
.appengine
.api
.mail
;
5 import com
.google
.appengine
.api
.mail
.MailServicePb
.MailAttachment
;
6 import com
.google
.appengine
.api
.mail
.MailServicePb
.MailHeader
;
7 import com
.google
.appengine
.api
.mail
.MailServicePb
.MailMessage
;
8 import com
.google
.appengine
.api
.mail
.MailServicePb
.MailServiceError
.ErrorCode
;
9 import com
.google
.apphosting
.api
.ApiProxy
;
11 import java
.io
.IOException
;
14 * This class implements raw access to the mail service.
15 * Applications that don't want to make use of Sun's JavaMail
16 * can use it directly -- but they will forego the typing and
17 * convenience methods that JavaMail provides.
20 class MailServiceImpl
implements MailService
{
21 static final String PACKAGE
= "mail";
25 public void sendToAdmins(Message message
)
26 throws IllegalArgumentException
, IOException
{
27 doSend(message
, true);
32 public void send(Message message
)
33 throws IllegalArgumentException
, IOException
{
34 doSend(message
, false);
38 * Does the actual sending of the message.
39 * @param message The message to be sent.
40 * @param toAdmin Whether the message is to be sent to the admins.
42 private void doSend(Message message
, boolean toAdmin
)
43 throws IllegalArgumentException
, IOException
{
45 MailMessage msgProto
= new MailMessage();
46 if (message
.getSender() != null) {
47 msgProto
.setSender(message
.getSender());
49 if (message
.getTo() != null) {
50 for (String to
: message
.getTo()) {
54 if (message
.getCc() != null) {
55 for (String cc
: message
.getCc()) {
59 if (message
.getBcc() != null) {
60 for (String bcc
: message
.getBcc()) {
64 if (message
.getReplyTo() != null) {
65 msgProto
.setReplyTo(message
.getReplyTo());
67 if (message
.getSubject() != null) {
68 msgProto
.setSubject(message
.getSubject());
70 if (message
.getTextBody() != null) {
71 msgProto
.setTextBody(message
.getTextBody());
73 if (message
.getHtmlBody() != null) {
74 msgProto
.setHtmlBody(message
.getHtmlBody());
76 if (message
.getAttachments() != null) {
77 for (Attachment attach
: message
.getAttachments()) {
78 MailAttachment attachProto
= new MailAttachment();
79 attachProto
.setFileName(attach
.getFileName());
80 attachProto
.setDataAsBytes(attach
.getData());
81 msgProto
.addAttachment(attachProto
);
84 if (message
.getHeaders() != null) {
85 for (Header header
: message
.getHeaders()) {
86 MailHeader headerProto
= new MailHeader();
87 headerProto
.setName(header
.getName());
88 headerProto
.setValue(header
.getValue());
89 msgProto
.addHeader(headerProto
);
93 byte[] msgBytes
= msgProto
.toByteArray();
96 ApiProxy
.makeSyncCall(PACKAGE
, "SendToAdmins", msgBytes
);
98 ApiProxy
.makeSyncCall(PACKAGE
, "Send", msgBytes
);
100 } catch (ApiProxy
.ApplicationException ex
) {
101 switch (ErrorCode
.valueOf(ex
.getApplicationError())) {
103 throw new IllegalArgumentException("Bad Request: " +
104 ex
.getErrorDetail());
105 case UNAUTHORIZED_SENDER
:
106 throw new IllegalArgumentException("Unauthorized Sender: " +
107 ex
.getErrorDetail());
108 case INVALID_ATTACHMENT_TYPE
:
109 throw new IllegalArgumentException("Invalid Attachment Type: " +
110 ex
.getErrorDetail());
111 case INVALID_HEADER_NAME
:
112 throw new IllegalArgumentException("Invalid Header Name: " +
113 ex
.getErrorDetail());
116 throw new IOException(ex
.getErrorDetail());