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)