Package com.google.appengine.api.mail

Examples of com.google.appengine.api.mail.MailService


  /** {@inheritDoc} */
  @Override
  public void sendMessage(Message message, Address[] addresses)
      throws MessagingException {
    MailService service = MailServiceFactory.getMailService();
    MailService.Message msg = new MailService.Message();

    String sender = null;
    if (message instanceof MimeMessage) {
      Address senderAddr = ((MimeMessage) message).getSender();
      if (senderAddr != null) {
        sender = senderAddr.toString();
      }
    }
    if (sender == null && message.getFrom() != null
        && message.getFrom().length > 0) {
      sender = message.getFrom()[0].toString();
    }
    msg.setSender(sender);

    try {
      msg.setReplyTo(Joiner.on(", ").useForNull("null").join(message.getReplyTo()));
    } catch (NullPointerException e) {
    }

    boolean toAdmins = false;
    Address[] allRecipients = message.getAllRecipients();
    if (allRecipients != null) {
      for (Address addr : allRecipients) {
        if (ADMINS_ADDRESS.equals(addr.toString())) {
          toAdmins = true;
        }
      }
    }

    if (!toAdmins) {
      Set<String> allAddresses = new HashSet<String>();
      for (Address addr : addresses) {
        allAddresses.add(addr.toString());
      }
      msg.setTo(convertAddressFields(message.getRecipients(RecipientType.TO), allAddresses));
      msg.setCc(convertAddressFields(message.getRecipients(RecipientType.CC), allAddresses));
      msg.setBcc(convertAddressFields(message.getRecipients(RecipientType.BCC), allAddresses));
    }

    msg.setSubject(message.getSubject());

    Object textObject = null;
    Object htmlObject = null;
    String textType = null;
    String htmlType = null;
    Multipart otherMessageParts = null;

    List<MailService.Header> headers = new ArrayList<MailService.Header>();
    Enumeration originalHeaders = message.getMatchingHeaders(HEADERS_WHITELIST);
    while (originalHeaders.hasMoreElements()) {
      Header header = (Header) originalHeaders.nextElement();
      headers.add(new MailService.Header(header.getName(), header.getValue()));
    }
    msg.setHeaders(headers);

    if (message.getContentType() == null) {
      try {
        textObject = message.getContent();
        textType = message.getContentType();
      } catch (IOException e) {
        throw new MessagingException("Getting typeless content failed", e);
      }
    } else if (message.isMimeType("text/html")) {
      try {
        htmlObject = message.getContent();
        htmlType = message.getContentType();
      } catch (IOException e) {
        throw new MessagingException("Getting html content failed", e);
      }
    } else if (message.isMimeType("text/*")) {
      try {
        textObject = message.getContent();
        textType = message.getContentType();
      } catch (IOException e) {
        throw new MessagingException("Getting text/* content failed", e);
      }
    } else if (message.isMimeType("multipart/*")) {
      Multipart mp;
      try {
        mp = (Multipart) message.getContent();
        for (int i = 0; i < mp.getCount(); i++) {
          BodyPart bp = mp.getBodyPart(i);
          if (bp.isMimeType("text/plain") && textObject == null) {
            textObject = bp.getContent();
            textType = bp.getContentType();
          } else if (bp.isMimeType("text/html") && htmlObject == null) {
            htmlObject = bp.getContent();
            htmlType = bp.getContentType();
          } else {
            if (otherMessageParts == null) {
              String type = mp.getContentType();
              assert (type.startsWith("multipart/"));
              otherMessageParts = new MimeMultipart(
                  type.substring("multipart/".length()));
            }
            otherMessageParts.addBodyPart(bp);
          }
        }
      } catch (IOException e) {
        throw new MessagingException("Getting multipart content failed", e);
      }
    }

    if (textObject != null) {
      if (textObject instanceof String) {
        msg.setTextBody((String) textObject);
      } else if (textObject instanceof InputStream) {
        try {
          msg.setTextBody(inputStreamToString((InputStream) textObject, textType));
        } catch (IOException e) {
          throw new MessagingException("Stringifying text body failed", e);
        }
      } else {
        throw new MessagingException("Converting text body failed");
      }
    }

    if (htmlObject != null) {
      if (htmlObject instanceof String) {

        msg.setHtmlBody((String) htmlObject);
      } else if (htmlObject instanceof InputStream) {
        try {
          msg.setHtmlBody(inputStreamToString((InputStream) htmlObject, htmlType));
        } catch (IOException e) {
          throw new MessagingException("Stringifying html body failed", e);
        }
      } else {
        throw new MessagingException("Converting html body failed");
      }
    }

    if (otherMessageParts != null) {
      ArrayList<MailService.Attachment> attachments =
          new ArrayList<MailService.Attachment>(otherMessageParts.getCount());
      for (int i = 0; i < otherMessageParts.getCount(); i++) {
        BodyPart bp = otherMessageParts.getBodyPart(i);
        String name = bp.getFileName();
        byte[] data;
        try {
          Object o = bp.getContent();
          if (o instanceof InputStream) {
            data = inputStreamToBytes((InputStream) o);
          } else if (o instanceof String) {
              data = ((String) o).getBytes();
          } else {
            throw new MessagingException("Converting attachment data failed");
          }
        } catch (IOException e) {
          throw new MessagingException("Extracting attachment data failed", e);
        }
        MailService.Attachment attachment =
            new MailService.Attachment(name, data);
        attachments.add(attachment);
      }
      msg.setAttachments(attachments);
    }

    try {
      if (toAdmins) {
        service.sendToAdmins(msg);
      } else {
        service.send(msg);
      }
    } catch (IOException e) {
      notifyTransportListeners(
          TransportEvent.MESSAGE_NOT_DELIVERED, new Address[0], addresses,
          new Address[0], message);
View Full Code Here


      ApiProxy.getCurrentEnvironment().getAttributes().get("com.google.appengine.runtime.default_version_hostname"),
      URLEncoder.encode(confirmationCode, "UTF-8"));
    String body = String.format("You've been invited to administer an instance of " +
            "YouTube Direct. To accept this invitation, please visit %s", url);
   
    MailService mailService = MailServiceFactory.getMailService();
    Message message = new Message();

    message.setSubject("Invitation to Administer YouTube Direct");
    message.setSender(fromAddress);
    message.setTo(toAddress);
    message.setTextBody(body);
    mailService.send(message);
  }
