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

时间:2017-03-31 13:37:47

标签: 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("staffing@readingbeerfestival.org.uk");
        messageHelper.setTo(recipient);
        messageHelper.setSubject("Sample mail subject");
        messageHelper.setText(message);
    };
    mailSender.send(messagePreparator);
  }
}

我的例外是:

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

3 个答案:

答案 0 :(得分:4)

您可以在不重复配置的情况下执行此操作(在这种情况下,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

<强> EmailService

@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);
    }
}

答案 1 :(得分:3)

要回答我自己的问题,以防其他人发现它有用:

所需的一组属性是:

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

您还需要自己提供JavaMailSender并设置其他属性:

@Bean
public JavaMailSender getJavaMailSender() {
    JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
    mailSender.setHost("smtp.gmail.com");
    mailSender.setPort(587);

    mailSender.setUsername("***");
    mailSender.setPassword("***");

    Properties props = mailSender.getJavaMailProperties();
    props.put("mail.transport.protocol", "smtp");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.debug", "true");

    return mailSender;
}

不确定为什么我需要重复配置,但这对我有用。参考:baeldung.com

答案 2 :(得分:1)

您使用了 smtp.gmail.com ,因此电子邮件应为 gmail

编辑: 在我的应用程序中 spring mvc 我喜欢这样: 服务:

import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;


    /**
         * This method will send compose and send the message
         * */
        public void sendMail(String to, String subject, String body)
        {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setTo(to);
            message.setSubject(subject);
            message.setText(body);
            mailSender.send(message);
        }

在控制器中我使用了我的服务

applicationMailer.sendMail("*******@gmail.com", "Message from "+ userName , mailModel.getBody());

配置

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="smtp.gmail.com"/>
        <property name="port" value="587"/>  <!--   ou 25 -->
        <property name="username" value="*****@gmail.com"/>
        <property name="password" value="******"/>
        <property name="javaMailProperties">
            <props>
                <prop key="mail.transport.protocol">smtp</prop>
                <prop key="mail.smtp.auth">true</prop>
                <prop key="mail.smtp.starttls.enable">true</prop>
                <prop key="mail.debug">true</prop>
            </props>
        </property>
    </bean>