Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Email 通过gmail发送JAVA Spring邮件_Email_Spring Boot_Gmail - Fatal编程技术网

Email 通过gmail发送JAVA Spring邮件

Email 通过gmail发送JAVA Spring邮件,email,spring-boot,gmail,Email,Spring Boot,Gmail,我想使用gmail smtp服务器和spring mail sender发送电子邮件,它不会引发任何异常,但我的邮件没有发送,我没有收到 以下是服务的外观: @Service public class MailSenderService { @Autowired public MailSender mailSender; public void prepareAndSend(String recipient, String message) { try

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

以下是服务的外观:

@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

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>

org.springframework.boot
春季启动邮件

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

如果您在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>

org.springframework.boot
春季启动邮件

如果您在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>

org.springframework.boot
春季启动邮件
application.yml示例(使用Spring Boot 2.1.1进行测试):

application.yml示例(使用弹簧靴2.1.1进行测试):


您是否已将发件人电子邮件帐户配置为?是否已检查此项。可能会有帮助。那么,除了应该使用错误记录器而不是使用printStackTrace()方法之外,您是否进行了一些调试?如果没有,您如何声明一切正常?您是否将发件人电子邮件帐户配置为?您是否检查了此项。可能会有帮助。那么,除了应该使用错误记录器而不是使用printStackTrace()方法之外,您是否进行了一些调试?如果您没有,您如何陈述一切顺利?虽然此代码可以回答此问题,但提供有关此代码为什么和/或如何回答此问题的附加上下文可以提高其长期价值。虽然此代码可以回答此问题,提供关于此代码为什么和/或如何回答此问题的附加上下文可提高其长期价值。