当前位置: 技术问答>java相关
java中怎样实现利用socket编写收邮件的代码
来源: 互联网 发布时间:2015-02-11
本文导语: 向pop服务器提交用户名和密码以后,如何得到邮件的相关内容(包括完全收取下来) | import java.io.*; import java.net.*; import java.util.*; import javax.swing.JOptionPane; import java.awt.event.*; public clas...
向pop服务器提交用户名和密码以后,如何得到邮件的相关内容(包括完全收取下来)
|
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.JOptionPane;
import java.awt.event.*;
public class Pop3test implements Runnable
{
Socket s = null;
BufferedReader in = null;
//BufferedWriter out = null;
PrintWriter out = null;
Vector vector = new Vector();
mailtxt mt = null;
boolean seemsg = false;
public static void main(String[] args){
Pop3test pop = new Pop3test();
Thread h = new Thread(pop);
h.start();
}
public void run(){
getmail("pop.yeah.net","user","****",110,true);
~~~~~用户名~~密码
}
public void getmail(String server,String user,String pwd,int port,boolean now){
try{
s = new Socket(server,port);
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
//out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
out = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
seemsg = now;
String str;
// System.out.println(server+" pop3");
str = in.readLine();
System.out.println("USER :"+user);
if (str.indexOf("OK")-1){
String maildate = str.substring(n+11,str.length()-n);
System.out.println("Message-ID:"+maildate);
received = true;
continue;
}
n = str.indexOf("To:");
if (n>-1){
String maildate = str.substring(n+3,str.length()-n);
System.out.println("To :"+maildate);
received = true;
continue;
}
n = str.indexOf("From:");
if (n>-1){
String maildate = str.substring(n+5,str.length()-n);
System.out.println("From :"+maildate);
received = true;
continue;
}
n = str.indexOf("Subject:");
if (n>-1){
String maildate = new String(str.substring(n+8,str.length()-n).getBytes("gb2312"));
// String maildate = str.substring(n+8,str.length()-n);
System.out.println("Subject :"+maildate);
received = true;
continue;
}
n = str.indexOf("X-Priority:");
if (n>-1){
String maildate = str.substring(n+11,str.length()-n);
System.out.println("X-Priority:"+maildate);
received = true;
continue;
}
n = str.indexOf("X-Originating-IP:");
if (n>-1){
String maildate = str.substring(n+17,str.length()-n);
System.out.println("X-Originating-IP:"+maildate);
received = true;
continue;
}
n = str.indexOf("Sender:");
if (n>-1){
String maildate = new String(str.substring(n+7,str.length()-n).getBytes("gb2312"));
//String maildate = str.substring(n+7,str.length()-n);
System.out.println("Sender :"+maildate);
received = true;
continue;
}
if (!received){
continue;
}
str = new String(str.getBytes("gb2312"));
//str = new String(bbb);
if (seemsg){
result += str + "n";
}
}
if (seemsg){
System.out.println(result);
}
System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
return true;
}catch(IOException io){
System.out.println(io.toString());
return false;
}
}
}
import java.net.*;
import java.util.*;
import javax.swing.JOptionPane;
import java.awt.event.*;
public class Pop3test implements Runnable
{
Socket s = null;
BufferedReader in = null;
//BufferedWriter out = null;
PrintWriter out = null;
Vector vector = new Vector();
mailtxt mt = null;
boolean seemsg = false;
public static void main(String[] args){
Pop3test pop = new Pop3test();
Thread h = new Thread(pop);
h.start();
}
public void run(){
getmail("pop.yeah.net","user","****",110,true);
~~~~~用户名~~密码
}
public void getmail(String server,String user,String pwd,int port,boolean now){
try{
s = new Socket(server,port);
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
//out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
out = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
seemsg = now;
String str;
// System.out.println(server+" pop3");
str = in.readLine();
System.out.println("USER :"+user);
if (str.indexOf("OK")-1){
String maildate = str.substring(n+11,str.length()-n);
System.out.println("Message-ID:"+maildate);
received = true;
continue;
}
n = str.indexOf("To:");
if (n>-1){
String maildate = str.substring(n+3,str.length()-n);
System.out.println("To :"+maildate);
received = true;
continue;
}
n = str.indexOf("From:");
if (n>-1){
String maildate = str.substring(n+5,str.length()-n);
System.out.println("From :"+maildate);
received = true;
continue;
}
n = str.indexOf("Subject:");
if (n>-1){
String maildate = new String(str.substring(n+8,str.length()-n).getBytes("gb2312"));
// String maildate = str.substring(n+8,str.length()-n);
System.out.println("Subject :"+maildate);
received = true;
continue;
}
n = str.indexOf("X-Priority:");
if (n>-1){
String maildate = str.substring(n+11,str.length()-n);
System.out.println("X-Priority:"+maildate);
received = true;
continue;
}
n = str.indexOf("X-Originating-IP:");
if (n>-1){
String maildate = str.substring(n+17,str.length()-n);
System.out.println("X-Originating-IP:"+maildate);
received = true;
continue;
}
n = str.indexOf("Sender:");
if (n>-1){
String maildate = new String(str.substring(n+7,str.length()-n).getBytes("gb2312"));
//String maildate = str.substring(n+7,str.length()-n);
System.out.println("Sender :"+maildate);
received = true;
continue;
}
if (!received){
continue;
}
str = new String(str.getBytes("gb2312"));
//str = new String(bbb);
if (seemsg){
result += str + "n";
}
}
if (seemsg){
System.out.println(result);
}
System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
return true;
}catch(IOException io){
System.out.println(io.toString());
return false;
}
}
}
|
不是有javamail api吗?
干嘛要用低层的?
干嘛要用低层的?