前言

  • spring boot 2.0.0.RELEASE
  • spring 5.0.4.RELEASE
  • 网易企业邮箱 2019年版
  • jdk 1.8

发邮件的说明

按照加密方式方式,可以分为:

  • 明文
  • TLS加密
  • SSL加密

发送明文

发送明文邮件时,需要设置好:

  • 协议(默认是smtp,可不用设置)
  • host
  • 端口(默认是25)
  • 用户名
  • 密码
  • 开启身份验证(mail.smtp.auth=true)

网易企业邮箱 不再支持明文邮件了。使用此种方式调用时,会获得错误:535 5.7.0 ERR.LOGIN.NONSSL

JavaMailSenderImpl发送邮件,示例代码如下:

public class TestSendMail1 {
	private static Logger log = LoggerFactory.getLogger(TestSendMail1.class);
	
	public static void main(String[] args) throws Exception {
		JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
		mailSender.setHost("smtphz.qiye.163.com");
		mailSender.setPort(25);
		mailSender.setUsername("xxx@mydomain.com");
		mailSender.setPassword("123456");
		
		Properties javaMailProperties = mailSender.getJavaMailProperties();
		javaMailProperties.setProperty("mail.smtp.auth", "true"); 
		javaMailProperties.setProperty("mail.debug", "true");//启用调试
    	javaMailProperties.setProperty("mail.smtp.timeout", "3000"); //设置超时时间3秒
		mailSender.setJavaMailProperties(javaMailProperties);
		
		try{
			MimeMessage mailMessage = mailSender.createMimeMessage();
			MimeMessageHelper helper = new MimeMessageHelper(mailMessage, true, "UTF-8");
			helper.setFrom("xxx@mydomain.com");
			helper.setTo("xxx@mydomain.com");
			helper.setSubject("测试邮件:您有一个新的未处理消息(使用明文发送)");
			helper.setText(String.format("hi,%s: <br/><br/><br/>很高兴认识你!", "张三"), true);
			
            mailSender.send(mailMessage);
        }catch (Exception e) {
            log.error("发送邮件失败", e);
        }
	}

}

springboot mail插件发送邮件,配置如下(示例代码在后面):

spring.mail.host=smtphz.qiye.163.com
spring.mail.username=xxx@mydomain.com
spring.mail.password=123456
spring.mail.port=25
spring.mail.protocol=smtp
spring.mail.default-encoding=UTF-8
spring.mail.properties.mail.smtp.timeout=3000
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.debug=true
spring.mail.properties.mail.smtp.ssl.enable=false
spring.mail.properties.mail.imap.ssl.socketFactory.fallback=false
spring.mail.properties.mail.smtp.ssl.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.smtp.starttls.enable=false
spring.mail.properties.mail.smtp.starttls.required=false

TLS加密

发送明文邮件时,需要设置好:

  • 协议(默认是smtp,可不用设置)
  • host
  • 端口(默认情况下,TLS加密使用25端口)
  • 用户名
  • 密码
  • 开启身份验证(mail.smtp.auth=true)
  • 支持TLS加密(mail.smtp.starttls.required = true)

TLS加密使用25端口 ! TLS加密使用25端口 ! TLS加密使用25端口!

JavaMailSenderImpl发送邮件,示例代码如下:

public class TestSendMail2 {
	private static Logger log = LoggerFactory.getLogger(TestSendMail2.class);
	
