我試圖使用以下方法我的程序發送電子郵件凍結:JVM嘗試發送電子郵件
void sendEmails(Tutor t, Date date, Time time, String tuteeName, String tuteeEmail){
System.out.println("sending emails");
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "465");
SimpleDateFormat timeFmt = new SimpleDateFormat("hh:mm a");
SimpleDateFormat dateFmt = new SimpleDateFormat("EEE, MMMM dd");
String datePrint = dateFmt.format(date);
String timePrint = timeFmt.format(time);
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message tutorMessage = new MimeMessage(session);
tutorMessage.setFrom(new InternetAddress("[email protected]"));
tutorMessage.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(t.getEmail()));
tutorMessage.setSubject("Tutoring Appointment Scheduled");
tutorMessage.setText("Hey " + t.getName() +
"\n \t You have a new appointment scheduled on " + datePrint + " at " + timePrint +
"with " + tuteeName + ". \n If you cannot make this appointment, please contact club leadership immediately. "
+ "\n Thanks for helping out!");
Transport.send(tutorMessage);
System.out.println("Done sending");
} catch (MessagingException e) {
System.out.println("messagingerror");
e.printStackTrace();
}
}
但是當程序運行到這個方法中,GUI鎖死和程序凍結。這是我第一次嘗試在一個Java程序中使用電子郵件,所以我不確定這是什麼東西。
我懷疑它無法連接到SMTP服務器。請檢查服務器是否啓動。 – 2013-03-16 03:58:18