无法在AppEngine教程中发送电子邮件

时间:2016-02-26 01:59:13

标签: android google-app-engine servlets firebase

我只是按照以下所述的教程进行操作:https://cloud.google.com/solutions/mobile/firebase-app-engine-android-studio添加后端模块部分。

我的Servlet代码是:

import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.ValueEventListener;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import java.util.logging.Logger;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyServlet extends HttpServlet {
    static Logger Log = Logger.getLogger("com.example.username.myapplication.backend.MyServlet");

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {
        Log.info("Got cron message, constructing email.");

        //Create a new Firebase instance and subscribe on child events.
        Firebase firebase = new Firebase("[firebase-DB]");
        firebase.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                // Build the email message contents using every field from Firebase.
                final StringBuilder newItemMessage = new StringBuilder();
                newItemMessage.append("Hello main user");


                //Now Send the email
                Properties props = new Properties();
                Session session = Session.getDefaultInstance(props, null);
            try {
                Message msg = new MimeMessage(session);
                //Make sure you substitute your project-id in the email From field
                msg.setFrom(new InternetAddress("anything@firebasebackendserver.appspotmail.com",
                        "Todo Nagger"));
                msg.addRecipient(Message.RecipientType.TO,
                        new InternetAddress("myEmail@gmail.com", "Recipient"));
                msg.setSubject("Good Morning!");
                msg.setText(newItemMessage.toString());
                Transport.send(msg);
            } catch (MessagingException | UnsupportedEncodingException e) {
                Log.warning(e.getMessage());
            }
            }

            public void onCancelled(FirebaseError firebaseError) {
            }
        });
    }

}

cron.xml文件为:

<?xml version="1.0" encoding="UTF-8"?>
<cronentries>
    <cron>
        <url>/hello</url>
        <description>Send me an email of outstanding items in the morning</description>
        <schedule>every 1 minutes</schedule>
    </cron>
</cronentries>

在Firebase中完成后,AppEngine仪表板确实注册了请求已发生,但是,从未发送过该电子邮件,我在日志中看到的只是错误:

Caused by: com.google.apphosting.api.ApiProxy$FeatureNotEnabledException: The Socket API will be enabled for this application once billing has been enabled in the admin console.

即使我已为此应用启用了结算功能。

在您真正开始使用结算之前,是否存在某种宽限期...?

代码布局有什么问题?

1 个答案:

答案 0 :(得分:0)

我怀疑你是从一个不允许的发件人那里发送的。这是[{3}}:

的摘录

出于安全考虑,邮件的发件人地址必须是以下之一:

  • 当前已登录的用户的Gmail或Google Apps帐户
  • 任何形式为anything@appname.appspotmail.com或anything@appalias.appspotmail.com
  • 的电子邮件地址
  • docs
  • 下的云平台控制台中列出的任何电子邮件地址

如果是这种情况,您会在Email API Authorized Senders中看到错误。