Sending mail functionality for Google App Engine


When we search for sending mail functionality then we get the solution using javax mail API.

Following is the code of using javax.mail API :


String emailId = <recipient-email-id>;
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
props.put("mail.debug", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");

Session s = Session.getInstance(props, null);
s.setDebug(true);

Message message = new MimeMessage(s);

InternetAddress from = new InternetAddress(<Sender mail-id>,<Sender name>);
InternetAddress to = new InternetAddress(emailId);

message.setFrom(from);
message.addRecipient(Message.RecipientType.TO, to);

message.setSubject(<subject>);
message.setContent(<content>, "text/plain");

val tr = s.getTransport("smtp");
tr.connect("smtp.gmail.com", <email-id>, <password>);
message.saveChanges();
tr.sendMessage(message, message.getAllRecipients());
tr.close();

If you are using free app on Google App Engine then above code will not work and will get following error:

com.google.apphosting.api.ApiProxy$FeatureNotEnabledException: The Socket API will be enabled for this application once billing has been enabled in the admin console.

In above code, we are trying to set smtp configuration. When we use smtp configuration then Google App Engine tries to use Socket API and these Socket APIs are available only with paid apps. If you will use above code with free app on Google App Engine then we will get above error.

So, to avoid this error, use following code :


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

String msgBody = "...";

Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(<sender-email>, "Sender name"));
msg.addRecipient(Message.RecipientType.TO,new InternetAddress(<recipient-email>, "recipient name"));
msg.setSubject("Your Example.com account has been activated");
msg.setText(msgBody);
Transport.send(msg);

There is one more possibility that you can get following error as well:

javax.mail.SendFailedException: Send failure (javax.mail.MessagingException: Illegal Arguments (java.lang.IllegalArgumentException: Unauthorized Sender: Unauthorized sender))

This error comes when sender email address is not added in permission list in your app on Google App Engine. So sender email must be added in permission list.

If you want to get more on this then see here

About Rishi Khandelwal

Sr. Software Engineer having more than 5 years industry experience. He has working experience in various technologies such as Scala, Java, Play, Akka, Lift Web, Spark, ElasticSearch, Backbone.js, html5, javascript, Less, Amazon EC2, WebRTC, SBT
This entry was posted in Scala. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s