当前位置: 技术问答>java相关
初学关于SOCKET的问题
来源: 互联网 发布时间:2017-03-17
本文导语: 各位大虾: 小弟刚学JAVA的网络编程,现在根据书上的例子使用socket中向smtp发送指令来 发送邮件: 以下是主要代码: socket = new Socket(InetAddress.getByName(host),Integer.parseInt(port)); //记录传输信息 ...
各位大虾:
小弟刚学JAVA的网络编程,现在根据书上的例子使用socket中向smtp发送指令来
发送邮件:
以下是主要代码:
socket = new Socket(InetAddress.getByName(host),Integer.parseInt(port));
//记录传输信息
frame.jLog.append("C: CONNECTED TO " + host+"port:"+linesep);
try{
//创建流
inData = new BufferedReader(new InputStreamReader (socket.getInputStream()));
outData = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
msg = "220 " + host + "Simple Mail Transfer Service readly";
//此函数是用于显示服务器回显的信息
SMTPResponse("220",msg);
//与服务器建立信任
msg = "HELO " + host;
//此函数用于向服务器发送指令,在命令后加上
this.SMTPSend(msg,true,true);
//CHECK response from server(helo)
msg = "250 OK" + host;
SMTPResponse("250 ",msg);
//check mail from address
msg = "MAIL FROM :";
SMTPSend(msg,true,true);
//check reponse from server(mail from )
SMTPResponse("250","250 OK","No such user here");
//check rcpt to address are valid or not
StringTokenizer tokensizer =
new StringTokenizer(to,",");
while(tokensizer.hasMoreTokens())
{
String rcpTo = tokensizer.nextToken().trim();
msg = "RCPT TO :";
SMTPSend(msg,true,true);
//check response from server(RCTP to)
msg = "250 OK";
SMTPResponse("250","250 OK","No such user here");
}
//start to send message content
msg = "DATA";
SMTPSend(msg,true,true);
msg = "354 Start mail input;end with .";
SMTPResponse("354",msg);
//Start date
Date sentDate = new Date();
SimpleDateFormat formatter1 = new SimpleDateFormat("EEE, d MMM yyyy hh:mm:ss z");
msg = "Date:" + formatter1.format(sentDate);
SMTPSend(msg,true,false);
//start From
msg = "From: " + addr.trim();
SMTPSend(msg,true,false);
//start to
msg = "To: " + to;
SMTPSend(msg,true,false);
//start subject
msg = "Subject: " + subject;
SMTPSend(msg,true,false);
outData.write('r');
outData.write('n');
//start content
StringBuffer content = new StringBuffer(msgText.length());
int k = 0,l;
while((l = msgText.indexOf("n.",k))!= -1)
{
content.append(msgText.substring(k,l) + "n..");
k = l + 2;
}
if (k >0)
{
content.append(msgText.substring(k));
msg = content.toString();
}else
{
msg = msgText+".";
}
//JOptionPane.showMessageDialog(null,msg);
SMTPSend(msg,true,false);
msg = String.valueOf(".");
SMTPSend(msg,true,true);
msg = "250 0K";
SMTPResponse("250",msg);
//end of contents
msg = "QUIT";
SMTPSend(msg,true,true);
//SMTPResponse("221",msg);
msg = "221" + host + "serviece closing transimisoon channe";
SMTPResponse("221",msg);
//
outData.close();
inData.close();
socket.close();
...........
但是执行的时候提示我要smtp发送的权限。但是,我看书上说用SOCKET发送是不要口令和权限的,只是POP才需要。请问各位是怎么回事,是不是我这本书太旧还是我没设置好指令。
返回错误是(c是客户发出的指令,S是服务器返回的信息):
C:Trying to connect to host[ smtp.21cn.com] ,port[25]
C: CONNECTED TO smtp.21cn.comport:
S:220 smtp.21cn.comSimple Mail Transfer Service readly
c:HELO smtp.21cn.com
S:250 smtp.21cn.com
c:MAIL FROM :
s:250 553 From , message blocked, you are not authorized to send mail, authentication is required.
S:250 OK
c:RCPT TO:
s:250 503 Bad sequence of commands
S:250 OK
c:DATA
S:354 503 Need RCPT before DATA.
S:354 Start mail input;end with .
c:Date:星期二, 10 十二月 2002 12:12:14 CST
c:From: wenchengli_2000@21cn.com
c:To: wenchengli_2000@21cn.com
c:Subject: test
c:sssssssss.
c:.
S:250 500 Syntax error, command unrecognized
S:250 0K
c:QUIT
S:221 500 Syntax error, command unrecognized
S:221smtp.21cn.comserviece closing transimisoon channe
小弟刚学JAVA的网络编程,现在根据书上的例子使用socket中向smtp发送指令来
发送邮件:
以下是主要代码:
socket = new Socket(InetAddress.getByName(host),Integer.parseInt(port));
//记录传输信息
frame.jLog.append("C: CONNECTED TO " + host+"port:"+linesep);
try{
//创建流
inData = new BufferedReader(new InputStreamReader (socket.getInputStream()));
outData = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
msg = "220 " + host + "Simple Mail Transfer Service readly";
//此函数是用于显示服务器回显的信息
SMTPResponse("220",msg);
//与服务器建立信任
msg = "HELO " + host;
//此函数用于向服务器发送指令,在命令后加上
this.SMTPSend(msg,true,true);
//CHECK response from server(helo)
msg = "250 OK" + host;
SMTPResponse("250 ",msg);
//check mail from address
msg = "MAIL FROM :";
SMTPSend(msg,true,true);
//check reponse from server(mail from )
SMTPResponse("250","250 OK","No such user here");
//check rcpt to address are valid or not
StringTokenizer tokensizer =
new StringTokenizer(to,",");
while(tokensizer.hasMoreTokens())
{
String rcpTo = tokensizer.nextToken().trim();
msg = "RCPT TO :";
SMTPSend(msg,true,true);
//check response from server(RCTP to)
msg = "250 OK";
SMTPResponse("250","250 OK","No such user here");
}
//start to send message content
msg = "DATA";
SMTPSend(msg,true,true);
msg = "354 Start mail input;end with .";
SMTPResponse("354",msg);
//Start date
Date sentDate = new Date();
SimpleDateFormat formatter1 = new SimpleDateFormat("EEE, d MMM yyyy hh:mm:ss z");
msg = "Date:" + formatter1.format(sentDate);
SMTPSend(msg,true,false);
//start From
msg = "From: " + addr.trim();
SMTPSend(msg,true,false);
//start to
msg = "To: " + to;
SMTPSend(msg,true,false);
//start subject
msg = "Subject: " + subject;
SMTPSend(msg,true,false);
outData.write('r');
outData.write('n');
//start content
StringBuffer content = new StringBuffer(msgText.length());
int k = 0,l;
while((l = msgText.indexOf("n.",k))!= -1)
{
content.append(msgText.substring(k,l) + "n..");
k = l + 2;
}
if (k >0)
{
content.append(msgText.substring(k));
msg = content.toString();
}else
{
msg = msgText+".";
}
//JOptionPane.showMessageDialog(null,msg);
SMTPSend(msg,true,false);
msg = String.valueOf(".");
SMTPSend(msg,true,true);
msg = "250 0K";
SMTPResponse("250",msg);
//end of contents
msg = "QUIT";
SMTPSend(msg,true,true);
//SMTPResponse("221",msg);
msg = "221" + host + "serviece closing transimisoon channe";
SMTPResponse("221",msg);
//
outData.close();
inData.close();
socket.close();
...........
但是执行的时候提示我要smtp发送的权限。但是,我看书上说用SOCKET发送是不要口令和权限的,只是POP才需要。请问各位是怎么回事,是不是我这本书太旧还是我没设置好指令。
返回错误是(c是客户发出的指令,S是服务器返回的信息):
C:Trying to connect to host[ smtp.21cn.com] ,port[25]
C: CONNECTED TO smtp.21cn.comport:
S:220 smtp.21cn.comSimple Mail Transfer Service readly
c:HELO smtp.21cn.com
S:250 smtp.21cn.com
c:MAIL FROM :
s:250 553 From , message blocked, you are not authorized to send mail, authentication is required.
S:250 OK
c:RCPT TO:
s:250 503 Bad sequence of commands
S:250 OK
c:DATA
S:354 503 Need RCPT before DATA.
S:354 Start mail input;end with .
c:Date:星期二, 10 十二月 2002 12:12:14 CST
c:From: wenchengli_2000@21cn.com
c:To: wenchengli_2000@21cn.com
c:Subject: test
c:sssssssss.
c:.
S:250 500 Syntax error, command unrecognized
S:250 0K
c:QUIT
S:221 500 Syntax error, command unrecognized
S:221smtp.21cn.comserviece closing transimisoon channe
|
这是邮件服务器的设置问题:有的邮件服务器发送的时候也是需要登陆的,如果不登陆就无法发送,为了避免这个问题,你可以自己搭建一个简单的email server。