75c10f3358b969df4c81153ac92896b580e11f04.svn-base
9.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package com.espeed.yxy.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import javax.crypto.CipherInputStream;
import javax.mail.BodyPart;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import com.espeed.yxy.tool.Encrypt;
import sun.misc.BASE64Decoder;
/**
* 程序名称: EspeedMail_时速邮箱
* 程序版本: V1.0
* 作 者: 深圳市科飞时速网络技术有限公司(0755-88843776)
* 版权所有: 深圳市科飞时速网络技术有限公司
* 技术支持: Tech@21gmail.com
* 单元名称: 解析邮件(营销游)
* 开始时间: 2013.12.09
* 程 序 员: 谢勇
* 最后修改:
* 备 注: 如需修改请通知程序员
*/
public class AnalysisMail {
private MimeMessage msg;
private StringBuffer bodyText=new StringBuffer();
public String attrname="";
/**解析邮件*/
public List ParseEmlMethod(String path,String key){
//定义结果集
List<String> result=new ArrayList<String>();
try {
//System.setProperty("mail.mime.multipart.ignoreexistingboundaryparameter", "true");
if(key!=null&&!key.equals("")){
//解密邮件
BASE64Decoder enc=new BASE64Decoder();
//需要一个流 给它解密的流
CipherInputStream CStream = null;
CStream =(CipherInputStream) Encrypt.decrypt(path,Encrypt.toKey(enc.decodeBuffer(key)));
Session mailSession = Session.getDefaultInstance(System.getProperties(), null);
msg = new MimeMessage(mailSession, CStream);
}else{
InputStream is =new FileInputStream(path);
Session mailSession = Session.getDefaultInstance(System.getProperties(), null);
msg = new MimeMessage(mailSession, is);
}
//获取内容
getMailContent((Part) msg);
result.add(bodyText+"");
//saveAttachMent((Part) msg);
//result.add(attrName);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**解析附件或图片*/
public String parsePicOrAttr(String emlpath,String key,int check,String savepath){
try {
attrname="";
if(key!=null&&!key.equals("")){
//解密邮件
BASE64Decoder enc=new BASE64Decoder();
//需要一个流 给它解密的流
CipherInputStream CStream = null;
CStream =(CipherInputStream) Encrypt.decrypt(emlpath,Encrypt.toKey(enc.decodeBuffer(key)));
Session mailSession = Session.getDefaultInstance(System.getProperties(), null);
msg = new MimeMessage(mailSession, CStream);
}else{
InputStream is =new FileInputStream(emlpath);
Session mailSession = Session.getDefaultInstance(System.getProperties(), null);
msg = new MimeMessage(mailSession, is);
}
//处理图片与附件
saveAttachMent((Part)msg,check,savepath);
} catch (Exception e) {
e.printStackTrace();
}
return attrname;
}
/**获取内容*/
public void getMailContent(Part part) throws Exception {
String contenttype = part.getContentType();
int nameindex = contenttype.indexOf("name");
boolean conname = false;
if (nameindex != -1)
conname = true;
if (part.isMimeType("text/plain") && !conname) {
bodyText.append(part.getContent());
} else if (part.isMimeType("text/html") && !conname) {
bodyText.append(part.getContent());
} else if (part.isMimeType("multipart/*")) {
Multipart multipart = (Multipart) part.getContent();
int counts = multipart.getCount();
for (int i = 0; i < counts; i++) {
getMailContent(multipart.getBodyPart(i));
}
} else if (part.isMimeType("message/rfc822")) {
getMailContent((Part) part.getContent());
} else {
}
}
/**附件与图片*/
public void saveAttachMent(Part part,int check,String savepath) {
try {
boolean isPic=false;
String filename="";
String [] contentid=null;//是否有图片
if(part.isMimeType("multipart/*")){
Multipart mp = (Multipart)part.getContent();
for(int i=0;i<mp.getCount();i++){
BodyPart mpart = mp.getBodyPart(i);
String disposition="";
disposition = mpart.getDisposition();
if((disposition != null) &&((disposition.equals(Part.ATTACHMENT))||(disposition.equals(Part.INLINE)))){
filename = mpart.getFileName();
filename = MimeUtility.decodeText(filename);
//是否是图片
contentid=mpart.getHeader("Content-ID");
if(contentid!=null){//说明是图片
isPic=true;
}
//判断是解析图片还是附件
if(check==1){//附件
if(!isPic){
saveFile(filename,mpart.getInputStream(),savepath,check);
}
}else{//图片
if(isPic){
saveFile(filename,mpart.getInputStream(),savepath,check);
}
}
}else if(mpart.isMimeType("multipart/*")){
saveAttachMent(mpart,check,savepath);
}else if(mpart.isMimeType("message/rfc822")){
filename = mpart.getFileName();
filename=MimeUtility.decodeText(filename);
//是否是图片
contentid=mpart.getHeader("Content-ID");
if(contentid!=null){//说明是图片
isPic=true;
}
//判断是解析图片还是附件
if(check==1){//附件
if(!isPic){
saveFile(filename,mpart.getInputStream(),savepath,check);
}
}else{//图片
if(isPic){
saveFile(filename,mpart.getInputStream(),savepath,check);
}
}
}
}
}else if(part.isMimeType("message/rfc822")){
saveAttachMent((Part)part.getContent(),check,savepath);
}else if(part.isMimeType("application/octet-stream")){
filename=part.getFileName();
contentid=part.getHeader("Content-ID");
if(contentid!=null){//说明是图片
isPic=true;
}
//判断是解析图片还是附件
if(check==1){//附件
if(!isPic){
saveFile(filename,part.getInputStream(),savepath,check);
}
}else{//图片
if(isPic){
saveFile(filename,part.getInputStream(),savepath,check);
}
}
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("erro:保存附件异常!");
}
}
/**真正的保存附件到指定目录*/
private void saveFile(String fileName,InputStream in,String savepath,int check){
try {
//重新取名
String savaname="";//指定保存的名字
//时间命名
SimpleDateFormat df=new SimpleDateFormat("yyyyMMddHHmmssSSS");
String nowtime=df.format(new Date());
savaname=nowtime;
String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; //生成字符串从此序列中取
Random random = new Random();
StringBuffer sb = new StringBuffer();
for (int i=0;i<5;i++) {
int number = random.nextInt(base.length());
sb.append(base.charAt(number));
}
savaname+=sb.toString();
//附件名编码解析
if(fileName.contains("=?")||fileName.contains("?B")){
fileName=fileName.replace(" ", "");
fileName=MimeUtility.decodeText(fileName);
}else{
String newstr=fileName.replace("?", "");
newstr=new String(newstr.getBytes("iso-8859-1"));
if(!newstr.contains("?")){
fileName=new String(fileName.getBytes("iso-8859-1"));
}
}
//取得文件后缀
String[] typearry=fileName.split("\\.");
String filetype=typearry[typearry.length-1];
//如果是图片则使用新随即名称
if(check==1){//附件
attrname+=savaname+"."+filetype+"#####"+fileName+",";
}else{//图片
attrname+=savaname+"."+filetype+"#####";
}
String osName = System.getProperty("os.name");
String storedir = savepath;
String separator = "";
if (osName == null)
osName = "";
if (osName.toLowerCase().indexOf("win") != -1) {
separator = "\\";
if (storedir == null || storedir.equals(""))
storedir = "c:\\tmp";
}else {
separator = "/";
storedir = "/tmp";
}
File storefile = new File(storedir + separator);
if(!storefile.exists()){
storefile.mkdirs();
}
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
try {
bos = new BufferedOutputStream(new FileOutputStream(storefile+"//"+ savaname+"."+filetype));
bis = new BufferedInputStream(in);
int c;
while ((c = bis.read()) != -1) {
bos.write(c);
bos.flush();
}
}catch (Exception exception) {
exception.printStackTrace();
}finally {
bos.close();
bis.close();
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("erro:保存附件到指定目录异常!");
}
}
/**获得系统相对路径*/
public String path(){;
String pathew = this.getClass().getClassLoader().getResource("/").getPath();
pathew = pathew.substring(1, pathew.length());
pathew = pathew.replace("/WEB-INF/classes/","");
return pathew;
}
public static void main(String[] args) {
AnalysisMail paxlmail=new AnalysisMail();
List result = paxlmail.ParseEmlMethod("f:\\emily20161221170720.eml","");
}
}