我想在处理器之后发送邮件。这些是我的步骤配置:
public Step remindByMail() {
return stepBuilderFactory
.get("remindByMail")
.<Customer, SimpleMailMessage> chunk(1)
.reader(getCustomerList())
.processor(new MailProcessor())
.writer(new SendMail())
.build();
}
public class MailProcessor implements ItemProcessor<Customer, SimpleMailMessage> {
private String from = "aaa@oku.com";
@Override
public SimpleMailMessage process(Customer customer) throws Exception {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from);
message.setTo("duyetpt@oku.com");
message.setSubject("Welcome " + customer.getUsername());
message.setText(customer.getInfo());
return message;
}
}
public class SendMail implements ItemWriter<SimpleMailMessage> {
@Override
public void write(List<? extends SimpleMailMessage> messages) throws Exception {
messages.stream().forEach((message)->mailSender.send(message));
}
}
我已经在application.properties
文件中设置了这些属性,
spring.mail.default-encoding=UTF-8
spring.mail.protocol=smtp
spring.mail.host=195.179.79.52
spring.mail.port=25
spring.mail.username=admin
spring.mail.password=admin
spring.mail.properties.mail.smtp.auth=true
我的pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-task</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.oracle.ojdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>19.3.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
但是,我得到了错误:
org.springframework.mail.MailAuthenticationException: Authentication failed; nested exception is javax.mail.AuthenticationFailedException: No authentication mechanisms supported by both server and client
at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:440)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:323)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:312)
.....
Caused by: javax.mail.AuthenticationFailedException: No authentication mechanisms supported by both server and client
at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:880)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:780)
.....
我应该怎么做才能使用Spring Batch发送邮件?
答案 0 :(得分:1)
我的错误消息是我的猜测-服务器和客户端均不支持身份验证机制,因此需要从配置中删除以下属性。
spring.mail.username=admin
spring.mail.password=admin
spring.mail.properties.mail.smtp.auth=true
在Spring Boot + Batch中,我使用没有这些属性的组织服务器发送电子邮件,因为不需要用户名和密码,因为程序必须驻留在组织网络中,这是唯一的安全措施。
如果有人尝试使用它,那就完全不同了-gmail,yahoo等。