Can I send email from GAE/Java running in Eclipse?

91 views
Skip to first unread message

Rick Horowitz

unread,
Jan 10, 2010, 10:40:31 AM1/10/10
to Google App Engine
I'm trying to test sending email using Java from GAE SDK 1.3 while in
development mode using the Eclipse Plugin. I'm using Eclipse Build id:
20090920-1017 and Java OpenJDK 1.6. Is this possible, or can I only
send email when my app is actually deployed? I read the docs and I am
aware that it says in development mode, sent mail is displayed in the
log (when using the example code in the Java GAE doc). I tried that
and couldn't figure out how to get my sent email to show up on the
log. So I then tried to use smtp to send an email to my Gmail account
from GAE. It seems that this should be possible since there is clear
documentation for Python on how to do this. Again, it was not clear to
me how to apply the Python instructions to Java, nor was it clear
whether this works in Java.

Here's what I did to try to use smtp to send an email to my Gmail
account from GAE/Java.

I ran a simple standalone Java test program to verify that I can use
JavaMail to send to my Gmail account using smtps. No problem with
that. I have now tried a number of variations to try to get the same
thing to work with GAE. Here's the basic code (adapted from the
JavaMail demo program for Gmail):

/**
* Send the email message
* @param toAddr Email address to whom to send the email
* @param subject The email's subject field
* @param msgBody The email's message body
*/
protected void sendMsg(InternetAddress toAddr, String subject, String
msgBody) throws Exception {

String host = "smtp.gmail.com";
String username = gmailUsername;
String password = gmailPassword;
Properties props = new Properties();
props.put("mail.smtps.auth", "true");
props.put("mail.transport.protocol", "smtps");

Session session = Session.getDefaultInstance(props, null);
Transport t = null;
try {
t = session.getTransport();
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(gmailEmailAddr));
msg.addRecipient(Message.RecipientType.TO, toAddr);
msg.setSubject(subject);
msg.setText(msgBody);
// set the message content here
t.connect(host, username, password);
t.sendMessage(msg, msg.getAllRecipients());
} finally {
try {
if (t != null)
t.close();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

When I run this, I get the following exception:

javax.mail.NoSuchProviderException: Unable to locate provider for
protocol: smtps
at javax.mail.Session.getProvider(Session.java:229)
at javax.mail.Session.getTransport(Session.java:338)
at javax.mail.Session.getTransport(Session.java:327)
at com.bodyfixit.server.AbstractServiceImpl.sendMsg
(AbstractServiceImpl.java:53)

I then tried to replace the GAE mail implementation with JavaMail
1.4.1 by copying mail.jar to my WEB-INF/lib folder. When I call my
sendMsg() method (see above) I now get the following exception:

Caused by: java.lang.NoClassDefFoundError:
javax.net.ssl.SSLSocketFactory is a restricted class. Please see the
Google App Engine developer's guide for more details.
at com.google.appengine.tools.development.agent.runtime.Runtime.reject
(Runtime.java:51)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:
225)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:
1359)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:
412)
at javax.mail.Service.connect(Service.java:288)
at javax.mail.Service.connect(Service.java:169)
at com.bodyfixit.server.AbstractServiceImpl.sendMsg
(AbstractServiceImpl.java:60)

Reply all
Reply to author
Forward
0 new messages