【问题标题】:Cannot Send Email in AppEngine TutorialAppEngine 教程中无法发送电子邮件
【发布时间】:2016-06-09 02:20:07
【问题描述】:

我只是按照以下教程进行操作: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.

即使我已经为此应用启用了计费功能。

在启用计费后是否有某种宽限期才能真正开始使用它...?

代码布局有什么问题?

【问题讨论】:

    标签: android google-app-engine servlets firebase


    【解决方案1】:

    我怀疑您是从不允许的发件人那里发送的。这是docs的摘录:

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

    • 当前登录用户的 Gmail 或 Google Apps 帐户
    • 任何形式为anything@appname.appspotmail.com 或anything@appalias.appspotmail.com 的电子邮件地址
    • Cloud Platform Console 中Email API Authorized Senders 下列出的任何电子邮件地址

    如果是这种情况,您将在logs 中看到错误。

    【讨论】:

    • 另外,您可以从此页面将允许的发件人列入白名单 - console.cloud.google.com/appengine/settings
    • 我真的需要让这个工作,我们可以进入聊天吗?
    • 尝试改用 appspotmail.com 地址。因此,如果您的 appid 是 myawesomeapp,请将您的发件人设置为 fred@myawesomeapp.appspotmail.com。那应该工作。如果这行得通,那么你就有了一些基础。
    猜你喜欢
    • 1970-01-01
    • 2014-07-31
    • 1970-01-01
    • 2013-09-22
    • 2011-02-18
    • 1970-01-01
    • 1970-01-01
    • 2019-01-19
    相关资源
    最近更新 更多