无法从谷歌应用引擎发送电子邮件

时间:2013-01-09 11:55:03

标签: java security google-app-engine email javamail

我正在使用GAE版本1.7.3并使用java mail api发送电子邮件。在本地开发服务器上,邮件发送正常。但在部署到app引擎后,我得到了以下异常

com.google.apphosting.api.ApiProxy$CallNotFoundException: The API package 'remote_socket' or call 'Resolve()' was not found.
    at java.lang.Thread.getStackTrace(Thread.java:1495)
    at com.google.apphosting.runtime.ApiProxyImpl.doSyncCall(ApiProxyImpl.java:240)
    at com.google.apphosting.runtime.ApiProxyImpl.access$000(ApiProxyImpl.java:66)
    at com.google.apphosting.runtime.ApiProxyImpl$1.run(ApiProxyImpl.java:183)
    at com.google.apphosting.runtime.ApiProxyImpl$1.run(ApiProxyImpl.java:180)

    at java.security.AccessController.doPrivileged(Native Method)

    at com.google.apphosting.runtime.ApiProxyImpl.makeSyncCall(ApiProxyImpl.java:180)

    at com.google.apphosting.runtime.ApiProxyImpl.makeSyncCall(ApiProxyImpl.java:66)

    at com.google.apphosting.api.ApiProxy.makeSyncCall(ApiProxy.java:107)
    at com.google.apphosting.api.ApiProxy.makeSyncCall(ApiProxy.java:56)

    at com.google.appengine.api.socket.SocketApiHelper.apiProxyMakeSyncCall(SocketApiHelper.java:90)

    at com.google.appengine.api.socket.SocketApiHelper.makeSyncCall(SocketApiHelper.java:58)

    at com.google.appengine.api.socket.NameServiceImpl.lookupAllHostAddr(NameServiceImpl.java:61)

    at com.google.apphosting.util.ResolverManager$AppEngineNameservice.lookupAllHostAddr(ResolverManager.java:42)

    at java.net.InetAddress.getAddressFromNameService(InetAddress.java:1353)
    at java.net.InetAddress.getAllByName0(InetAddress.java:1303)

    at java.net.InetAddress.getAllByName(InetAddress.java:1232)

    at java.net.InetAddress.getAllByName(InetAddress.java:1163)

    at java.net.InetAddress.getByName(InetAddress.java:1113)

    at javax.mail.Service.connect(Service.java:257)

    at javax.mail.Service.connect(Service.java:91)

    at javax.mail.Service.connect(Service.java:76)

    at javax.mail.Transport.send(Transport.java:94)
    at javax.mail.Transport.send(Transport.java:48)
    at com.test.igreetings.service.impl.EmailImpl.sendEmail(EmailImpl.java:61)

以下是我发送电子邮件的代码:

try {
            // Email is my custom pojo class
            Email email = new Email();
            email.setMailHost("smtp.gmail.com");
            email.setAuth("true");
            email.setFrom("username@gmail.com");
            email.setTo("targetemail@example.com");
            email.setSubject("test);
            email.setMessage("test body");
            email.setPort("25");
            email.setStarttls("true");
            email.setLogin("username@gmail.com");
            email.setPassword("pwd");

            Properties props = new Properties();
            props.setProperty("mail.host", email.getMailHost());
            props.setProperty("mail.smtp.port", email.getPort());
            props.setProperty("mail.smtp.auth", email.getAuth());
            props.setProperty("mail.smtp.starttls.enable", email.getStarttls());

            Authenticator auth = new SMTPAuthenticator(email.getLogin(), email.getPassword());

            Session session = Session.getInstance(props, auth);

            MimeMessage msg = new MimeMessage(session);
            msg.setText(email.getMessage());
            msg.setSubject(email.getSubject());
            msg.setFrom(new InternetAddress(email.getFrom()));
            msg.addRecipient(Message.RecipientType.TO, new InternetAddress(email.getTo()));
            Transport transport = session.getTransport("smtp");
            transport.connect(email.getMailHost(), 25, "username@gmail.com", "pwd");

            transport.sendMessage(msg, msg.getAllRecipients());

            transport.close();
        } catch (AuthenticationFailedException ex) {
            Logging.log("AuthenticationFailedException in sendEmail: " + ex.toString());
            ex.printStackTrace();

        } catch (AddressException ex) {

            Logging.log("AddressException in sendEmail: " + ex.toString());
            ex.printStackTrace();
        } catch (MessagingException ex) {
            Logging.log("MessagingException in sendEmail: " + ex.toString());
            ex.printStackTrace();
        }

编辑:

我根据您的建议更改了代码,这是我更新的代码:

Properties props = new Properties();
                        Session session = Session.getDefaultInstance(props, null);
                        Message msg = new MimeMessage(session);
                        msg.setFrom(new InternetAddress("username@gmail.com"));
                        msg.addRecipient(Message.RecipientType.TO,
                        new InternetAddress("targetemail@example.com"));
                        msg.setSubject("test");
                        msg.setText("test");
                        Transport.send(msg);

我得到类似的异常未找到API包'remote_socket'或调用'CreateSocket()'。上述发件人也已注册为此应用程序的开发人员。

2 个答案:

答案 0 :(得分:2)

由于@Shay注意到您无法将外部SMTP提供程序与GAE一起使用。 GAE使用它自己的SMTP网关。

GAE上的

相关问题:您只能通过Google帐户发送来自其中一个应用管理员(GAE管理员 - >权限)或当前登录用户的来自(即来自字段)的电子邮件。注意:这仅适用于通过Google帐户登录的用户,而不适用于通过联合登录(例如OpenID)登录的用户:Sending email on behalf of Google Apps user(答案来自Google工程师)

更新:如前所述,username@gmail.com必须是logged into your GAE app via Google Apps login或用户必须是GAE应用管理员的用户的电子邮件。

Update2:这对我有用:

Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
try {
    Message msg = new MimeMessage(session);
    msg.setFrom(new InternetAddress("admin@domain.com", "Admin", "UTF-8"));
    msg.addRecipient(Message.RecipientType.TO, new InternetAddress(recipientAddress));
    msg.setSubject(emailSubject);
    msg.setText(emailText);
    Transport.send(msg);

} catch (AddressException e) {
// TO address not valid
} catch (MessagingException e) {
    // other email error
} catch (UnsupportedEncodingException e) {
    // should not happen UTF-8 is always available
}

答案 1 :(得分:1)

  

应用无法使用JavaMail界面连接到其他邮件   用于发送或接收电子邮件的服务。 SMTP配置   添加到传输或会话被忽略。

删除端口,用户,密码和邮件主机设置,它应该可以工作。