Inquisitor
Inquisitor

Reputation: 3

Send Email alerts from Google App Engine

I am planning to use Google App Engine to deploy a web application. The application sends alerts by email to users if some other users does some activity on the user's page. Is there any way I can send notifications to the user by an email in this case?

Upvotes: 0

Views: 213

Answers (1)

Lipis
Lipis

Reputation: 21835

Yes you can use JavaMail to Send Mail. Here is an example taken from the docs:

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

// ...
        Properties props = new Properties();
        Session session = Session.getDefaultInstance(props, null);

        String msgBody = "...";

        try {
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress("[email protected]", "Example.com Admin"));
            msg.addRecipient(Message.RecipientType.TO,
                             new InternetAddress("[email protected]", "Mr. User"));
            msg.setSubject("Your Example.com account has been activated");
            msg.setText(msgBody);
            Transport.send(msg);

        } catch (AddressException e) {
            // ...
        } catch (MessagingException e) {
            // ...
        }

It is also important that the sender address must be one of the following types:

  • The address of a registered administrator for the application
  • The address of the user for the current request signed in with a Google Account. You can determine the current user's email address with the Users API. The user's account must be a - Gmail account, or be on a domain managed by Google Apps.
  • Any valid email receiving address for the app (such as [email protected]).

Upvotes: 1

Related Questions