开通邮件服务
使用Java代码发送邮件,首先要开通邮件的服务,以126网易邮箱为例:进入设置页面
点击SMTP和POP3
开启服务,开启之后会有一个唯一的验证码,发送邮件的时候用来识别的。
Java代码调用
使用javax.mail发送邮件信息配置服务器的基本信息
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.126.com"); // SMTP服务器地址
props.put("mail.smtp.port", "25"); // 非加密SMTP端口
props.put("mail.smtp.auth", "true"); // 需要认证
props.put("mail.smtp.ssl.enable", "false"); //关闭端口
分析
Properties properties = new Properties();
是 Java 中用于创建一个 Properties
对象的语句。Properties
是 java.util
包中的一个类,它是一个扩展自 Hashtable
的集合类,主要用于保存键值对(key-value pair),并且这些键值对通常用于存储配置信息(例如应用程序的配置文件)。
建立会话,发送者和对应的code
String from = "a3153947655@126.com";
String code = "ZQvckmf68qHb"; //替换成自己的
Session session = Session.getInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from,code);
}
});
建立与SMTP服务器建立会话。
创建MimeMessage对象,设置from 发送方 ,recipients 接收方
setSubject 发送的主题
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
message.setSubject("测试邮件","utf-8");
设置邮件的主体内容
Multipart multipart=new MimeMultipart(); //可以携带多个消息
MimeBodyPart textPart=new MimeBodyPart();
textPart.setText("这是测试邮件","utf-8");
multipart.addBodyPart(textPart);
message.setContent(multipart);
// 发送邮件信息
Transport.send(message);
完整的发送邮件代码:
public class test {
public static void main(String[] args) throws MessagingException, UnsupportedEncodingException {
Properties properties = new Properties();
properties.put("mail.smtp.host", "smtp.126.com");
properties.put("mail.smtp.port", 25);
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.ssl.enable", "false");
String from = "a3153947655@126.com";
String code = "NMunbf3XYdxt5upf";
String to = "3153947655@qq.com";
Session session = Session.getInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from, code);
}
});
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
message.setSubject("测试邮件","utf-8");
Multipart multipart=new MimeMultipart();
MimeBodyPart textPart=new MimeBodyPart();
textPart.setText("这是测试邮件","utf-8");
multipart.addBodyPart(textPart);
MimeBodyPart textPart2=new MimeBodyPart();
textPart2.setDataHandler(new DataHandler(new FileDataSource("E:\\NJITZX大三上\\学校课程\\TCPIP\\tcpcommunication\\cmmtcp\\src\\CapturePacket.java")));
textPart2.setFileName(MimeUtility.encodeText("CapturePacket.java"));
multipart.addBodyPart(textPart2);
message.setContent(multipart);
// 发送邮件
Transport.send(message);
}
}
邮件发送成功
携带附件发送
设置DataHandler 传入一个FileDataSource,后面是文件地址
还需要传入文件名称
MimeBodyPart textPart2=new MimeBodyPart();
textPart2.setDataHandler(new DataHandler(new FileDataSource("E:\\NJITZX大三上\\学校课程\\TCPIP\\tcpcommunication\\cmmtcp\\src\\CapturePacket.java")));
textPart2.setFileName(MimeUtility.encodeText("CapturePacket.java"));
multipart.addBodyPart(textPart2);
邮件下载
public static void main(String[] args) throws MessagingException, IOException {
String username = "@126.com"; // 收件箱的邮箱地址
String code = ""; // 你的邮箱pop3和SMTP的验证码
// 创建邮箱连接的属性
Properties properties = new Properties();
properties.put("mail.pop3.host", "pop.126.com");
properties.put("mail.pop3.port", "110"); // POP3 默认端口是 110
properties.put("mail.pop3.ssl.enable", "false");
// 获取会话对象
Session session = Session.getInstance(properties); //建立会话信息
// 连接到 POP3 邮件服务器
Store store = session.getStore("pop3");
store.connect(username, code);
// 获取邮箱的收件箱
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
// 获取邮件数量
int messageCount = folder.getMessageCount();
System.out.println("邮件数量: " + messageCount);
// 获取所有邮件
Message[] messages = folder.getMessages();
// 遍历邮件并显示主题和发送者
for (Message message : messages) {
MimeMessage mimeMessage = (MimeMessage) message;
System.out.println("邮件主题: " + mimeMessage.getSubject());
System.out.println("邮件发送者: " + mimeMessage.getFrom()[0]);
System.out.println("邮件内容: ");
Object content = mimeMessage.getContent();
// 输出邮件正文(纯文本内容)
if (content instanceof String) {
System.out.println(content);
} else {
System.out.println("邮件包含附件或 HTML 格式内容");
}
// 标记邮件为已读(可选)
mimeMessage.setFlag(Flags.Flag.SEEN, true); // 标记邮件为已读
}
// 关闭文件夹和连接
folder.close(false); // 不删除邮件,保持在服务器上
store.close(); // 关闭连接
}