View Full Code Here

  public void sendPhotoEntryToAdmins(PhotoEntry photoEntry) {
    log.info(
        String.format("Sending photo from PhotoEntry id '%s' to admins...", photoEntry.getId()));
    AdminConfig adminConfig = adminConfigDao.getAdminConfig();

    MailService mailService = MailServiceFactory.getMailService();
    Message message = new Message();

    try {
      String fromAddress = adminConfig.getFromAddress();
      if (util.isNullOrEmpty(fromAddress)) {
        throw new IllegalArgumentException("No from address found in configuration.");
      }

      message.setSubject("Unable to submit photo to Picasa");
      message.setSender(fromAddress);
      message.setTextBody("YouTube Direct was unable to upload a photo submission to Picasa.\n\n"
          + "There might be a service issue, or your Picasa configuration might be incorrect.\n\n"
          + "The photo in question is attached.");

      BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
      byte[] photoBytes = blobstoreService.fetchData(
          photoEntry.getBlobKey(), 0, photoEntry.getOriginalFileSize() - 1);

      MailService.Attachment photoAttachment =
          new MailService.Attachment(photoEntry.getOriginalFileName(), photoBytes);
      message.setAttachments(photoAttachment);

      mailService.sendToAdmins(message);
      log.info("Email sent to admins.");
    } catch (IOException e) {
      log.log(Level.WARNING, "", e);
    } catch (IllegalArgumentException e) {
      log.log(Level.WARNING, "", e);
View Full Code Here

        throw new IllegalArgumentException(
            "No notification email addresses found in configuration.");
      }
      String[] addresses = addressCommaSeparated.split("\\s*,\\s*");

      MailService mailService = MailServiceFactory.getMailService();
      Message message = new Message();

      // Default to the first (or only) address in the recipient list to use
      // as From: header.
      message.setSender(addresses[0]);
      message.setTo(addresses);
      message.setSubject(subject);
      message.setTextBody(body);

      mailService.send(message);
    } catch (IOException e) {
      log.log(Level.WARNING, "", e);
    } catch (IllegalArgumentException e) {
      log.info(e.getMessage());
    }
View Full Code Here

  private void sendUserModerationEmail(String toAddress, String subject, String body) {
    try {
      AdminConfig adminConfig = adminConfigDao.getAdminConfig();

      MailService mailService = MailServiceFactory.getMailService();
      Message message = new Message();

      String fromAddress = adminConfig.getFromAddress();
      if (util.isNullOrEmpty(fromAddress)) {
        throw new IllegalArgumentException("No from address found in configuration.");
      }

      message.setSender(fromAddress);
      message.setTo(toAddress);
      message.setSubject(subject);
      message.setTextBody(body);

      mailService.send(message);
    } catch (IOException e) {
      log.log(Level.WARNING, "", e);
    } catch (IllegalArgumentException e) {
      log.log(Level.WARNING, "", e);
    }
View Full Code Here

    @Override
    @SuppressWarnings("unchecked")
    protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
        OutboundBinding binding = resolveAndRemoveReferenceParameter(
                parameters, "outboundBindingRef", OutboundBinding.class, new GMailBinding());
        MailService service = resolveAndRemoveReferenceParameter(
                parameters, "mailServiceRef", MailService.class, MailServiceFactory.getMailService());
        GMailEndpoint endpoint = new GMailEndpoint(uri, remaining);
        endpoint.setOutboundBinding(binding);
        endpoint.setMailService(service);
        return endpoint;
View Full Code Here

  private static final Logger logger = Logger.getLogger(GbMailService.class.getName());

  public static void sendMail(Email email, long controlId, String message) {

    MailService mailService = MailServiceFactory.getMailService();
    Message message1 = new Message();
    message1.setTo(email.getEmail());
    message1.setSubject("The task no [" + controlId + "]" + message);
    message1.setSender("gobo-tools@"
      + ApiProxy.getCurrentEnvironment().getAppId()
      + ".appspotmail.com");
    message1.setTextBody("The task no [" + controlId + "]" + message);
    try {
      mailService.send(message1);
    } catch (IOException e) {
      logger.warning(e.getMessage());
    }
  }
View Full Code Here

        if (request.getServerPort() != 80) {
            b.append(":").append(request.getServerPort());
        }
        b.append("/minutes?download=").append(blobKey.getKeyString());
        message.setTextBody(b.toString());
        MailService mailService = MailServiceFactory.getMailService();
        mailService.send(message);
        response.setStatus(HttpServletResponse.SC_NO_CONTENT);
        return null;
    }
View Full Code Here

        if (request.getServerPort() != 80) {
            b.append(":").append(request.getServerPort());
        }
        b.append("/minutes.html?minutes=").append(keyString);
        message.setTextBody(b.toString());
        MailService mailService = MailServiceFactory.getMailService();
        mailService.sendToAdmins(message);
    }
View Full Code Here

TOP

Related Classes of com.google.appengine.api.mail.MailService

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.