	public static void main(String[] args) throws Exception {
		JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
		mailSender.setHost("smtphz.qiye.163.com");
		mailSender.setPort(25);
		mailSender.setUsername("xxx@mydomain.com");
		mailSender.setPassword("123456");
		
		Properties javaMailProperties = mailSender.getJavaMailProperties();
		javaMailProperties.setProperty("mail.smtp.auth", "true"); 
		javaMailProperties.setProperty("mail.debug", "true");//启用调试
    	javaMailProperties.setProperty("mail.smtp.timeout", "3000"); //设置超时时间3秒
    	javaMailProperties.setProperty("mail.smtp.starttls.enable", "true");
    	javaMailProperties.setProperty("mail.smtp.starttls.required", "true");
    	javaMailProperties.setProperty("mail.smtp.ssl.enable", "false");
    	javaMailProperties.setProperty("mail.imap.ssl.socketFactory.fallback", "false");
    	javaMailProperties.setProperty("mail.smtp.ssl.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		mailSender.setJavaMailProperties(javaMailProperties);
		
		try{
			MimeMessage mailMessage = mailSender.createMimeMessage();
			MimeMessageHelper helper = new MimeMessageHelper(mailMessage, true, "UTF-8");
			helper.setFrom("xxx@mydomain.com");
			helper.setTo("xxx@mydomain.com");
			helper.setSubject("测试邮件:您有一个新的未处理消息(使用TLS加密发送)");
			helper.setText(String.format("hi,%s: <br/><br/><br/>很高兴认识你!", "张三"), true);
			
            mailSender.send(mailMessage);
        }catch (Exception e) {
            log.error("发送邮件失败", e);
        }
	}

}

springboot mail插件发送邮件,配置如下(示例代码在后面):

spring.mail.host=smtphz.qiye.163.com
spring.mail.username=xxx@mydomain.com
spring.mail.password=123456
spring.mail.port=25
spring.mail.protocol=smtp
spring.mail.default-encoding=UTF-8
spring.mail.properties.mail.smtp.timeout=3000
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.debug=true
spring.mail.properties.mail.smtp.ssl.enable=false
spring.mail.properties.mail.imap.ssl.socketFactory.fallback=false
spring.mail.properties.mail.smtp.ssl.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.smtp.starttls.enable=false
spring.mail.properties.mail.smtp.starttls.required=true

SSL加密

发送明文邮件时,需要设置好:

  • 协议(默认是smtp,可不用设置)
  • host
  • 端口(默认情况下,SSL加密使用456端口)
  • 用户名
  • 密码
  • 开启身份验证(mail.smtp.auth=true)
  • 支持SSL加密(mail.smtp.ssl.enable = true)

网易企业邮箱 SSL 端口994

JavaMailSenderImpl发送邮件,示例代码如下:

public class TestSendMail3 {
	private static Logger log = LoggerFactory.getLogger(TestSendMail3.class);
	
	public static void main(String[] args) throws Exception {
		JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
		mailSender.setHost("smtphz.qiye.163.com");
		mailSender.setPort(994);
		mailSender.setUsername("xxx@mydomain.com");
		mailSender.setPassword("123456");
		
		Properties javaMailProperties = mailSender.getJavaMailProperties();
		javaMailProperties.setProperty("mail.smtp.auth", "true"); 
		javaMailProperties.setProperty("mail.debug", "true");//启用调试
    	javaMailProperties.setProperty("mail.smtp.timeout", "3000"); //设置超时时间3秒
    	javaMailProperties.setProperty("mail.smtp.starttls.enable", "false");
    	javaMailProperties.setProperty("mail.smtp.starttls.required", "false");
    	javaMailProperties.setProperty("mail.smtp.ssl.enable", "true");
    	javaMailProperties.setProperty("mail.imap.ssl.socketFactory.fallback", "false");
    	javaMailProperties.setProperty("mail.smtp.ssl.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		mailSender.setJavaMailProperties(javaMailProperties);
		
		try{
			MimeMessage mailMessage = mailSender.createMimeMessage();
			MimeMessageHelper helper = new MimeMessageHelper(mailMessage, true, "UTF-8");
			helper.setFrom("xxx@mydomain.com");
			helper.setTo("xxx@mydomain.com");
			helper.setSubject("测试邮件:您有一个新的未处理消息(使用SSL加密发送)");
			helper.setText(String.format("hi,%s: <br/><br/><br/>很高兴认识你!", "张三"), true);
			
            mailSender.send(mailMessage);
        }catch (Exception e) {
            log.error("发送邮件失败", e);
        }
	}

}

springboot mail插件发送邮件,SSL加密,配置如下(示例代码在后面):

spring.mail.host=smtphz.qiye.163.com
spring.mail.username=xxx@mydomain.com
spring.mail.password=123456
spring.mail.port=25
spring.mail.protocol=smtp
spring.mail.default-encoding=UTF-8
spring.mail.properties.mail.smtp.timeout=3000
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.debug=true
spring.mail.properties.mail.smtp.ssl.enable=true
spring.mail.properties.mail.imap.ssl.socketFactory.fallback=false
spring.mail.properties.mail.smtp.ssl.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.smtp.starttls.enable=false
spring.mail.properties.mail.smtp.starttls.required=false

springboot mail插件发送邮件 示例

@RunWith(SpringRunner.class)
@SpringBootTest(classes=Application.class)
@TestPropertySource("classpath:application-TestSendMail3.properties")
public class TestSendMail4 {
	private Logger log = LoggerFactory.getLogger(TestSendMail4.class);
	
