【问题标题】:JAVA Spring mail sending via gmail通过gmail发送JAVA Spring邮件
【发布时间】:2017-09-20 02:22:57
【问题描述】:

我想使用 gmail smtp 服务器和 spring 邮件发件人发送电子邮件,它不会抛出任何异常但我的邮件没有发送我没有收到它。

Service 的外观如下:

@Service
public class MailSenderService {

    @Autowired
    public MailSender mailSender;

    public void prepareAndSend(String recipient, String message) {
        try {

            SimpleMailMessage mail = new SimpleMailMessage();
            String from = "testSender1@gmail.com";
            String to = "testRecipient1@gmail.com";
            String subject = "Test subject";
            mail.setFrom(from);
            mail.setTo(recipient);
            mail.setSubject(subject);
            mail.setText(message);
            mailSender.send(mail);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

应用程序属性

spring.mail.host = smtp.gmail.com
spring.mail.username = ********@gmail.com
spring.mail.password = ********
spring.mail.properties.mail.smtp.auth = 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

和pom依赖

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

我在端口 8080 上部署我的应用程序并使用这两个参数调用此服务,没有捕获到异常但在我的 testRecipient1 收件箱中我没有找到任何新邮件,我错过了什么?

【问题讨论】:

标签: email spring-boot gmail


【解决方案1】:

如果您没有在 Web 应用程序中使用 SSL 证书而不是尝试我的代码,它在 spring boot 中运行良好,文本邮件和 HTML 邮件都运行良好。

@Autowired
private JavaMailSender mailSender;

@RequestMapping(value="/ShareVecancy",method=RequestMethod.GET)
public ModelAndView sendEmailToReference(HttpServletRequest request,HttpSession session){

    try {

            MimeMessage mail = mailSender.createMimeMessage();
            MimeMessageHelper messageHelper = new MimeMessageHelper(mail, true);
            messageHelper.setTo("reciverEmail@mail.com");
            messageHelper.setSubject("Testing mail");
            messageHelper.setText("HTML Text or Any text You want to send ", true);
            mailSender.send(mail);

        } catch (MailException e) {
            e.printStackTrace();
        }


}

属性文件:::

spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=xyz@gmail.com
spring.mail.password=*******
spring.mail.protocol=smtp
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.default-encoding=UTF-8

依赖::

 <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>

【讨论】:

    【解决方案2】:

    application.yml 示例(使用 Spring Boot 2.1.1 测试):

    spring
      mail:
        host: smtp.gmail.com
        username: youremail@gmail.com
        port: 587
        password: ******
        protocol: smtp
        properties:
          mail:
            smtp:
              starttls:
                enable: true
    

    【讨论】:

    • 虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
    【解决方案3】:

    您也可以尝试以下设置,但我无法提供有关此的更多信息,因为这将是一个很长的帖子。但是GeeksForGeeks 确实解释了 spring 邮件属性中的所有内容,谷歌也有它自己的指导方针,你可以找到 here

    附言我还从 gmail 帐户设置中禁用了对不太安全的应用程序的访问

    下面是application.properties

    spring.mail.host=smtp.gmail.com
    spring.mail.port=465
    spring.mail.username=xxxxx@gmail.com
    spring.mail.password=*****
    spring.mail.protocol=smtp
    spring.mail.properties.mail.smtp.auth=true
    spring.mail.properties.mail.smtp.starttls.enable=true
    spring.mail.properties.mail.smtp.ssl.enable=true
    spring.mail.default-encoding=UTF-8
    

    以下是我的电子邮件服务课程(不是那么专业,但对于个人项目,它可以满足我的需要)

    @Service
    @AllArgsConstructor
    @Slf4j
    public class EmailService implements EmailSender {
    
        private final JavaMailSender mailSender;
    
        @Override
        @Async
        public void send(String to, String email) {
            try {
                MimeMessage mimeMessage = mailSender.createMimeMessage();
                MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, "utf-8");
                helper.setText(email, true);
                helper.setTo(to);
                helper.setSubject("Your subject");
                helper.setFrom("xxxxxx@gmail.ro");
                mailSender.send(mimeMessage);
            } catch (MessagingException e) {
                log.error("failed to send email", e);
                throw new IllegalStateException("failed to send email");
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-09-26
      • 2017-10-15
      • 1970-01-01
      • 2016-08-31
      • 1970-01-01
      • 2012-01-07
      • 2023-03-19
      相关资源
      最近更新 更多