2013-11-23 18 views
2

我已經正確地上傳了一個測試html頁面和servlet後面的this article。這工作,並會給我發一封電子郵件。但是,當我將這些代碼幾乎完全複製到下面顯示的代碼中的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("[email protected]", "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(); 
    } 
} 
+0

你怎麼知道它不發送**電子郵件? –

+1

好吧,我假設因爲我發送給自己,我沒有收到它。 – Nick

+0

您是否檢查過**垃圾郵件**文件夾? –

回答

1

您的通話更改爲setFrom()使用在Developers Guide允許的電子郵件地址:

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

  • 註冊管理員爲應用
  • 用戶與谷歌的帳戶登錄當前請求的地址的地址。您可以使用用戶API確定當前用戶的電子郵件地址。用戶的帳戶必須是Gmail帳戶,或者位於Google Apps管理的域中。
  • 該應用的任何有效的電子郵件接收地址(例如[email protected])。
+1

已經是,這個問題的未來圍觀者的好建議。看起來有些錯誤,因爲我今天早上重新部署了它,並且它剛剛工作......最有可能的是黑魔法。 – Nick

相關問題