⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 inceptmail.java

📁 理想的邮件收发管理
💻 JAVA
字号:
package zx.mmy;

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import java.io.*;

public class InceptMail {
	//邮件接收处理
	public static void receive(String host, String username, String password) {
		Store store = null;
		Folder folder = null;
		String mail = null;
		String title = null;
		try {
			Properties props = new Properties();
			Session session = Session.getDefaultInstance(props, null);
			store = session.getStore("pop3");
			store.connect(host, username, password);
			folder = store.getFolder("INBOX");
			folder.open(Folder.READ_ONLY);
			Message[] message = folder.getMessages();
			for (int i = 0, n = message.length; i < n; i++) {
				mail = (String) ((InternetAddress) message[i].getFrom()[0])
						.getAddress();
				// System.out.println("邮件地址:" + mail);
				title = (String) message[i].getSubject();
				// System.out.println("邮件标题:" + title);
				// 获取信息对象
				System.out.println("邮箱名称:" + mail);
				System.out.println("邮箱标题:" + title);
				Part messagePart = message[i];
				Object content = messagePart.getContent();// 附件
				if (content instanceof Multipart) {
					messagePart = ((Multipart) content).getBodyPart(0);
				}
				// 获取content类型
				String contentType = messagePart.getContentType();
				// 如果邮件内容是纯文本或者是HTML,那么打印出信息
				System.out.println("发送类型:" + contentType);
				String thisLine = null;
				StringBuffer str = new StringBuffer();
				if (contentType.startsWith("text/plain") || contentType.startsWith("text/html")) {
					InputStream is = messagePart.getInputStream();
					BufferedReader reader = new BufferedReader(new InputStreamReader(is));
					thisLine = reader.readLine();
					while (thisLine != null) {
						str.append(thisLine);
						thisLine = reader.readLine();
					}
					System.out.println("邮箱内容:" + str.toString());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 释放资源
			try {
				if (folder != null)
					folder.close(false);
				if (store != null)
					store.close();
			} catch (Exception ex2) {
				ex2.printStackTrace();
			}
		}
	}

	public static void main(String[] args) {
		receive("pop.163.com", "muchuan69@163.com", "mmy5855342");
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -