Stack Exchange
javax.mail.MessagingException: Connection error in AppEngine app
In my GAE application, I am not able to send mail using Java mail api.
I am getting following error.
javax.mail.MessagingException: Connection error (java.net.SocketException: Permission denied: Not allowed to issue a socket bind: permission denied.)
    at org.apache.geronimo.javamail.transport.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:408)
    at javax.mail.Service.connect(Service.java:248)
    at com.soa.util.SendMailSSL.sendMail(SendMailSSL.java:54)
    at com.soa.managers.MailManager.mailApproved(MailManager.java:87)
    at com.soa.managers.WidgetManager.approveRequest(WidgetManager.java:216)
    at com.soa.servlets.WidgetServlet.doPost(WidgetServlet.java:71)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
    at com.google.appengine.api.socket.dev.DevSocketFilter.doFilter(DevSocketFilter.java:74)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.tools.development.ResponseRewriterFilter.doFilter(ResponseRewriterFilter.java:123)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:34)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:61)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.tools.development.StaticFileFilter.doFilter(StaticFileFilter.java:125)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.tools.development.BackendServersFilter.doFilter(BackendServersFilter.java:97)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
    at com.google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:94)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:383)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:326)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
    at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:938)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:755)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
    at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)
    at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
Caused by: java.net.SocketException: Permission denied: Not allowed to issue a socket bind: permission denied.
    at com.google.appengine.api.socket.SocketApiHelper.translateError(SocketApiHelper.java:94)
    at com.google.appengine.api.socket.SocketApiHelper.translateError(SocketApiHelper.java:105)
    at com.google.appengine.api.socket.SocketApiHelper.makeSyncCall(SocketApiHelper.java:71)
    at com.google.appengine.api.socket.AppEngineSocketImpl.createSocket(AppEngineSocketImpl.java:502)
    at com.google.appengine.api.socket.AppEngineSocketImpl.bind(AppEngineSocketImpl.java:518)
    at java.net.Socket.bind(Socket.java:577)
    at java.net.Socket.<init>(Socket.java:373)
    at java.net.Socket.<init>(Socket.java:249)
    at org.apache.geronimo.javamail.transport.smtp.SMTPTransport.getConnectedSocket(SMTPTransport.java:1096)
    at org.apache.geronimo.javamail.transport.smtp.SMTPTransport.getConnection(SMTPTransport.java:856)
    at org.apache.geronimo.javamail.transport.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:385)
    ... 40 more

My code is
package com.soa.util;

import java.util.*;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendMailSSL {
    public static boolean sendMail(String msg, String subject,
            List<String> recipients) {

        Address[] emails = new InternetAddress[recipients.size()];
        for (int i = 0; i < recipients.size(); i++) {
            try {
                //System.out.println(recipients.get(i));
                emails[i] = new InternetAddress(recipients.get(i));
            } catch (AddressException e) {
                e.printStackTrace();
            }
        }

        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.auth", "true");
        props.setProperty("mail.smtp.port", "587");
        props.put("mail.smtp.starttls.enable", "true");

        Session session = Session.getDefaultInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(
                                "my_mail_id@gmail.com", "password");
                    }
                });

        try {
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress("my_mail_id@gmail.com"));// Sender
                                                                            // Id.
            message.addRecipients(Message.RecipientType.TO, emails);
            message.setSubject(subject);
            message.setText(msg);

            // send message.
            //Transport.send(message);
            Transport transport = session.getTransport("smtp");
            transport.connect("smtp.gmail.com", 587, "my_mail_id@gmail.com", "password");
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
            System.out.println("message sent successfully");
            return true;
        } catch (MessagingException e) {
            e.printStackTrace();
            System.out.println("failed");
        }catch (Exception e) {
            e.printStackTrace();
            System.out.println("failed :(");
        }
        return false;
    }
}

How can I get my application working?
Also if i use Transport.send(message); instead of creating an instance of Transport, I will not get any error messages, but the email will not be sent.
How can I fix it. somebody pl help me.

At last I found the answer...
The problems was, Google does not allow you to use external mail ids (non gmail or non google mail-ids) and thus you can not configure connection properties to the transport object.
you can just specify your app engine admin mail-id at message.setFrom() and then simply send it using Transport.send();
In my case Transport.send() was doing nothing, simply because I was trying to send it from my local eclipse project. Google will send mails only from the application that are deployed.
So code your apllication, and if no errors, deploy it to app engine. Then the app will start sending mails.
refer This google developers page  for more info.
Additionally, remove all Oracle javax.mail jars. Your app engine jar's by default have app-engine specific javax.mail classes
