简体   繁体   中英

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?

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("admin@example.com", "Example.com Admin"));
            msg.addRecipient(Message.RecipientType.TO,
                             new InternetAddress("user@example.com", "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 xxx@APP-ID.appspotmail.com).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM