首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >通过gmail发送JAVA Spring邮件

通过gmail发送JAVA Spring邮件
EN

Stack Overflow用户
提问于 2017-04-23 19:26:10
回答 3查看 6K关注 0票数 1

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

下面是Service的外观:

代码语言:javascript
复制
@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();
        }
    }
}

AppProperties

代码语言:javascript
复制
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依赖项。

代码语言:javascript
复制
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

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

EN

回答 3

Stack Overflow用户

发布于 2017-04-24 13:19:06

如果你没有在你的web应用程序中使用SSL证书而不是尝试我的代码,它在spring boot中工作得很好,文本邮件和HTML邮件都工作得很好。

代码语言:javascript
复制
@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();
        }


}

属性文件:

代码语言:javascript
复制
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

依赖性::

代码语言:javascript
复制
 <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>
票数 0
EN

Stack Overflow用户

发布于 2018-12-04 04:02:53

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

代码语言:javascript
复制
spring
  mail:
    host: smtp.gmail.com
    username: youremail@gmail.com
    port: 587
    password: ******
    protocol: smtp
    properties:
      mail:
        smtp:
          starttls:
            enable: true
票数 0
EN

Stack Overflow用户

发布于 2022-01-11 21:37:51

你也可以尝试下面的设置,但我不能给出更多关于这方面的信息,因为这将是一个很长的帖子。然而,GeeksForGeeks确实在spring邮件属性中解释了一切都做了什么,而且谷歌也有自己的指南,你可以在here中找到它

附注:我还禁止从gmail帐户设置访问安全性较低的应用程序

下面是application.properties

代码语言:javascript
复制
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

下面是我的电子邮件服务类(不是很专业,但对于个人项目,它可以做我需要的技巧)

代码语言:javascript
复制
@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");
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43570543

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档