Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending email with google app engine

Im tring to send a simple email with this code using google app engine. But nothing happens, is there something i have to configure in order to use the mail api? This runs on localhost. I am using gmail as mail host.

   String host = "smtp.google.com";
String to = "[email protected]";
String from = "[email protected]";
String subject = "this is a test";
String messageText = "test";
boolean sessionDebug = false;
// Create some properties and get the default Session.
Properties props = System.getProperties();
props.put("mail.host", host);
props.put("mail.transport.protocol", "smtp");
Session mailSession = Session.getDefaultInstance(props, null);

// Set debug on the Session
// Passing false will not echo debug info, and passing True will.

mailSession.setDebug(sessionDebug);

// Instantiate a new MimeMessage and fill it with the 
// required information.

Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = { new InternetAddress(to) };
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(messageText);

// Hand the message to the default transport service
// for delivery.

Transport.send(msg);
like image 311
Armand Ndizigiye Avatar asked Jun 09 '12 03:06

Armand Ndizigiye


People also ask

What email does Google App Engine use?

The mail API provides two ways to send an email message: the mail. send_mail() function and the EmailMessage class. This page describes how to use the legacy bundled services and APIs. This API can only run in first-generation runtimes in the App Engine standard environment.

How do I send an email using GCP?

To send email from GCP, you first need to create an account. Once you have an account, you can create an email address and set up your GCP account to use that address for email. You can also use your GCP account to access your email messages in the Gmail web interface or through the Google Cloud Platform Console.

Can an API send an email?

An email API (application programming interface) gives applications access to the functionality available in an email platform, such as generating and sending transactional emails, manipulating templates, and enabling access to email metrics.


2 Answers

When running the AppEngine development server locally, anything sent via the Mail service will not actually be sent - it will just be logged to the console

See here

When an application running in the development server calls the Mail service to send an email message, the message is printed to the log. The Java development server does not send the email message.

In addition, the from address must be (from here)

  • The email of an app administrator
  • The email of the currently logged in user who signed in using a Google Account
  • A valid email receiving address from the app
like image 150
jimr Avatar answered Oct 26 '22 01:10

jimr


The sender should be your own Gmail email address instead of [email protected]

Reason is because the SMTP server needs to authenticate you.

like image 33
Lai Xin Chu Avatar answered Oct 26 '22 00:10

Lai Xin Chu