初めてJavaでSMTPを操作しようとしています。しかし、ログインしてテスト メールを送信することさえできない可能性があります。
public void sendMail() throws AddressException {
final String username = "myusername@gmail.com";
final String password = "*******";
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", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("from@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("to@gmail.com"));
message.setSubject("Subject");
message.setText("Just Test Mail");
Transport.send(message);
System.out.println("SENT!");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
このコードの問題点がわかりません。正常にコンパイルされますが、メールを送信しようとすると、次のデバッグ メッセージが表示されます。
Caused by: java.lang.RuntimeException: javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbuR
534-5.7.14 EY2k-9Ce8uWcOC3c1LSLdlFWcaIpFFpopYCeQy2niL0hD-knkk0Q58ObpnzTiGohEIx7Qz
534-5.7.14 jXIdMVyr2CxEqnckmBMlA-IBxZnmSObE6uby8wATa6inRuZyaLWWE-gIsk5HiExKJzkNke
534-5.7.14 LIoQEXxNYLfDrJQhYtNFJzHy1bl5ke5UqDUbReJyrEjLritFV1n5OalHDWD0Z32v75FzXS
534-5.7.14 ttXQra4ExolIcZR7TSeU2_xGuS0g0> Please log in via your web browser and
534-5.7.14 then try again.
534-5.7.14 Learn more at
534 5.7.14 https://support.google.com/mail/answer/78754 e6sm10484931pad.0 - gsmtp
at money.SendMailController.sendMail(SendMailController.java:48)
at money.MainController.sendMail(MainController.java:477)
... 53 more
Caused by: javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbuR
534-5.7.14 EY2k-9Ce8uWcOC3c1LSLdlFWcaIpFFpopYCeQy2niL0hD-knkk0Q58ObpnzTiGohEIx7Qz
534-5.7.14 jXIdMVyr2CxEqnckmBMlA-IBxZnmSObE6uby8wATa6inRuZyaLWWE-gIsk5HiExKJzkNke
534-5.7.14 LIoQEXxNYLfDrJQhYtNFJzHy1bl5ke5UqDUbReJyrEjLritFV1n5OalHDWD0Z32v75FzXS
534-5.7.14 ttXQra4ExolIcZR7TSeU2_xGuS0g0> Please log in via your web browser and
534-5.7.14 then try again.
534-5.7.14 Learn more at
534 5.7.14 https://support.google.com/mail/answer/78754 e6sm10484931pad.0 - gsmtp
at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:932)
at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:843)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:748)
at javax.mail.Service.connect(Service.java:388)
at javax.mail.Service.connect(Service.java:246)
at javax.mail.Service.connect(Service.java:195)
at javax.mail.Transport.send0(Transport.java:254)
at javax.mail.Transport.send(Transport.java:124)
at money.SendMailController.sendMail(SendMailController.java:43)
... 54 more
Java経由で実際にメールを送信するにはどうすればよいですか?