导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
邮箱发送工具类
/**
* @author LongFei
* @version V1.0
* @description 邮件发送
*/
public class MailUtil {
//协议 smtp或者pop
private static String protocol = "smtp";
private static String emailUrl = "smtp.qq.com";
private static int emailPort = 25;
//发件人邮箱地址
private static String user = "xxx@qq.com";
//密码或者授权码
private static String password = "xxx";
/**
* 发送邮件
*
* @param emailUrl 邮件服务器地址
* @param emailPort 邮件服务器端口
* @param toEmail 收件人邮箱
* @param title 邮件标题
* @param centent 邮件内容
*
* @throws Exception
*/
public static void sendMail(String emailUrl, int emailPort, String toEmail, String title, String centent) throws Exception {
Properties prop = System.getProperties();
prop.put("mail.host", emailUrl);
prop.put("mail.transport.protocol", protocol);
//这句好像加不加都行 - -
// prop.put("mail.smtp.auth", "true");
Session session = Session.getInstance(prop);
session.setDebug(true);
Transport ts = session.getTransport(protocol);
ts.connect(emailUrl, emailPort, user, password);
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(user));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
message.setSubject(title);
message.setContent(centent, "text/html;charset=utf-8");
System.out.println("正在发送邮件....");
ts.sendMessage(message, message.getAllRecipients());
System.out.println("发送邮件成功!");
ts.close();
}
public static void main(String[] args) throws Exception {
sendMail(emailUrl, emailPort, "xxxr@163.com", "测试发送", "发送内容22222222222222");
}
}
常用邮件服务器地址
sina.com:
POP3服务器地址:pop3.sina.com.cn(端口:110)
SMTP服务器地址:smtp.sina.com.cn(端口:25)
sinaVIP:
POP3服务器:pop3.vip.sina.com (端口:110)
SMTP服务器:smtp.vip.sina.com (端口:25)
sohu.com:
POP3服务器地址:pop3.sohu.com(端口:110)
SMTP服务器地址:smtp.sohu.com(端口:25)
126邮箱:
POP3服务器地址:pop.126.com(端口:110)
SMTP服务器地址:smtp.126.com(端口:25)
139邮箱:
POP3服务器地址:POP.139.com(端口:110)
SMTP服务器地址:SMTP.139.com(端口:25)
163.com:
POP3服务器地址:pop.163.com(端口:110)
SMTP服务器地址:smtp.163.com(端口:25)
QQ邮箱
POP3服务器地址:pop.qq.com(端口:110)
SMTP服务器地址:smtp.qq.com (端口:25)
QQ企业邮箱
POP3服务器地址:pop.exmail.qq.com (SSL启用 端口:995)
SMTP服务器地址:smtp.exmail.qq.com(SSL启用 端口:587/465)
yahoo.com:
POP3服务器地址:pop.mail.yahoo.com
SMTP服务器地址:smtp.mail.yahoo.com
yahoo.com.cn:
POP3服务器地址:pop.mail.yahoo.com.cn(端口:995)
SMTP服务器地址:smtp.mail.yahoo.com.cn(端口:587
HotMail
POP3服务器地址:pop3.live.com (端口:995)
SMTP服务器地址:smtp.live.com (端口:587)
gmail(google.com)
POP3服务器地址:pop.gmail.com(SSL启用 端口:995)
SMTP服务器地址:smtp.gmail.com(SSL启用 端口:587)
263.net:
POP3服务器地址:pop3.263.net(端口:110)
SMTP服务器地址:smtp.263.net(端口:25)
263.net.cn:
POP3服务器地址:pop.263.net.cn(端口:110)
SMTP服务器地址:smtp.263.net.cn(端口:25)
x263.net:
POP3服务器地址:pop.x263.net(端口:110)
SMTP服务器地址:smtp.x263.net(端口:25)
21cn.com:
POP3服务器地址:pop.21cn.com(端口:110)
SMTP服务器地址:smtp.21cn.com(端口:25)
Foxmail:
POP3服务器地址:POP.foxmail.com(端口:110)
SMTP服务器地址:SMTP.foxmail.com(端口:25)
china.com:
POP3服务器地址:pop.china.com(端口:110)
SMTP服务器地址:smtp.china.com(端口:25)
tom.com:
POP3服务器地址:pop.tom.com(端口:110)
SMTP服务器地址:smtp.tom.com(端口:25)
etang.com:
POP3服务器地址:pop.etang.com
SMTP服务器地址:smtp.etang.com
阿里云企业邮箱
Smtp 服务器地址 smtp.mxhichina.com
网友评论