英文:
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("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);
}
}
}
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
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
答案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:
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
答案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
<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>
答案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("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;
}
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论