类型 | 服务器名称 | 服务器地址(163为例) | SSL协议端口 | 非SSL协议端口号 | TSL协议端口 |
收件服务器 | POP | pop.163.com | 995 | 110 | |
收件服务器 | IMAP | imap.163.com | 993 | 143 | |
发件服务器 | SMTP | smtp.163.com | 465/994 | 25 | 587 |
腾讯企业邮箱服务器地址:smtp.exmail.qq.com
腾讯邮箱服务器地址:smtp.qq.com
常见问题:
1、如果出现454 Command not permitted when TLS active
错误,请检查你的邮件端口配置的是不是25端口,如果是,请改成465即可解决问题。
2、部分邮件提供商smtp登录密码不是账号密码,而是授权码,务必注意。使用SSL、TLS这个一定要注意。
3、550 用户被锁定:普通 163 邮箱是无法通过 smtp.163.com 发送邮件的,只有 163 VIP 邮箱才行,然后设置 mail.smtp.host=smtp.vip.163.com
4、454 Command not permitted when TLS active:需要设置 mail.smtp.starttls.enable=false
5、553 authentication is required:需要设置 mail.smtp.auth=true
6、550 Invalid User:from 必须写成带 @ 的邮件格式,且 username 要用 @ 前面的
SSL、TSL 发送邮件DEMO
package com.xx.util;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMailTLS {
public static void main(String[] args) {
final String username = "xxx@qq.com";
final String password = "xxxxxxxxxxxxxx";
Properties props = new Properties();
props.put("mail.smtp.auth", true);
// TLS 需要这两行
props.put("mail.smtp.starttls.enable", true);
props.put("mail.smtp.port", "587");
// SSL 需要这三行
// props.put("mail.smtp.ssl.enable", true);
// props.put("mail.smtp.port", "465");
// props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
// props.put("mail.smtp.host", "smtp.163.com");
props.put("mail.smtp.host", "smtp.qq.com");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("202888534@qq.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("fenghongwang@goclouds.cn"));
message.setSubject("Testing Subject1");
message.setText("Dear Mail Crawler,"
+ "\n\n No spam to my email, please!");
Transport.send(message);
System.out.println("Done");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}