Java 从谷歌应用程序引擎发送电子邮件警报
我计划使用Google App Engine部署一个web应用程序。如果其他用户在用户页面上执行某些活动,应用程序将通过电子邮件向用户发送警报。在这种情况下,我是否可以通过电子邮件向用户发送通知?是的,您可以使用。以下是一个取自文档的示例:Java 从谷歌应用程序引擎发送电子邮件警报,java,google-app-engine,web,notifications,Java,Google App Engine,Web,Notifications,我计划使用Google App Engine部署一个web应用程序。如果其他用户在用户页面上执行某些活动,应用程序将通过电子邮件向用户发送警报。在这种情况下,我是否可以通过电子邮件向用户发送通知?是的,您可以使用。以下是一个取自文档的示例: import java.util.Properties; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; import
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) {
// ...
}
发件人地址必须是以下类型之一,这一点也很重要:
- 应用程序的注册管理员的地址
- 使用Google帐户登录的当前请求的用户地址。您可以使用Users API确定当前用户的电子邮件地址。用户的帐户必须是-Gmail帐户,或者位于谷歌应用程序管理的域上
- 应用程序的任何有效电子邮件接收地址(例如xxx@APP-ID.appspotmail.com)