javax.mail.MessagingException:AppEngine应用程序中的连接错误

时间:2012-11-10 09:22:17

标签: google-app-engine javamail

在我的GAE应用程序中,我无法使用Java mail api发送邮件。 我收到了以下错误。

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

我的代码是

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

如何让我的应用程序正常运行?

此外,如果我使用Transport.send(message);而不是创建传输实例,我将不会收到任何错误消息,但不会发送电子邮件。

我该如何解决?有人帮助我。

1 个答案:

答案 0 :(得分:1)

最后我找到了答案...... 问题是,Google不允许您使用外部邮件ID(非gmail或非google mail-id),因此您无法为传输对象配置连接属性。

您只需在message.setFrom()中指定您的应用引擎管理员mail-id,然后使用Transport.send()发送它;

在我的情况下,Transport.send()什么也没做,只是因为我试图从我的本地eclipse项目发送它。 Google将仅从部署的应用程序发送邮件。

因此编写您的应用程序代码,如果没有错误,请将其部署到应用程序引擎。然后应用程序将开始发送邮件。

请参阅This google developers page 了解详情。

此外,删除所有Oracle javax.mail jar。您的应用引擎jar默认情况下具有特定于应用引擎的javax.mail类