SpringBoot 简单邮件及异常排查

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值