【问题标题】:Why is my google app engine project not sending an email?为什么我的谷歌应用引擎项目没有发送电子邮件?
【发布时间】:2013-11-23 14:56:31
【问题描述】:

我在this article 之后上传了一个测试html 页面和servlet。这有效,并将给我发送一封电子邮件。但是,当我将这段代码几乎完全复制到下面显示的代码中的 SendEmail 方法中时,它不会发送电子邮件。我知道当我在本地运行它时,它可以很好地使用 SendEmail 方法(但您不能使用 GAE 中的开发服务器发送电子邮件)。当我部署它时,页面或日志中没有错误,所以它看起来很旧,似乎只是没有发送电子邮件。有人知道原因吗?

public class EmailService {
    private static SimpleDateFormat dateFormatter = new SimpleDateFormat ("MM/dd/yyyy");

    public static void SendDeadlineEmails() {
        PersistenceManager pm = getPersistenceManager();
        try {
            List<DeadlineEmailObject> studentsWithDeadlineToday = populateEmailList(pm);
            sendEmails(studentsWithDeadlineToday);
        } finally {
            pm.close();
        }
    }

    private static List<DeadlineEmailObject> populateEmailList(PersistenceManager pm) {
        List<Student> students =  getStudents(pm);
        List<DeadlineEmailObject> studentsWithDeadlineToday = new ArrayList<DeadlineEmailObject>();
        String today = dateFormatter.format(System.currentTimeMillis());

        for(Student student : students) {
            Set<Charge> charges = student.getCharges();
            if(charges != null) {
                for(Charge charge : charges) {
                    String deadline = dateFormatter.format(charge.getDeadline());
                    if(deadline.equals(today)) {
                        studentsWithDeadlineToday.add(new DeadlineEmailObject(student, charge));
                    }
                }
            }
        }
        return studentsWithDeadlineToday;
    }

    @SuppressWarnings("unchecked")
    private static List<Student> getStudents(PersistenceManager pm) {
        return (List<Student>) pm.newQuery(Student.class).execute();
    }

    private static void sendEmails(List<DeadlineEmailObject> studentsWithDeadlineToday) {
        for(DeadlineEmailObject emailObj : studentsWithDeadlineToday) {
            sendEmail(emailObj);
            System.out.println("Student: " + emailObj.getStudent().getFullName() + "\nAmount: " + emailObj.getCharge().getAmount() + 
                    "\nDeadline: " + dateFormatter.format(emailObj.getCharge().getDeadline()));
        }
    }

    private static void sendEmail(DeadlineEmailObject emailObj) {
        Properties props = new Properties();
        Session session = Session.getDefaultInstance(props, null);
        try {
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress("njbuwm@gmail.com", "Admin"));
            msg.addRecipient(Message.RecipientType.TO, new InternetAddress(emailObj.getStudent().getEmail(), emailObj.getStudent().getFullName()));
            msg.setSubject("Deadline Reached");
            msg.setText(buildMessage(emailObj));
            Transport.send(msg);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private static String buildMessage(DeadlineEmailObject emailObj) {
        String email = "";
        email += "Dear " + emailObj.getStudent().getFullName() + " ,\n";
        email += "You owe us money. This much: $" + emailObj.getCharge().getAmount() + ".\n";
        email += "For this reason: " + emailObj.getCharge().getReason() + ".\n";
        email += "The deadline is today and I advise you to pay it or you will be deported to Idontpaymybills Island forever.\n";
        email += "Thank you,\n Automated Emailer";
        return email;
    }

    private static PersistenceManager getPersistenceManager() {
        return JDOHelper.getPersistenceManagerFactory("transactions-optional").getPersistenceManager();
    }
}

【问题讨论】:

  • 你怎么知道它没有发送电子邮件?
  • 好吧,我假设是因为我将它发送给自己而我没有收到它。
  • 你检查过你的垃圾邮件文件夹了吗?

标签: java google-app-engine email jakarta-mail


【解决方案1】:

将您的电话改为setFrom(),以使用Developers Guide 中允许的电子邮件地址:

为了设置发件人地址,应用程序调用 setFrom() 方法 MimeMessage 对象。发件人地址必须是以下之一 类型:

  • 应用程序注册管理员的地址
  • 使用 Google 帐户登录的当前请求的用户地址。您可以使用用户 API 确定当前用户的电子邮件地址。用户的帐户必须是 Gmail 帐户,或者位于由 Google Apps 管理的域中。
  • 应用的任何有效电子邮件接收地址(例如 xxx@APP-ID.appspotmail.com)。

【讨论】:

  • 已经是,但是对于这个问题的未来旁观者来说是个好建议。似乎有些问题,因为我今天早上重新部署并且它刚刚工作......很可能是黑魔法。
猜你喜欢
  • 2012-06-13
  • 2011-08-16
  • 1970-01-01
  • 2017-06-27
  • 1970-01-01
  • 2012-07-18
  • 2011-07-23
  • 2023-03-07
  • 1970-01-01
相关资源
最近更新 更多