我試圖使用JavaMail API,通過我的Gmail帳戶的電子郵件發送,但我得到一個javax.mail.MessagingException的例外javax.mail.MessagingException的同時發送郵件使用JavaMail API
驗證碼:
public static Result sendMail() {
Map < String, String[] > values = request().body().asFormUrlEncoded();
String toAddresses = values.get("toaddrs")[0];
String subject = values.get("subject")[0];
String body = values.get("body")[0];
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("[email protected]", "samplepass");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddresses));
message.setSubject(subject);
message.setText(body);
Transport.send(message);
return ok("sent");
} catch (MessagingException e) {
return ok("Error in sending email");
}
}
在調試,我在這裏結束了在服務班組長
會拋出異常:javax.mail.MessagingException: Host, username, and password must be specified.
on'Transport.send(message);' – 2013-05-15 07:31:25