1.JavaMail的关键对象
Properties:属性对象
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.sina.com.cn");
props.put("mail.smtp.auth", "true");
针对不同的的邮件协议,JavaMail规定了服务提供者必须支持一系列属性,下表是针对SMTP协议的一些常见属性(属性值都以String类型进行设置,属性类型栏仅表示属性是如何被解析的):
属性名 | 属性类型 | 说明 |
---|---|---|
mail.stmp.host | String | SMTP服务器地址,如smtp.sina.com.cn |
mail.stmp.port | int | SMTP服务器端口号,默认为25 |
mail.stmp.auth | boolean | SMTP服务器是否需要用户认证,默认为false |
mail.stmp.user | String | SMTP默认的登陆用户名 |
mail.stmp.from | String | 默认的邮件发送源地址 |
mail.stmp.socketFactory.class | String | socket工厂类类名,通过设置该属性可以覆盖提供者默认的实现,必须实现javax.net.SocketFactory接口 |
mail.stmp.socketFactory.port | int | 指定socket工厂类所用的端口号,如果没有规定,则使用默认的端口号 |
mail.smtp.socketFactory.fallback | boolean | 设置为true时,当使用指定的socket类创建socket失败后,将使用java.net.Socket创建socket,默认为true |
mail.stmp.timeout | int | I/O连接超时时间,单位为毫秒,默认为永不超时 |
2. 读取邮件
import java.io.FileOutputStream;
import java.util.Date;
import java.util.Enumeration;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeUtility;public class EmailReader {public static void main(String[] args) {// 邮箱配置String host = "pop.163.com";String username = "111@163.com";String password = "21212";// 创建连接属性Properties props = new Properties();props.put("mail.pop3.host", host);// 创建会话Session session = Session.getDefaultInstance(props,new javax.mail.Authenticator() {protected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(username, password);}});try {// 取得pop3协议的邮件服务器Store store = session.getStore("pop3");// 连接pop.qq.com邮件服务器store.connect(host, username, password);// 打开收件箱//返回文件夹对象Folder folder = store.getFolder("INBOX");//设置仅读folder.open(Folder.READ_ONLY);// 读取邮件Message[] messages = folder.getMessages();for (int i = 0; i < messages.length; i++) {Message message = messages[i];// 处理邮件,例如保存为文件
// saveAsEmlFile(message, username+"_" + i + ".eml");String from = InternetAddress.toString( messages[i].getFrom());System.out.println("from = " + from);String s = mimeDecodeString(from);System.out.println("s = " + s);String replyTo = InternetAddress.toString( messages[i].getReplyTo());System.out.println("replyTo = " + replyTo);String to = InternetAddress.toString( messages[i].getRecipients(Message.RecipientType.TO));System.out.println("to = " + to);String subject = messages[i].getSubject();System.out.println("subject = " + subject);Date sentDate = messages[i].getSentDate();System.out.println("sentDate = " + sentDate);Date receDate = messages[i].getReceivedDate();System.out.println("receDate = " + receDate);Enumeration headers = messages[i].getAllHeaders();System.out.println("headers = " + headers);}// 要关闭与 folder 和 store 的连接// 关闭收件箱和邮件存储folder.close(false);store.close();} catch (Exception e) {e.printStackTrace();}}/*** 乱码处理* @param res* @return*/public static String mimeDecodeString(String res) {if(res != null) {res = res.trim();try {if (res.startsWith("=?GB") || res.startsWith("=?gb")|| res.startsWith("=?UTF") || res.startsWith("=?utf")) {res = MimeUtility.decodeText(res);}} catch (Exception e) {System.out.println("乱码处理出错==>" + res+ "<=====>"+e);}return res;}return null;}private static void saveAsEmlFile(Message message, String filename) throws Exception {// 创建邮件文件FileOutputStream outputStream = new FileOutputStream(filename);message.writeTo(outputStream);outputStream.close();}
}