	@Autowired
	private JavaMailSenderImpl mailSender;
	
	@Test
    public void test01() {
		try{
			MimeMessage mailMessage = mailSender.createMimeMessage();
			MimeMessageHelper helper = new MimeMessageHelper(mailMessage, true, "UTF-8");
			helper.setFrom("xxx@mydomain.com");
			helper.setTo("xxx@mydomain.com");
			helper.setSubject("测试邮件:您有一个新的未处理消息(使用 spring boot 发送)");
			helper.setText(String.format("hi,%s: <br/><br/><br/>很高兴认识你!", "张三"), true);
			
            mailSender.send(mailMessage);
        }catch (Exception e) {
            log.error("发送邮件失败", e);
        }
    }

}

application-TestSendMail3.properties

spring.mail.host=smtphz.qiye.163.com
spring.mail.username=xxx@mydomain.com
spring.mail.password=123456
spring.mail.port=25
spring.mail.protocol=smtp
spring.mail.default-encoding=UTF-8
spring.mail.properties.mail.smtp.timeout=3000
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.debug=true
spring.mail.properties.mail.smtp.ssl.enable=false
spring.mail.properties.mail.imap.ssl.socketFactory.fallback=false
spring.mail.properties.mail.smtp.ssl.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.smtp.starttls.enable=false
spring.mail.properties.mail.smtp.starttls.required=true

其它

网易企业邮箱从这里 查协议、host、端口

https://qiye.163.com/help/client.html?device=pc

网易企业邮箱错误码: 420 ERR.LOGIN.DOMAINNOTEXIST 错误

查到的host不对。按照前面的说明查host。

网易企业邮其它错误码

"ERR.LOGIN.USERORPASSNULL":"参数错误:用户名或者密码为空",
"ERR.LOGIN.DOMAINNULL":"参数错误:未指定的域邮箱参数",
"ERR.LOGIN.PASSERR":"用户名和密码不匹配",
"ERR.LOGIN.ILLEGALACCOUNT":"该帐号属于群发名单或者别名,不允许登录",
"ERR.LOGIN.USERNOTEXIST":"该帐号不存在,请你确认域名和帐号",
"ERR.LOGIN.SUPERADMINDOMAINERR":"该超级管理员并未申请任何域名邮箱",
"ERR.LOGIN.SYSTEMBUSY":"登录系统繁忙,请你稍后再试",
"ERR.LOGIN.IPDENY":"登录失败,你的IP在黑名单中,请你联系客服",
"ERR.LOGIN.USRSTATUS1":"该帐号已被禁用,请联系管理员",
"ERR.LOGIN.USRSTATUS2":"帐号已过期,请联系管理员",
"ERR.LOGIN.USRSTATUS5":"该帐号已被管理员删除,请联系管理员",
"SUC.LOGIN.USRUNLOCK":"该帐号已解除冻结状态,请你再次输入用户和密码继续",
"ERR.LOGIN.DOMAINEXPED":"该域名邮箱已经过期了",
"ERR.LOGIN.DOMAINNOTREGISTER":"该域名尚未通过,请以超级管理员登录",
"ERR.LOGIN.DOMAINSTATUS1":"该域名邮箱已被禁用,请你联系客服",
"ERR.SESSIONNULL":"会话已经失效,请你重新登录操作",
"ERR.SYSTEM":"系统繁忙,请你稍后再试",
"ERR.ADMINREQUIRE":"非法权限,该帐号无法执行这个操作",
"ERR.PARAMNULL":"系统发生错误:参数为空",
"ERR.ERR.PARAMTOOLONG":"参数过长",
"ERR.ILLEGAL":"系统非法操作"。