📄 message.java
字号:
package com.cwq.batchmail.mail;import java.util.*;/** * A Message is an email according to RFC 2822. * * @author Kai Blankenhorn */public class Message implements java.io.Serializable { protected boolean partial = false; protected boolean deletable = false; protected boolean read = false; protected String account; protected String messageID; protected String[] header; protected String[] body; protected String from = ""; protected String to = ""; protected String subject = ""; protected String date = ""; protected String CC = ""; protected String BCC = ""; protected int size = 0; protected String boundary = null; public Message(String _from, String _to, String _contentType, String _subject, String _body, String _cc) { String[] strings = null; if(_cc != null && _cc.trim().length() > 0) { strings = new String[] { "From: ".concat(_from), "To: ".concat(_to), "CC: ".concat(_cc), "Subject: ".concat(_subject), "Date: ".concat(new java.util.Date().toString()), "Content-Type: ".concat(_contentType), "", _body, "", }; } else { strings = new String[] { "From: ".concat(_from), "To: ".concat(_to), "Subject: ".concat(_subject), "Date: ".concat(new java.util.Date().toString()), "Content-Type: ".concat(_contentType), "", _body, "" }; } Vector header = new Vector(); Vector body = new Vector(); String[] conv = new String[0]; boolean bodyLine = false; for(int i = 0; i < strings.length; i++) { if(!bodyLine && strings[i].equals("")) { bodyLine = true; } else { if(bodyLine) { body.add(strings[i]); } else { header.add(strings[i]); } } } Vector unfoldedHeader = new Vector(); for(int i = 0; i < header.size(); i++) { if(((String) header.get(i)).matches("^\\s.+") && i > 0) { unfoldedHeader.setElementAt((String) unfoldedHeader.get(unfoldedHeader.size() - 1) + " " + ((String) header.get(i)).trim(), unfoldedHeader.size() - 1); } else { unfoldedHeader.add(header.get(i)); } } this.setHeader((String[]) unfoldedHeader.toArray(conv)); this.setBody((String[]) body.toArray(conv)); for(int i = 0; i < this.header.length; i++) { String line = this.header[i]; if(line.toUpperCase().startsWith("TO:")) { setTo(line.substring(4)); } if(line.toUpperCase().startsWith("FROM:")) { setFrom(line.substring(6)); } if(line.toUpperCase().startsWith("SUBJECT:")) { setSubject(line.substring(9)); } if(line.toUpperCase().startsWith("DATE:")) { setDate(line.substring(6)); } if(line.toUpperCase().startsWith("CC:")) { setCC(line.substring(4)); } if(line.toUpperCase().startsWith("MESSAGE-ID:")) { String tmp = ""; if(line.indexOf("<") > 11 && line.indexOf(">") == line.length() - 1) { tmp = line.substring(line.indexOf("<") + 1, line.indexOf(">")); } else { tmp = line.substring(11).trim(); } setMessageID(tmp); } } } public String getHeaderFieldByName(String fieldName) { for(int i = 0; i < header.length; i++) { if(header[i].startsWith(fieldName + ":")) { return header[i].substring(fieldName.length() + 1).trim(); } } return null; } public String getFrom() { return from; } public void setFrom(String from) { this.from = from; } public String getTo() { return to; } public void setTo(String to) { this.to = to; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getMessageID() { return messageID; } public void setMessageID(String messageID) { this.messageID = messageID; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public String getBoundary() { for(int i = 0; i < header.length; i++) { if(header[i].trim().startsWith("boundary=\"")) { this.boundary = header[i].substring(header[i].indexOf('"') + 1, header[i].lastIndexOf('"')); } } return boundary; } public String[] getHeader() { return header; } public void setHeader(String[] header) { this.header = header; } public String[] getBody() { return body; } public void setBody(String[] body) { this.body = body; } public boolean hasBeenClassified() { return false; } public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public int getSize() { return this.calculateSize(); } public void setSize(int size) { this.size = size; } private int calculateSize() { int size = 0; int i = 0; if(this.size > 0) { size = this.size; } else { if(header == null && body == null) { size = this.size; } else { for(i = 0; i < body.length; i++) { size += body[i].length() + 2; } size += 2; // empty line between header and body for(i = 0; i < header.length; i++) { size += header[i].length() + 2; } } } return size; } public void setDeletable(boolean delete) { this.deletable = delete; } public boolean getDeletable() { return deletable; } public void setPartialDownload(boolean b) { partial = b; } public boolean isPartialDownload() { return partial; } public boolean isRead() { return read; } public void setRead(boolean read) { this.read = read; } public String getCC() { return CC; } public void setCC(String CC) { this.CC = CC; } public String getBCC() { return BCC; } public void setBCC(String BCC) { this.BCC = BCC; } public String[] getSource() { String[] src = new String[header.length + body.length]; for(int i = 0; i < header.length; i++) { src[i] = header[i]; } for(int i = 0; i < body.length; i++) { src[header.length + i] = body[i]; } return src; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -