【问题标题】:Configure Spring Boot to use my Gmail smtp配置 Spring Boot 以使用我的 Gmail smtp
【发布时间】:2018-11-16 18:09:03
【问题描述】:

有没有办法用 application.properties 文件中的条目替换以下代码:

属性 props = this.mailSender.getJavaMailProperties(); props.put("mail.smtp.starttls.enable", "true");

问候

【问题讨论】:

    标签: java spring-boot smtp gmail


    【解决方案1】:

    只需将这些放在 application.properties 上。其他按原样。

    #SMTP configuration
        spring.mail.host=smtp.gmail.com
        spring.mail.port=587
        spring.mail.username=klklk@gmail.com
        spring.mail.password=lkkl
        spring.mail.properties.mail.smtp.starttls.enable=true
        spring.mail.properties.mail.smtp.ssl.trust=smtp.gmail.com
        spring.mail.properties.mail.smtp.starttls.required=true
        spring.mail.properties.mail.smtp.auth=true
        spring.mail.properties.mail.smtp.connectiontimeout=5000
        spring.mail.properties.mail.smtp.timeout=5000
        spring.mail.properties.mail.smtp.writetimeout=5000
    

    这样写邮件发送者类

    @Service
    public class EmailServiceImpl implements EmailService {
        @Autowired
        public JavaMailSender emailSender;
        private final String imageLink = "images/teddy.jpeg";
        private final String imageNameToSend = "teddy.jpeg";
    
        @Override
        public void sendSimpleMessage(String to, String subject, String text) throws IOException, MessagingException {
            MimeMessage message = emailSender.createMimeMessage();
            MimeMessageHelper helper = null;
            try {
                helper = new MimeMessageHelper(message, true);
    
    
                helper.setTo(to);
                helper.setSubject(subject);
    
                FileSystemResource file
                        = new FileSystemResource(new ClassPathResource(imageLink).getFile());
                helper.addAttachment(imageNameToSend, file);
    
                MimeBodyPart messageBodyPart = new MimeBodyPart();
                messageBodyPart.setContent(text, "text/html");
    
    
                Multipart multipart = new MimeMultipart();
                multipart.addBodyPart(messageBodyPart);
    
                message.setContent(multipart);
                emailSender.send(message);
            } catch (MessagingException | IOException e ) {
                log.info("Exception catched {}",e);
                throw e;
            }
        }
    }
    

    邮件服务会是这样的

    public interface EmailService {
        void sendSimpleMessage(
                String to, String subject, String text) throws IOException, MessagingException;
    }
    

    将以下库添加到 build.gradle 文件中,或者如果您正在使用其他构建工具,请将此库添加到相应的文件中

    compile('org.springframework.boot:spring-boot-starter-mail:1.5.9.RELEASE')
    

    【讨论】:

    • 不起作用,我收到以下错误:消息 1 失败:com.sun.mail.smtp.SMTPSendFailedException:530 5.7.0 必须先发出 STARTTLS 命令。 k4sm31119026wrx.91 - gsmtp ] 根本原因 org.springframework.mail.MailSendException:失败的消息:com.sun.mail.smtp.SMTPSendFailedException:530 5.7.0 必须首先发出 STARTTLS 命令。 k4sm31119026wrx.91 - gsmtp
    • 试试 spring.mail.properties.mail.smtp.starttls.enable=false
    • 检查我更新的代码并遵循这个。你也可以看看这个项目github.com/sanjoy-sust/FreightManagement/tree/develop/src/main/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-21
    • 1970-01-01
    • 2012-02-14
    • 2017-02-26
    • 2017-05-12
    • 2018-06-14
    相关资源
    最近更新 更多