Armand Ndizigiye
Armand Ndizigiye

Reputation: 199

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);

Upvotes: 5

Views: 6768

Answers (4)

Apparently, GAE doesn't allow the use of the admin accounts any more. you need to use the service account: [email protected]

My previous projects still work with admin accounts, but the recently created projects just don't allow me to use any of the admin accounts.

Upvotes: 1

Taranjeet Singh
Taranjeet Singh

Reputation: 337

Other than email not working on localhost or due to the sender email not being the authenticated one, I have experienced that email does not work even when the version is not the default one. I could not find this documented anywhere.

For example: nondefaultversion-dot-myapp.appspot.com (email does not work, no error logs) myapp.appspot.com (email works)

Please confirm if others have also faced this issue.

Upvotes: 0

jimr
jimr

Reputation: 11230

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

Upvotes: 10

Lai Xin Chu
Lai Xin Chu

Reputation: 2482

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

Reason is because the SMTP server needs to authenticate you.

Upvotes: 1

Related Questions