spiringboot 在window上可以发送邮件,linux服务器: 503 Error: need EHLO and AUTH first !
需要配置
spring.mail.properties.mail.smtp.auth=true
退信 said: 552 from is empty. #ERR_FROM_INVALID (in reply to end of DATA command)
原因:jar包冲突:geronimo-javamail_1.4_spec-1.3.jar与mail.jar 冲突
maven中排除指定包:
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-java2wsdl</artifactId>
<version>1.3</version>
<exclusions>
<exclusion>
<groupId>org.apache.geronimo.specs </groupId>
<artifactId>geronimo-javamail_1.4_spec </artifactId>
</exclusion>
</exclusions>
</dependency>
测试Controller
import java.util.Date;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject;
@RestController
@RequestMapping("/email")
public class EmailController {
@Autowired
public JavaMailSender javaMailSender;// 从容器中拿到 邮件发送对象
// 邮件发送人
@Value("${spring.mail.username}")
private String emailFrom;
@PostMapping("/test")
public String send(@RequestBody JSONObject sendParams) {
if(StringUtils.isBlank(emailFrom) ) {
return "没有发件人";
}
String subject = sendParams.getString("subject");
if(StringUtils.isBlank(subject) ) {
return "没有邮件主题";
}
// 邮件接收人
String to = sendParams.getString("to");
if(StringUtils.isBlank(sendParams.getString("to")) ) {
return "没有收件人";
}
SimpleMailMessage mail = new SimpleMailMessage();
mail.setFrom(emailFrom);
mail.setTo(to.split(","));
//抄送人
String cc = sendParams.getString("cc");
if(StringUtils.isNotBlank(sendParams.getString("to"))){
mail.setCc(cc.split(","));
}
mail.setSubject(subject);
mail.setText("测试发送邮件:"+new Date()); // 邮件内容
javaMailSender.send(mail);
return "邮件发送成功";
}
}
yml配置
# SMTP服务器
spring:
mail:
host: smtp.qq.com
port: 587
#发送邮件的邮箱地址:改成自己的邮箱
username: ***@qq.com
#发送短信后它给你的授权码 填写到这里
password: wclgqwppbcslbjbd
protocol: smtp
#其他参数
properties:
mail:
#配置SSL 加密工厂
smtp:
auth: true
ssl:
#配置ssl
enable: false
required: false
#开启debug模式,这样邮件发送过程的日志会在控制台打印出来,方便排查错误
# debug: true