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

Posted

技术标签:

【中文标题】javax.mail.MessagingException:AppEngine 应用程序中的连接错误【英文标题】:javax.mail.MessagingException: Connection error in AppEngine app 【发布时间】:2012-11-10 09:22:16 【问题描述】:

在我的 GAE 应用程序中,我无法使用 Java 邮件 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); 而不是创建 Transport 实例,我不会收到任何错误消息,但不会发送电子邮件。

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

【问题讨论】:

【参考方案1】:

我终于找到了答案…… 问题是,Google 不允许您使用外部邮件 ID(非 gmail 或非 google 邮件 ID),因此您无法配置传输对象的连接属性。

您可以在 message.setFrom() 中指定您的应用引擎管理员邮件 ID,然后简单地使用 Transport.send() 发送它;

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

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

请参阅This google developers page 了解更多信息。

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

【讨论】:

以上是关于javax.mail.MessagingException:AppEngine 应用程序中的连接错误的主要内容,如果未能解决你的问题,请参考以下文章