簡體   English   中英

為什么我的Google App Engine項目沒有發送電子郵件?

[英]Why is my google app engine project not sending an email?

我完全按照本文上傳了一個測試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();
    }
}

將您的呼叫更改為setFrom()以使用《 開發人員指南》中允許的電子郵件地址:

要設置發件人地址,應用程序將在MimeMessage對象上調用setFrom()方法。 發件人地址必須是以下類型之一:

  • 該應用程序的注冊管理員地址
  • 使用Google帳戶登錄的當前請求的用戶地址。 您可以使用Users API確定當前用戶的電子郵件地址。 該用戶的帳戶必須是Gmail帳戶,或者位於由Google Apps管理的域上。
  • 該應用程序的任何有效電子郵件接收地址(例如xxx@APP-ID.appspotmail.com)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM