Я пытаюсь отправить как по электронной почте с помощью JavaMail API, с помощью моего аккаунта Gmail, но я получаю исключение, javax.mail.MessagingExceptionjavax.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");
}
}
на отладку, я в конечном итоге здесь в classs Service
, который бросает исключение: javax.mail.MessagingException: Host, username, and password must be specified.
на 'Transport.send (message);' –