📄 testmail.java
字号:
import java.io.*;
import sun.io.*;
import java.lang.*;
import java.util.*;
import java.net.*;
import java.text.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class testMail implements Runnable
{
public static void main(String arg[]) throws Exception
{
ThreadGroup g=new ThreadGroup("TG");
Runnable r=new testMail();
Thread t;
for (;;)
{
t = new Thread(g,r);
t.start();
t.sleep(60000);
}
}
public void run()
{
String timeTxt = getTime();
int myCount=0;
String rName="";
String time1Txt=timeTxt.substring(0,4)+timeTxt.substring(5,7)+timeTxt.substring(8,10)+timeTxt.substring(11,13)+timeTxt.substring(14,16);
String host="mail.hrwf.net";
String user="w.fautre@hrwf.net";
String pass="willy1";
if (timeTxt.substring(14,16).equals("24"))
//if (timeTxt.substring(15,16).equals("4"))
{
String FileName="\\mail\\html\\mail"+time1Txt+".html";
System.out.println(timeTxt);
File fileOut = new File(FileName);
try
{
FileWriter xyz = new FileWriter(fileOut);
Session session =Session.getInstance(new Properties(),null);
Store mystore=session.getStore("pop3");
mystore.connect(host,user,pass);
if(mystore.isConnected())
{
Folder inbox = mystore.getFolder("INBOX");
if (inbox.exists()) {
inbox.open(Folder.READ_WRITE);
int nCount = inbox.getMessageCount();
if (nCount > 0)
{
for(int i=1;i<=nCount;i++){
MimeMessage msg = (MimeMessage)inbox.getMessage(i);
if (msg.isMimeType("multipart/*") || msg.isMimeType("MULTIPART/*") || msg.isMimeType("Multipart/*"))
{
Part p = (Part)msg;
try
{
rName = handleMultipart(p);
}
catch (Exception e)
{
System.out.println("mail error01");
}
}
else
{
rName = msg.getContent().toString();
}
xyz.write(rName);
}
xyz.close();
}
}
inbox.close(true);
}
mystore.close();
}
catch (Exception e)
{
System.out.println("mail error02");
}
}
}
public static String handleMultipart(Part p) throws Exception
{
String rName="";
String rImage="";
String weboa_osName = System.getProperty("os.name");
String mct = "";
String attPath = "\\mail\\file\\";
String attFile = "";
String filesName = "";
String timeTxt0 = "";
String timeTxt1 = "";
Multipart mp=(Multipart)p.getContent();
int myCount = mp.getCount();
for (int k=0;k<myCount;k++)
{
if (mp.getBodyPart(k).isMimeType("multipart/*") || mp.getBodyPart(k).isMimeType("Multipart/*") || mp.getBodyPart(k).isMimeType("MULTIPART/*"))
{
handleMultipart(mp.getBodyPart(k));
}
if (mp.getBodyPart(k).isMimeType("image/*") || mp.getBodyPart(k).isMimeType("Image/*") || mp.getBodyPart(k).isMimeType("IMAGE/*"))
{
rName = rName;
}
if (mp.getBodyPart(k).isMimeType("text/*") || mp.getBodyPart(k).isMimeType("Text/*") || mp.getBodyPart(k).isMimeType("TEXT/*"))
{
rName = mp.getBodyPart(k).getContent().toString();
if (mp.getBodyPart(k).isMimeType("text/plain") || mp.getBodyPart(k).isMimeType("TEXT/PLAIN"))
{
rName = rName.replaceAll("\n","<br>");
try
{
if (mp.getBodyPart(k).getHeader("Content-Transfer-Encoding")[0].equals("base64") || mp.getBodyPart(k).getHeader("Content-Transfer-Encoding")[0].toUpperCase().equals("8BIT"))
{
rName = rName;
}
else
{
rName = new String(rName.getBytes("ISO8859_1"),"GB2312");
}
}
catch (Exception e)
{
System.out.println("mail error03");
}
}
}
if (mp.getBodyPart(k).isMimeType("application/*") || mp.getBodyPart(k).isMimeType("Application/*") || mp.getBodyPart(k).isMimeType("APPLICATION/*"))
{
mct = mp.getBodyPart(k).getDisposition();
if (mct != null && mct.equals(Part.ATTACHMENT))
{
filesName = mp.getBodyPart(k).getFileName();
if (filesName.startsWith("=?gb") || filesName.startsWith("=?GB"))
{
filesName = MimeUtility.decodeText(filesName);
filesName = new String(filesName.getBytes("GB2312"),"ISO8859_1");
}
timeTxt0 = getTime();
timeTxt1 = timeTxt0.substring(0,4)+timeTxt0.substring(5,7)+timeTxt0.substring(8,10)+timeTxt0.substring(11,13)+timeTxt0.substring(14,16);
//attFile = attPath + timeTxt1 + getRandString(100) + String.valueOf(k) + "." + getFileExt(filesName);
attFile = attPath + timeTxt1 + getRandString(4) + String.valueOf(k) + "." + getFileExt(filesName);
System.out.println(attFile);
FileOutputStream out = new FileOutputStream ( attFile );
InputStream in = mp.getBodyPart(k).getInputStream();
int c = in.read();
while (c != -1)
{
out.write(c);
c = in.read();
}
in.close();
out.close();
}
}
}
return rName;
}
public static String getTime()
{
Calendar calendar = Calendar.getInstance(new Locale("CHINESE", "", ""));
int i = calendar.get(1);
int j = calendar.get(2);
int k = calendar.get(5);
int l = calendar.get(11);
int i1 = calendar.get(12);
int j1 = calendar.get(13);
String s1 = String.valueOf(++j);
if(s1.length() == 1)
s1 = "0" + s1;
String s2 = String.valueOf(k);
if(s2.length() == 1)
s2 = "0" + s2;
String s = String.valueOf(i) + "-" + s1 + "-" + s2 + " ";
String s3 = String.valueOf(l);
if(s3.length() == 1)
s3 = "0" + s3;
String s4 = String.valueOf(i1);
if(s4.length() == 1)
s4 = "0" + s4;
String s5 = String.valueOf(j1);
if(s5.length() == 1)
s5 = "0" + s5;
s = s + s3 + ":" + s4 + ":" + s5;
return s;
}
public static String getFileExt(String fileName)
{
String value = new String();
int start = 0;
int end = 0;
if(fileName == null)
return null;
start = fileName.lastIndexOf(46) + 1;
end = fileName.length();
value = fileName.substring(start, end);
if(fileName.lastIndexOf(46) > 0)
return value;
else
return "";
}
public static String getRandString(int i)
{
int j = 1;
boolean flag = false;
for(int k = 0; k < i; k++)
j *= 10;
Random random = new Random();
String s = String.valueOf(random.nextInt(j));
for(int l = s.length(); l < i; l++)
s = "0" + s;
return s;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -