Spring Boot发送电子邮件API REST

huangapple go评论133阅读模式
英文:

Spring boot send e-mail API REST

问题

我正在尝试创建一个用于发送电子邮件的REST API,但我遇到了以下错误:

无法自动装配找不到'javaMailSender'类型的bean

Mail Model.java

@Data
public class Mail {
    private String name;
    private String email;
    private String subject;
    private String message;
}

这是我的 MailService.java

@Service
public class MailService {
    private JavaMailSender javaMailSender;
    @Autowired
    public MailService(JavaMailSender javaMailSender) {
        this.javaMailSender = javaMailSender;

    }

    public void sendEmail(Mail usermodel) throws MailException {

        StringBuilder sb = new StringBuilder();
        sb.append("Name: ").append(usermodel.getName()).append(System.lineSeparator());
        sb.append("\n Message: ").append(usermodel.getMessage());

        SimpleMailMessage mail = new SimpleMailMessage();

        mail.setTo(usermodel.getEmail());
        mail.setFrom("emailremoved@gmail.com");
        mail.setSubject(usermodel.getMessage());
        mail.setText(sb.toString());

        javaMailSender.send(mail);
    }

}

EmailController.java

@RestController
@RequestMapping("/api/v1/email")
public class EmailController {
    @Autowired
    private MailService mailService;

    //some other code

    @PostMapping()
    public ResponseEntity<Mail> enviarEmail(Mail email){
        try {
            mailService.sendEmail(email);
            return new ResponseEntity<>(email,  HttpStatus.OK);
        } catch( MailException e){
            return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
        }


    }
}

感谢您的回复。

英文:

I'm trying to create an API rest to send email
but i'm facing this error:

**cold not autowire no bean of 'javaMailSender' type found **

Mail Model.java

@Data
public class Mail {
    private String name;
    private String email;
    private String subject;
    private String message;
}

this my MailService.java

@Service
public class MailService {
    private JavaMailSender javaMailSender;
    @Autowired
    public MailService(JavaMailSender javaMailSender) {
        this.javaMailSender = javaMailSender;

    }

    public void sendEmail(Mail usermodel) throws MailException {

        StringBuilder sb = new StringBuilder();
        sb.append(&quot;Name: &quot;).append(usermodel.getName()).append(System.lineSeparator());
        sb.append(&quot;\n Message: &quot;).append(usermodel.getMessage());

        SimpleMailMessage mail = new SimpleMailMessage();

        mail.setTo(usermodel.getEmail());
        mail.setFrom(&quot;emailremoved@gmail.com&quot;);
        mail.setSubject(usermodel.getMessage());
        mail.setText(sb.toString());

        javaMailSender.send(mail);
    }

}

EmailController.java

@RestController
@RequestMapping(&quot;/api/v1/email&quot;)
public class EmailController {
    @Autowired
    private MailService mailService;

    //some other code

    @PostMapping()
    public ResponseEntity&lt;Mail&gt; enviarEmail(Mail email){
        try {
            mailService.sendEmail(email);
            return new ResponseEntity&lt;&gt;(email,  HttpStatus.OK);
        } catch( MailException e){
            return new ResponseEntity&lt;&gt;(null, HttpStatus.INTERNAL_SERVER_ERROR);
        }


    }
}

thank you for your reply

答案1

得分: 0

你缺少javax.mail依赖项。
将以下内容添加到pom.xml中:

<dependency>
  <groupId>javax.mail</groupId>
  <artifactId>mail</artifactId>
  <version>1.4.7</version>
</dependency>
英文:

You are missing javax.mail dependency.
Add this to pom.xml

  &lt;dependency&gt;
    &lt;groupId&gt;javax.mail&lt;/groupId&gt;
    &lt;artifactId&gt;mail&lt;/artifactId&gt;
    &lt;version&gt;1.4.7&lt;/version&gt;
  &lt;/dependency&gt;

答案2

得分: 0

你是否在你的pom.xml中导入了正确的依赖项?

我已经为我的项目导入了这个依赖项,它正在工作:

<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>javax.mail</artifactId>
    <version>1.6.2</version>
</dependency>
英文:

Did you import the right dependency into your pom.xml ?

I've imported this for my project and it's working:

&lt;dependency&gt;
    &lt;groupId&gt;com.sun.mail&lt;/groupId&gt;
    &lt;artifactId&gt;javax.mail&lt;/artifactId&gt;
    &lt;version&gt;1.6.2&lt;/version&gt;
&lt;/dependency&gt;

答案3

得分: 0

请将以下Java邮件API和javax.activation依赖项添加到您的pom.xml文件中:

<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.activation/activation -->
<dependency>
    <groupId>javax.activation</groupId>
    <artifactId>activation</artifactId>
    <version>1.1.1</version>
</dependency>
英文:

try adding this Java mail API and javax.activation dependencies to your pom.xml

&lt;dependency&gt;
    &lt;groupId&gt;javax.mail&lt;/groupId&gt;
    &lt;artifactId&gt;mail&lt;/artifactId&gt;
    &lt;version&gt;1.4.7&lt;/version&gt;
  &lt;/dependency&gt;

&lt;!-- https://mvnrepository.com/artifact/javax.activation/activation --&gt;
&lt;dependency&gt;
    &lt;groupId&gt;javax.activation&lt;/groupId&gt;
    &lt;artifactId&gt;activation&lt;/artifactId&gt;
    &lt;version&gt;1.1.1&lt;/version&gt;
&lt;/dependency&gt;

答案4

得分: 0

I think you don't have configured emailsender.

what you should do is create configuration file with this bean like this:

@Configuration
public class EmailConfig {
    @Bean
    public JavaMailSender javaMailSender() 
    {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost("smtp.gmail.com");
        mailSender.setPort(25);
          
        mailSender.setUsername("admin@gmail.com");
        mailSender.setPassword("password");
          
        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;
    }
}
英文:

I think you don't have configured emailsender.

what you should do is create configuration file with this bean like this:

@Configuration
public class EmailConfig {
    @Bean
    public JavaMailSender javaMailSender() 
    {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost(&quot;smtp.gmail.com&quot;);
        mailSender.setPort(25);
          
        mailSender.setUsername(&quot;admin@gmail.com&quot;);
        mailSender.setPassword(&quot;password&quot;);
          
        Properties props = mailSender.getJavaMailProperties();
        props.put(&quot;mail.transport.protocol&quot;, &quot;smtp&quot;);
        props.put(&quot;mail.smtp.auth&quot;, &quot;true&quot;);
        props.put(&quot;mail.smtp.starttls.enable&quot;, &quot;true&quot;);
        props.put(&quot;mail.debug&quot;, &quot;true&quot;);
          
        return mailSender;
    }
}

</details>



huangapple
  • 本文由 发表于 2020年8月6日 16:10:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63279390.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定