java - 使用 Gmail 从 Spring Boot 应用程序发送电子邮件

标签 java email spring-boot smtp gmail

我研究了以下问题和答案: Spring Boot - Could not connect to SMTP host: smtp.gmail.com, port: 25, response: 421

我想做同样的事情 - 使用 Gmail 服务器从 Spring Boot 应用程序发送电子邮件。

我的配置是这样的:

spring.mail.host = smtp.gmail.com
spring.mail.username = ***@otherdomain
spring.mail.password = ***
spring.mail.properties.mail.smtp.auth = true
spring.mail.properties.mail.smtp.starttls.enable=true 
spring.mail.properties.mail.smtp.socketFactory.port = 465
spring.mail.properties.mail.smtp.socketFactory.class = javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.smtp.socketFactory.fallback = false
spring.mail.properties.mail.smtp.ssl.enable = true

这是从另一个问题复制过来的,只是用户名和密码已根据我的需要进行了更改。我的 scienario 唯一不同的是电子邮件地址的域不是 gmail.com。

我的电子邮件客户端类是:

@Service
public class MailClient {

  @Autowired
  private JavaMailSender mailSender;

  public void prepareAndSend(String recipient, String message) {
    MimeMessagePreparator messagePreparator = mimeMessage -> {
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage);
        messageHelper.setFrom("me@otherdomain.org.uk");
        messageHelper.setTo(recipient);
        messageHelper.setSubject("Sample mail subject");
        messageHelper.setText(message);
    };
    mailSender.send(messagePreparator);
  }
}

我的异常(exception)是:

Failed message 1: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. g78sm6788965wrd.11 - gsmtp

最佳答案

您可以在不复制配置的情况下执行此操作(在这种情况下 From Email 除外,但这取决于您的 SMTP 提供商):

依赖性

"org.springframework.boot:spring-boot-starter-mail"

application.properties

spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=from.email@gmail.com
spring.mail.password=*******
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true

电子邮件服务

@Component
public class EmailService {

    private final JavaMailSender sender;

    @Autowired
    public EmailService(JavaMailSender sender) {
        this.sender = sender;
    }

    public void send(String emailTo, String subject, String body) {
        MimeMessagePreparator message = newMessage -> {
            newMessage.setRecipient(
                    Message.RecipientType.TO,
                    new InternetAddress(emailTo)
            );
            newMessage.setFrom("from.email@gmail.com");
            newMessage.setSubject(subject);
            newMessage.setText(body);
        };

        this.sender.send(message);
    }
}

关于java - 使用 Gmail 从 Spring Boot 应用程序发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43142137/

相关文章:

java - 我一直在尝试通过 Liferay 中的 Java Web 应用程序发送邮件,但似乎没有任何效果

java - 如何使用 Nimbus LookAndFeel 更改 JToolTip 的背景颜色?

c# - 使用 System.Net.Mail 通过谷歌发送 smtp 邮件让我超时异常

java - 将 Spring Boot 服务注入(inject)非托管类

java - 在Java中将XML作为字符串发布,何时刷新并关闭连接?

Java 导入与代码性能

java - 无法使用 mvn spring-boot :build-image 构建 war 文件的镜像

java - 使用 Spring boot 和 Jackson 的日期时区

PHP HTML 电子邮件字符问题

c# - 在电子邮件中发送表格