📄 getmail.java
字号:
/*
* getMail.java
*
* Created on 2006年5月24日, 下午7:57
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package javamail;
/**
*
* @author freezing
*/
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Date;
import javax.activation.*;
import java.io.*;
import javax.swing.*;
public class getMail
{
private Store store;
private Folder folder;
private Message []message;
private Properties pro;
private String Host;
private String Name;
private String PSW;
private Session session;
private int count;
private Vector mailList;
public getMail(String host,String name,String password)throws NoSuchProviderException
{
try
{
Host=host;
Name=name;
PSW=password;
mailList=new Vector(10,1);
pro=System.getProperties();
session=Session.getDefaultInstance(pro,null);
store=session.getStore("pop3");
store.connect(Host,Name,PSW);
folder=store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
message=folder.getMessages();
count=message.length;
System.out.println(count);
// for(int i=0;i<message.length;i++)
// {
// System.out.println(i+": "+message[i].getFrom()[0]
// +"\t"+message[i].getSubject());
// }//end of Read Mail from WebServer and get the list
//
//begin to get the content of the spescial Mail
// BufferedReader reader = new BufferedReader ( new InputStreamReader(System.in));
// message[0].writeTo(System.out);
//System.out.println(((MimeMessage)message[0]).getContent());
//getcontent()不包括邮件头writeTo()则包括邮件头.
//-------------------------------------------------------
}catch(MessagingException e)
{
System.out.println(e);
}
}
public int MailCount()
{
return count;
}
public Vector vecMailList()
{
try{
for(int i=0;i<message.length;i++)
{
String temp=message[i].getFrom()[0]+"\t"+message[i].getSubject();
//System.out.println(i+": "+temp);
mailList.addElement(new String(temp));
}
return mailList;
}catch(MessagingException e)
{
System.out.println(e);
JOptionPane.showMessageDialog(null,"无法从服务器端获取邮件列表,请检查客户端口设置!","警告",JOptionPane.WARNING_MESSAGE);
return null;
}
}
public void getMailHead(int index)
{
try{
if(index>=0&&index<count)
{
String temp=message[index].getFrom()[0]+"\t"+message[index].getSubject();
System.out.println(index+": "+temp);
//mailList.addElement(new String(temp));
}
}
catch(MessagingException e)
{
System.out.println(e);
}
}
public void getMailBody(int index)
{
try{
System.out.println(((MimeMessage)message[index]).getContent());
}catch(MessagingException e)
{
System.out.println(e);
}
catch(IOException e)
{
System.out.println(e);
}
}
public void getMailAttach(int index)throws IOException,MessagingException
{
try{
Multipart mp = (Multipart)message[index].getContent();
for (int i=0, n=mp.getCount(); i<n; i++)
{
Part part = mp.getBodyPart(i);
String disposition = part.getDisposition();
System.out.println(disposition);
if ((disposition != null) && ((disposition.equals(Part.ATTACHMENT)||
(disposition.equals(Part.INLINE)))))
{
saveFile(transCode(part.getFileName()), part.getInputStream());
}
else if(disposition==null)
{
MimeBodyPart mbp = (MimeBodyPart)part;
if(mbp.isMimeType("text/plain"))
{
System.out.println("以下的文本格式");
String temp=(String)part.getContent();
System.out.println(temp);
showTextDialog show=new showTextDialog();
show.setContent(temp);
show.setVisible(true);
}
else if(mbp.isMimeType("text/html"))
{
System.out.println("网页格式");
String temp=(String)part.getContent();
System.out.println(temp);
showHtmlDialog app=new showHtmlDialog(temp);
app.setVisible(true);
}
}
}
}catch(IOException e)
{
System.out.println(e);
JOptionPane.showMessageDialog(null,"无法下载附件!","警告",JOptionPane.WARNING_MESSAGE);
}catch(MessagingException e)
{
System.out.println(e);
JOptionPane.showMessageDialog(null,"会话交互失败!","警告",JOptionPane.WARNING_MESSAGE);
}
}
public String transCode(String str)
{
try{
str=new String(str.getBytes("ISO8859_1"),"gb2312");
return str;
}catch(Exception e)
{
return null;
}
}
public void saveFile(String filename,InputStream is)
{
File file=new File(filename);
System.out.println(filename);
if(!file.exists())
{
try{
System.out.println("creating file....");
file.createNewFile();
FileOutputStream out=new FileOutputStream(file);
int c;
while((c=is.read())!=-1)
{
out.write(c);
}
System.out.println("file created!!!");
JOptionPane.showMessageDialog(null,"附件已经保存在特定目录下,请查看!","信息",JOptionPane.PLAIN_MESSAGE);
out.close();
is.close();
}catch(IOException e)
{
System.out.println(e);
JOptionPane.showMessageDialog(null,"无法下载附件!","警告",JOptionPane.WARNING_MESSAGE);
}
}
}
public static void main(String args[])throws NoSuchProviderException
{
getMail app=new getMail("pop3.163.com","starlights17","98905917");
int m=4;
app.getMailHead(m);
//app.getMailBody(m);
Vector vec;
// try{
//app.getMailAttach(m);
vec=app.vecMailList();
for(Object x:vec)
{
System.out.println((String)x);
}
// }
// catch(IOException e)
// {
// System.out.println(e);
// }
// catch(MessagingException e)
// {
// System.out.println(e);
// }
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -