您的位置:程序门 -> java -> web 开发



用java socket做的一个发送邮件的程序出现错误,大家给看看


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


用java socket做的一个发送邮件的程序出现错误,大家给看看
发表于:2007-09-19 21:23:34 楼主
源码:
package   mail;

import   java.awt.*;
import   java.awt.event.*;
import   java.util.*;
import   java.net.*;
import   java.io.*;
import   javax.swing.*;

public   class   mailtest  
{
public   static   void   main(string   args[])
{
jframe   frame=new   mailtestframe();
frame.setdefaultcloseoperation(jframe.exit_on_close);
frame.show();
}
}

class   mailtestframe   extends   jframe
{
private   bufferedreader   in;
private   printwriter   out;
private   jtextfield   from;
private   jtextfield   to;
private   jtextfield   smtpserver;
private   jtextarea   message;
private   jtextarea   communication;
sun.misc.base64encoder   enc;


public   static   final   int   width=300;
public   static   final   int   height=300;
public   mailtestframe()
{
setsize(width,height);
settitle( "mailtest ");
gridbaglayout   gbag=new   gridbaglayout();
getcontentpane().setlayout(gbag);

gridbagconstraints   gbc=new   gridbagconstraints();
gbc.fill=gridbagconstraints.horizontal;
gbc.weightx=0;
gbc.weighty=0;

gbc.weightx=0;
add(new   jlabel( "from: "),gbc,0,0,1,1);
gbc.weightx=100;
from=new   jtextfield(20);
add(from,gbc,1,0,1,1);

gbc.weightx=0;
add(new   jlabel( "to: "),gbc,0,1,1,1);
gbc.weightx=100;
to=new   jtextfield(20);
add(to,gbc,1,1,1,1);

gbc.weightx=0;
add(new   jlabel( "smtp   server: "),gbc,0,2,1,1);
gbc.weightx=100;
smtpserver=new   jtextfield(20);
add(smtpserver,gbc,1,2,1,1);

gbc.fill=gridbagconstraints.both;
gbc.weighty=100;
message=new   jtextarea();
add(new   jscrollpane(message),gbc,0,3,2,1);

communication=new   jtextarea();
add(new   jscrollpane(communication),gbc,0,4,2,1);

gbc.weighty=0;
jbutton   sendbutton=new   jbutton( "send ");
sendbutton.addactionlistener(new   actionlistener()
{
public   void   actionperformed(actionevent   evt)
{
new   thread()
{
public   void   run()
{
sendmail();
}
}.start();
}
});

jpanel   buttonpanel=new   jpanel();
buttonpanel.add(sendbutton);
add(buttonpanel,gbc,0,5,2,1);
}

private   void   add(component   c,gridbagconstraints   gbc,int   x,int   y,int   w,int   h)
{
gbc.gridx=x;
gbc.gridy=y;
gbc.gridwidth=w;
gbc.gridheight=h;
getcontentpane().add(c,gbc);
}

public   void   sendmail()
{
try
{
socket   s=new   socket(smtpserver.gettext(),25);

out=new   printwriter(s.getoutputstream());
in=new   bufferedreader(new   inputstreamreader(s.getinputstream()));
string   hostname=inetaddress.getlocalhost().gethostname();

receive();

send( "helo   "+hostname);
receive();
send( "mail   from: < "+from.gettext()+ "> ");
receive();
send( "rcpt   to: < "+to.gettext()+ "> ");
receive();
send( "data ");
receive();
stringtokenizer   tokenizer=new   stringtokenizer(message.gettext(), "\n ");
while(tokenizer.hasmoretokens())
send(tokenizer.nexttoken());
send( ". ");
s.close();
}
catch(ioexception   exception)
{
communication.append( "error:   "+exception);
}
}

public   void   send(string   s)throws   ioexception
{
communication.append(s);
communication.append( "\n ");
out.print(s);
out.print( "\r\n ");
out.flush();
}
public   void   receive()throws   ioexception
{
string   line=in.readline();
if(line!=null)
{
communication.append(line);
communication.append( "\n ");
}
}

}

最后报错为:
mail   from: <fff@ff.com>
553   requested   action   not   taken:   local   user   only,smtp5,wkjsjrcbpwlph_fgubb9ag==.26394s2   1190207440
rcpt   to: <******@126.com>
503   bad   sequence   of   commands
data
354   send   from   rising   mail   proxy
fffff
麻烦,大家给我分析下是什么问题啊,多谢了


快速检索

最新资讯
热门点击