我已经完全按照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();
}
}
答案 0 :(得分:1)
将您的来电更改为setFrom()
,以使用Developers Guide中允许的电子邮件地址:
要设置发件人地址,应用程序会调用setFrom()方法 MimeMessage对象。发件人地址必须是以下之一 类型:
- 应用程序的注册管理员的地址
- 使用Google帐户登录的当前请求的用户地址。您可以使用Users API确定当前用户的电子邮件地址。用户的帐户必须是Gmail帐户,或者位于由Google Apps管理的域中。
- 该应用的任何有效电子邮件接收地址(例如xxx@APP-ID.appspotmail.com)。