Spring Boot 如何在通过SMTP服务器发送电子邮件时自动进行身份验证

jutyujz0  于 2023-08-04  发布在  Spring
关注(0)|答案(1)|浏览(98)

我试图实现一个邮件发件人功能,但在测试过程中,这个错误不断弹出
不接受用户名和密码。欲知详情,请访问535 5.7.8 https://support.google.com/mail/?p=BadCredentials h25-20020a170906855900b00993017b64a9sm7314529ejy.223 - gsmtp
用户名和密码100%正确。当我做了一些研究,我发现,使这一过程成为可能,我必须“允许不太安全的应用程序”,但谷歌删除了这一功能前一段时间。下面是spring mail的配置和方法sendEmail

  • 应用程序.yml:*
mail:
    host: smtp.gmail.com
    port: 587
    username: emailtestingtool1.0@gmail.com
    password: ****
    properties:
        mail:
            smtp:
                auth: true
                starttls:
                    enable: true

字符串

  • sendEmail* 方法:
private void sendEmail(String email, String resetPasswordLink) throws MessagingException, UnsupportedEncodingException {

        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message);

        helper.setFrom("emailtestingtool1.0@gmail.com", "SBI intern");
        helper.setTo(email);

        String subject = "Here's the link to reset your password";

        String content = "<p>Hello, </p>"
                + "<p>You have requested to reset ur password.</p>"
                + "<p>Click the link below:</p>"
                + "<p><b><a href= \"" + resetPasswordLink + "\">Change my password</a></b></p>"
                + "<p>Ignore this email if u have not made the request.</p>";

        helper.setSubject(subject);
        helper.setText(content, true);

        mailSender.send(message);
    }

b09cbbtk

b09cbbtk1#

您还可以使用以下代码使用javax.mail发送电子邮件

import javax.mail*;
    
    public String sendMails() {

        String resp;

        String host = "smtp.gmail.com";
        final String user = "sender@gmail.com"; 
        final String pwd = "**application password**"; //https://support.google.com/accounts/answer/185833?visit_id=638140255515756664-3142292480&p=InvalidSecondFactor&rd=1
        final String to = "recipient@gmail.com";

        Properties properties = new Properties();
        properties.put("mail.debug", "false");
        properties.put("mail.smtp.host", host);
        properties.put("mail.smtp.port", 587);
        properties.put("mail.smtp.auth", true);
        properties.put("mail.smtp.starttls.enable", true);

        Session session = Session.getInstance(properties,
                new Authenticator() {

            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(user, pwd);
            }
        });
       
        try {
            String msg = "Test email body";

            MimeMessage message = new MimeMessage(session);

            //Set email header & From address
            message.setFrom(new InternetAddress("Email header <non-reply@domain.com>\r\n"));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            message.setSubject("Test email subject");

            Multipart multipart = new MimeMultipart("alternative");
            MimeBodyPart mimeBodyPart = new MimeBodyPart();
            mimeBodyPart.setContent(msg, "text/html; charset=utf-8");
            multipart.addBodyPart(mimeBodyPart);
            message.setContent(multipart);

            //send the message
            Transport.send(message);

            resp = "SUCCESS:Email sent successfully";
        } catch (MessagingException ex) {
            resp = "FAIL:" + ex.getMessage();

        }

        return resp;
    }

字符串
要为发件人的gmail帐户生成应用程序密码,请按照this link操作

相关问题