0

我第一次尝试在 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发送邮件的?

4

0 回答 0