SMTP的全称是“Simple Mail Transfer Protocol”,即简单邮件传输协议。它是一组用于从源地址到目的地址传输邮件的准备规范,通过它来控制邮件的中转方式。SMTP协议属于TCP/IP协议簇,它帮助每台计算机在发送或中转信件时找到下一个目的发送地。SMTP 服务器就是遵循SMTP协议的发送邮件服务器。SMTP认证,简单地说就是要求必须在提供了账户名和密码之后才可以登录SMTP服务器,这就使得那些垃圾邮件的散播者无可乘之机。可增加SMTP认证的目的是为了使用户避免受到垃圾邮件的侵扰。
POP3是Post Office Protocol 3的简称,即邮局协议的第3个版本,它规定怎样将个人计算机连接到Intenet的邮件服务器和下载电子邮件的电子协议。它是因特网电子邮件的第一个离线协议标准,POP3允许用户从服务器上把邮件存储到本地主机(即自己的计算机)上,同时删除保存在邮件服务器上的邮件,而POP3服务器则是遵循POP3协议的接收邮件服务器,用来接收电子邮件的。
IMAP全称是Internet Mail Access Protocol,即交互式邮件存取协议,它是跟POP3类似邮件访问标准协议之一。不同的是,开启了IMAP后,您在电子邮件客户端收取的邮件仍然保留在服务器上,同时在客户端上的操作都会反馈到服务器上,如:删除邮件,标记已读等,服务器上的邮件也会做相应的动作。所以无论从浏览器登录邮箱或者客户端软件登录邮箱,看到的邮件以及状态都是一致的。
邮箱名称 | SMTP服务器 | 端口号 |
QQ邮箱 | smtp.qq.com | 465/587 |
163邮箱 | smtp.163.com | 456 |
GMail邮箱 | smtp.gmail.com | 456/587 |
smtp.example.com
首先进入网易邮箱中的设置->常规设置->POP3/SMTP/IMAP,开启POP3/SMTP服务
在授权密码管理中,新增授权密码,密码只可以只用一次,记得保存密码!
准备好这些工作后,在文件夹下导入所需要用的jar包
package com.apesource.demo;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
//创建Session会话
public class Demo01 {
public static void main(String[] args) {
//邮箱账号信息
String userName = "********@163.com"; //邮箱发送账号
String password = "*****************";//账号授权密码
//SMTP服务器连接信息
Properties props = new Properties();
props.put("mail.smtp.host","smtp.163.com"); //SMTP主机名
props.put("mail.smtp.port", "25"); //主机端口号
props.put("mail.smtp.auth", "true"); //是否需要用户认证
props.put("mail.smtp.starttls.enable", "true"); //启用TLS加密
//创建Session会话
//参数一:smtp服务器连接参数
//参数二 账号和密码的授权认证对象
Session session = Session.getInstance(props,new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
});
System.out.println(session);
//设置debug模式便于调试
session.setDebug(true);
}
}
package com.apesource.demo;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
import javax.sound.midi.MidiChannel;
import com.apesource.util.JavaMailUtils;
//发送普通文本内容的邮件
public class Demo02 {
public static void main(String[] args) {
try {
//1.创建Session会话
Session session = JavaMailUtils.createSession();
//2.创建邮件对象
MimeMessage message = new MimeMessage(session); //构造一个Message对象
message.setSubject("这是一封邮件"); //设置邮件标题
message.setText("今天是2023年7月7号,农历五月二十"); //设置邮件正文
message.setFrom(new InternetAddress("**********@163.com")); //发件方
message.setRecipient(RecipientType.TO, new InternetAddress("*********@qq.com")); //收件方
//3.发送,使用Transport.send()
Transport.send(message);
//设置debug模式便于调试
session.setDebug(true);
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
绝大多数邮件服务器要去发送方地址和登录用户名必须一致,否则发送将失败。
MimeMessage的setRecipients方法设置邮件的收件人,其中
package com.apesource.demo;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
import com.apesource.util.JavaMailUtils;
//抄送多个邮箱地址
//邮件内容包含HTML标签
public class Demo03 {
public static void main(String[] args) {
try {
//创建Session会话
Session session = JavaMailUtils.createSession();
//创建MimeMessage邮件对象
MimeMessage message = new MimeMessage(session);
message.setSubject("测试邮件");
//邮件正文包含html标签
message.setText("今天是阳光明媚的一天,让我们一起拥抱太阳","utf-8","html");
message.setRecipient(RecipientType.TO, new InternetAddress("**************@qq.com"));
message.setRecipients(RecipientType.CC, new InternetAddress[] {new InternetAddress("**********@qq.com"),new InternetAddress("**********@qq.com")});
message.setFrom(new InternetAddress("**********@163.com"));
//发送邮件
Transport.send(message);
//设置debug模式便于调试
session.setDebug(true);
} catch (Exception e) {
}
}
}
package com.apesource.demo;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import javax.activation.DataHandler;
import javax.mail.BodyPart;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
import javax.mail.internet.MimeMultipart;
import javax.mail.util.ByteArrayDataSource;
import com.apesource.util.JavaMailUtils;
import com.sun.mail.handlers.multipart_mixed;
//发送带有"附件"的邮件
public class Demo04 {
public static void main(String[] args) {
try {
//1.创建Session对象
Session session = JavaMailUtils.createSession();
//2.创建MimeMessage邮件对象
MimeMessage message = new MimeMessage(session);
message.setRecipient(RecipientType.TO, new InternetAddress("*********@qq.com"));
message.setRecipients(RecipientType.CC, new InternetAddress[] {new InternetAddress("**********@qq.com"),new InternetAddress("*********@qq.com")});
message.setFrom(new InternetAddress("*********@163.com"));
message.setSubject("鸡汤邮件");
//邮件既包含正文又包含附件
//正文
BodyPart textpart = new MimeBodyPart();
textpart.setContent("用脑子干事算是工作,不用脑子的只能算是动作!","text/html;charset=utf-8"); //防止中文乱码 标签加粗字体
//附件
BodyPart filepart = new MimeBodyPart();
filepart.setFileName("图片名称"); //附件文件显示名称
//上传附件文件
filepart.setDataHandler(
new DataHandler(
new ByteArrayDataSource(
Files.readAllBytes(Paths.get("C:\\Users\\lenovo\\Pictures\\XXX.jpg")),
"application/octet-stream"))); //文件类型
//将正文+附件组装成Multipart对象
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(textpart);
multipart.addBodyPart(filepart);
//将Multipart对象放入邮件
message.setContent(multipart);
Transport.send(message);
//设置debug模式便于调试
session.setDebug(true);
} catch (MessagingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.apesource.demo;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import javax.activation.DataHandler;
import javax.mail.BodyPart;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.util.ByteArrayDataSource;
import javax.mail.internet.MimeMessage.RecipientType;
import com.apesource.util.JavaMailUtils;
//邮件正文嵌套图片
public class Demo05 {
public static void main(String[] args) {
try {
//1.创建Session对象
Session session = JavaMailUtils.createSession();
//2.创建MimeMessage邮件对象
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("*************@163.com")); //发件人
message.setRecipient(RecipientType.TO, new InternetAddress("*************@qq.com")); //收件人
//多人抄送使用new InternetAddress[] {}
message.setRecipients(RecipientType.CC, new InternetAddress[] {new InternetAddress("***************@qq.com"),new InternetAddress("**********@qq.com")});
message.setSubject("邮件标题"); //邮件标题
//邮件正文
BodyPart textpart = new MimeBodyPart();
StringBuilder contentText = new StringBuilder();
contentText.append("图片
");
contentText.append("这是一张图片
");
contentText.append("
");
textpart.setContent(contentText.toString(),"text/html;charset=utf-8");
//邮件附件
BodyPart imagepart = new MimeBodyPart();
imagepart.setDataHandler(
new DataHandler(
new ByteArrayDataSource(
Files.readAllBytes(Paths.get("C:\\Users\\lenovo\\Pictures\\XXX.jpg")),
"application/octet-stream")));
//设置当前image为内嵌图片
//这个ID和HTML中引用的ID对应起来,邮件客户端就可以正常显示内嵌图片
imagepart.setHeader("Content-ID", "gd"); //图片的内容ID
//邮件正文+附件
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(textpart);
multipart.addBodyPart(imagepart);
message.setContent(multipart);
Transport.send(message);
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}