Adsense

Tuesday, 27 May 2014

Google App Engine - Implement email notification Using JavaMail

 


I was trying to send an email alert using JavaMail for Google App Engine. Here is the sample working code for that.


 
Properties props = new Properties();
javax.mail.Session session1 = javax.mail.Session.getDefaultInstance(props, null);

String msgBody = "This is a test mail";

try {
    System.out.println("Email notification is sending");
    Message msg = new MimeMessage(session1);
    msg.setFrom(new InternetAddress("myGmailId1@gmail.com", "Example.com Admin"));
    msg.addRecipient(Message.RecipientType.TO,
            new InternetAddress("myGmailId2@gmail.com", "Mr. User"));
    msg.setSubject("Your Example.com account has been activated");
    msg.setText(msgBody);
    Transport.send(msg);
    System.out.println("Email notification has been sent");
} catch (AddressException e) {
    // ...
} catch (MessagingException e) {
    // ...
}
 
 
Ensure the following points as well: 

1.The sender e-mail address should either be registered as the administrator of your 
  app, or be the e-mail address of the current user that's logged in to the 
  app. 
2.For security purposes, the sender address of a message must be the
  email address of an administrator for the application or any valid
  email receiving address for the app (see Receiving Mail). The sender
  can also be the Google Account email address of the current user who
  is signed in, if the user's account is a Gmail account or is on a
  domain managed by Google Apps.

No comments:

Post a Comment