

Sending mail functionality for Google App Engine
source link: https://blog.knoldus.com/sending-mail-functionality-for-google-app-engine/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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 :
xxxxxxxxxx
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(,);
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:
xxxxxxxxxx
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 :
xxxxxxxxxx
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:
xxxxxxxxxx
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
Written by Rishi Khandelwal
Rishi is a Lead Consultant, with experience of more than 7 years. Rishi is product focused developer who loves developing both front-end user interfaces and scalable back-end infrastructure. He is a good team player, quick learner and a humble person. He has good time management skills, aimed to give best results and fully dedicated towards his work & responsibilities. He is able to work as individual and as well as in team. He loves to share his knowledge, therefore he often writes technical blogs.
Post navigation
Recommend
-
13
Sending mail without having to see your inbox For the longest time, the way I handled e-mail was with the "mail" program which came with the system. It was simple, fast (it never did "hourglass" or "beachball" garbage on me), and...
-
10
Adding an Email sending functionality in Play using Scala Reading Time: < 1 minuteTo provide an Email Sending functionality in your Play App that is being built with Play 2.2.1, follow these steps ( this post...
-
6
7pace’s Desktop App for Windows & Mac Boasts New Features & Functionality We post great content. Get it in your inbox. By submitting this form I...
-
11
A Non-blocking "Email sending" functionality in Scala Reading Time: < 1 minuteIn our last blog “Adding a...
-
10
Integrating sending mail functionality with LogBack in Scala Reading Time: < 1 minuteFew days ago, I had the requirement that whenever there will be an exception in the application, we should get an email for...
-
7
Add undo & redo functionality in your APPhalo! Opps I spelled it wrong. Let me Ctrl+z this. Ohh wait your app doesn't support undo and redo, what a shame :( Okay in this article lets fix that. But instead of working with a big and c...
-
11
Failure send...
-
34
Sending Mail via Gmail using OAuth2 (2022 Edition) (Shallow Thoughts) Akkana's Musings on Open Source Computing and Technology, Science, and Nature.
-
9
go-mail - Easy to use, yet comprehensive library for sending mails with Go
-
12
Various Ways of Sending Mail via SMTP...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK