那位大侠写过用javamail api和sun的pop3.jar来收信的?(255分)

  • 主题发起人 主题发起人 capitain
  • 开始时间 开始时间
C

capitain

Unregistered / Unconfirmed
GUEST, unregistred user!
小弟最近学习javamail,但是一直找不到有pop收信的帮助或文档请问各位大侠有一个小例子没有?或者哪里有pop收信比较详细的文档和javamail的文档?感觉不尽啊,我所以的分都送上了!
 
E文的要不要。
 
也要,但是不要是javamail自己带的那些,那个我看过,讲了等于没有
 
为什么要用java做?有点好奇。
 
用jsp+servlet来开发的webmail
 
>>但是不要是那些..,那个我看过,讲了等于没有
javamail自己带的不很好吗?
 
我给你一段源程序:
/*
A basic extension of the java.applet.Applet class
*/
import java.awt.*;
import java.applet.*;
import java.net.*;
import java.io.*;
public class Email extends Applet
{
public void init()
{
// Take out this line if youdo
n't use symantec.itools.net.RelativeURL or symantec.itools.awt.util.StatusScroller
symantec.itools.lang.Context.setApplet(this);
// This code is automatically generated by Visual Cafe when you add
// components to the visual environment. It instantiates and initializes
// the components. To modify the code, only use code syntax that matches
// what Visual Cafe can generate, or Visual Cafe may be unable to back
// parse your Java file into its visual environment.
//{{INIT_CONTROLS
setLayout(null);
setSize(560,477);
label1 = new java.awt.Label("MailTo:");
label1.setBounds(60,48,48,24);
add(label1);
txtMailTo = new java.awt.TextField();
txtMailTo.setBounds(108,48,300,24);
add(txtMailTo);
label2 = new java.awt.Label("RcvFrom");
label2.setBounds(48,72,48,24);
add(label2);
txtRcvFrom = new java.awt.TextField();
txtRcvFrom.setBounds(108,72,300,24);
add(txtRcvFrom);
label3 = new java.awt.Label("Subject:");
label3.setBounds(48,96,48,24);
add(label3);
textField1 = new java.awt.TextField();
textField1.setBounds(108,96,300,24);
add(textField1);
label4 = new java.awt.Label("CC");
label4.setBounds(60,120,36,24);
add(label4);
txtCC = new java.awt.TextField();
txtCC.setBounds(108,120,300,21);
add(txtCC);
Body = new java.awt.TextArea();
Body.setBounds(60,168,400,121);
add(Body);
Reply = new java.awt.TextArea();
Reply.setBounds(60,312,404,132);
add(Reply);
cmdSend = new java.awt.Button();
cmdSend.
setActionCommand("button");
cmdSend.
setLabel("Send");
cmdSend.
setBounds(444,96,83,29);
cmdSend.
setBackground(new Color(12632256));
add(cmdSend);
//}}

Action lAction = new Action();
cmdSend.
addActionListener(lAction);
}
//{{DECLARE_CONTROLS
java.awt.Label label1;
java.awt.TextField txtMailTo;
java.awt.Label label2;
java.awt.TextField txtRcvFrom;
java.awt.Label label3;
java.awt.TextField textField1;
java.awt.Label label4;
java.awt.TextField txtCC;
java.awt.TextArea Body;
java.awt.TextArea Reply;
java.awt.Button cmdSend;
//}}

public void sendMessage(java.awt.event.ActionEvent evt)
{
String sHostName = "gatekeeper.ncic.ac.cn";
int nPort = 25;
try {
Socket sktMail = new Socket(sHostName,nPort);
PrintStream ps = new PrintStream(sktMail.getOutputStream());
ps.println("HELO " + sHostName);
Reply.appendText(getReply(sktMail) + "/n");
ps.println("MAIL FROM: " + txtRcvFrom.getText());
Reply.appendText(getReply(sktMail) + "/n");
ps.println("RCPT TO: " + txtMailTo.getText());
Reply.appendText(getReply(sktMail) + "/n");
ps.println("DATA");
Reply.appendText(getReply(sktMail) + "/n");
ps.println("From: " + "刘真人" + "/n" + "Subject: " + textField1.getText() +"/n" +
"CC: " + txtCC.getText() +"/n" + Body.getText() +"/n" + "." + "/n");
Reply.appendText(getReply(sktMail) + "/n");
ps.println("QUIT");
Reply.appendText(getReply(sktMail));
ps.close();
sktMail.close();
} catch (IOException e) {
System.out.println("Error");
}
}
String getReply(Socket sktMail)
{
try {
DataInputStream disReply = new java.io.DataInputStream(sktMail.getInputStream());
return disReply.readLine();
} catch (IOException e) {
return e.getMessage();
}
}

class Action implements java.awt.event.ActionListener
{
public void actionPerformed(java.awt.event.ActionEvent event)
{
Object object = event.getSource();
if (object == cmdSend) {
sendMessage(event);
}
}
}
}
OK,我这摸够意思吧,给你这样长的原程序。



 
你写了这么多,我非常感谢,但是我问的是用sun的javamail api来实现收信
发信我都搞定了,就是不知道如何收,不知道孙老师试过那个javamail没?
 
看下面的代码:
import java.io.*;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class GetMessageExample {
public static void main (String args[]) throws Exception {
String host = "yourhost";
String username = "username";
String password = "password";
Properties props = new Properties();
Session session = Session.getInstance(props, null);
Store store = session.getStore("pop3");
store.connect(host, username, password);
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
BufferedReader reader = new BufferedReader (
new InputStreamReader(System.in));
Message message[] = folder.getMessages();
for (int i=0, n=message.length;
i<n;
i++) {
System.out.println(i + ": " + message.getFrom()[0]
+ "/t" + message.getSubject());
System.out.println(
"Do you want to read message? [YES to read/QUIT to end]");
String line = reader.readLine();
if ("YES".equals(line)) {
message.writeTo(System.out);
} else
if ("QUIT".equals(line)) {
break;
}
}
folder.close(false);
store.close();
}
}

 
怎么不能全部帖出来?下面是余下的:
Message message[] = folder.getMessages();
for (int i=0, n=message.length;
i<n;
i++) {
System.out.println(i + ": " + message.getFrom()[0]
+ "/t" + message.getSubject());
System.out.println(
"Do you want to read message? [YES to read/QUIT to end]");
String line = reader.readLine();
if ("YES".equals(line)) {
message.writeTo(System.out);
} else
if ("QUIT".equals(line)) {
break;
}
}
// Close connection
folder.close(false);
store.close();
}
}
 
capitain:代码全部帖出来了,只是没显示出来。
你用查看源文件的方法查看吧。
 
eguy很感谢!!!!!!!!!!,
还有一个小问题,收下来的是乱码
怎么才可以把它编码为可以看见的字符
用getbyte来转?还是有专门的类来解决?
要用到那些类,那些方法呢?
编码是在收信时就编码(用接口),还是收到本地再编码?
那个activetion的类这样的吗?可以给一个小例子?英文的都可以了
我现在一个人学,很需要入门的一些这方面的文档,
我愿意再出200分!
 
<p>sorry.我没有对代码测试。<br>
下面我改了一下。<br>
1。添加一个方法:<br>
public static void printParts(Part p) {<br>
try{<br>
Object o = p.getContent();<br>
if (o instanceof String) {<br>
System.out.println(&amp;quot;This is a String&amp;quot;);<br>
System.out.println((String)o);<br>
} else
if (o instanceof Multipart) {<br>
System.out.println(&amp;quot;This is a Multipart&amp;quot;);<br>
Multipart mp = (Multipart)o;<br>
int count = mp.getCount();<br>
for (int i = 0;
i &amp;lt;
count;
i++) {<br>
printParts(mp.getBodyPart(i));<br>
}<br>
} else
if (o instanceof InputStream) {<br>
System.out.println(&amp;quot;This is just an input stream&amp;quot;);<br>
InputStream is = (InputStream)o;<br>
int c;<br>
while ((c = is.read()) != -1)<br>
System.out.write(c);<br>
}}<br>
catch (Exception e) {<br>
e.printStackTrace();<br>
}<br>
}<br>
2。将<br>
if (&amp;quot;YES&amp;quot;.equals(line)) {<br>
&amp;nbsp;&amp;nbsp;&amp;nbsp;
message.writeTo(System.out);<br>
} <br>
改为<br>
if (&amp;quot;YES&amp;quot;.equals(line)) {<br>
&amp;nbsp;&amp;nbsp;&amp;nbsp;
printParts(message);<br>
} <br>
</p>
 
多谢!我明白了
我现在也正在看那个javamail,
懂了许多,
多谢大哥了
以后还要多多请教!
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
553
import
I
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部