最近在做邮件发送的功能,发现163邮箱老是无法发送成功,报端口错误,所以把下面的配置记录下来,以供大家参考。
163 邮箱配置流程:
1. 邮箱进入163邮箱,然后开启“客户端授权码”,记住你设置的密码,一会配置要用。如图:
2. 在邮箱界面,开启POP3/SMTP配置。
3. 在配置文件里面设置如下参数:
spring.mail.host=smtp.163.com
spring.mail.username=XXXXX@163.com
spring.mail.password=XXXXXXX(授权码)
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
-----------------------------163 邮箱额外的配置------------------------------------------
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.smtp.socketFactory.fallback=false
spring.mail.properties.mail.smtp.port=465
spring.mail.properties.mail.smtp.socketFactory.port=465
----------------------------------End--------------------------------------------------------------------
4. 发送邮件的JAVA 部分代码:
Properties props = new Properties();
Session mailSession = Session.getInstance(props);
MimeMessage message = new MimeMessage(mailSession);
message.setFrom(new InternetAddress(env.getProperty("spring.mail.username")));
message.setRecipient(RecipientType.TO, new InternetAddress("xxxx@xxxx.com"));
message.setSubject("Mobile Services Testing");
message.setContent("This is a test email from mobile services.", "text/html;charset=UTF-8");
mailSender.send(message);
到此163邮箱配置完成, QQ邮箱和GMAIL的基本相同,更新一下授权码和端口就可以了。