急问!!!为什么我的邮件发送程序不能成功发送邮件!!! 为什么要这样作?你发送的信息符合邮件协议没有。用JavaMail就可以了,非得自己写吗? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 morson (猫生) 我觉得最大的问题是在你的 Send("AUTH LOGIN") 后没有通过。就是说当你 Send(User) 和 Send(PassWord) 后 得到的是535 Error: authentication failed.所以发不出去。对了,问一下你那里看到的AUTH LOGIN 在 SMTP 中的,我的文档资料太老了,还不支持这种用法呢。 谢谢大家,这个程序我已经成功摆平了.以前错误的原因不是Send("AUTH LOGIN") 后没有通过,我在原来的程序中添加了一些代码,把Send函数写成:void Send(String s) throws IOException { if(s!=null) {jTextArea1.append(s+"\n"); out.println(s); out.flush(); } String line; if((line=in.readLine())!=null) jTextArea1.append(line+"\n"); }然后程序就可以运行了.AUTH LOGIN 我是在一本比较新的书上看到的刘勇 Java实例入门 中国青年出版社 2002 请问 Base64.encode()是那个类中的呀,我怎么查不到。 还有,我用你的程序跑了一下,无论我如何努力,就是出现535 Error: authentication failed.当然我没有用 Base64 用JavaMail就可以完成电子邮件的发送了,何必再自己实现smtp协议呢? yanyanEM(井井井) :base64这个类是我自己封装的,用来把用户名、密码转换为字节码,这在通过服务器验证时时必须的。如果你不使用它,其结果当然是通不过服务器的验证。就像你遇到的错误一样。另外,你必须注意你要事先获得通过邮件服务的服务器认证。比如说先到263注册一个信箱,这样就可以申请获得263的邮件服务。过几天我把整个程序的源代码全部写上,包括base64.java gfzhx(小小) 因为:我使用JBuilder enterprise 6.0 438.0时遇到一个很奇怪的现象:在帮助中有这个包javax.mail但是在用的时候import javax.之后系统给的提示中没有javax.mail这是为什么,请大家帮忙回答这个问题我也发了帖子。有人的回答为:需要 在 project 属性 path -> libary里面加入 jdk1.3或以上(JBuilder里面带的有) 的 jar文件然而我在project属性里加上了jdek1.4,但是不能用。在生成project时就会默认用了jdk1.4,我从来没动过它打import java.后会出io,*等,就是打import javax.后不会出mail,也不知道为什么。 //以下这段程序是使用JBuilder开发工具编写的//在本程序中263用户mailsender通过263的邮件发送服务,向地址//[email protected]发送//邮件,内容为:this is a test for sending mail.package sendmail;import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.util.*;import java.net.*;import java.io.*;import com.borland.jbcl.layout.*;//类定义public class Frame1 extends JFrame { JPanel contentPane; XYLayout xYLayout1 = new XYLayout(); JButton jButton1 = new JButton(); java.io.PrintWriter out; BufferedReader in; JTextArea jTextArea1 = new JTextArea(); //建立窗体 public Frame1() { enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit(); } catch(Exception e) { e.printStackTrace(); } }//设置窗体界面 private void jbInit() throws Exception { contentPane = (JPanel) this.getContentPane(); jButton1.setText("发送"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButton1_actionPerformed(e); } }); contentPane.setLayout(xYLayout1); this.setSize(new Dimension(400, 300)); this.setTitle("Frame Title"); jTextArea1.setVerifyInputWhenFocusTarget(false); jTextArea1.setLineWrap(true); jTextArea1.setWrapStyleWord(true); contentPane.add(jButton1, new XYConstraints(170, 49, -1, -1)); contentPane.add(jTextArea1, new XYConstraints(81, 88, -1, 189)); } //关闭窗体 protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } }//邮件发送部分 void jButton1_actionPerformed(ActionEvent e) { String User="[email protected]"; String Password="123456"; String text="this is a test for sending mail"; try {Socket s=new Socket("smtp.263.net",25); out=new java.io.PrintWriter(s.getOutputStream()); in=new BufferedReader(new InputStreamReader(s.getInputStream())); String hostname=InetAddress.getLocalHost().getHostName(); Send(null); Send("HELO"+hostname); Send("AUTH LOGIN"); String tmp=new String(Base64.encode(User.getBytes())); Send(tmp); String tp=new String(Base64.encode(Password.getBytes())); Send(tp); Send("MAIL FROM:"+"[email protected]"); Send("RCPT TO:"+"[email protected]"); Send("DATA"); out.println(text); Send("."); Send("QUIT"); s.close();} catch(IOException ex) {jTextArea1.append("error:"+ex);} }//自定义的send函数,用于发送字符串s void Send(String s) throws IOException { if(s!=null) {jTextArea1.append(s+"\n"); out.println(s); out.flush(); } String line; if((line=in.readLine())!=null) jTextArea1.append(line+"\n"); }}//发送邮件时采用的编码方法由自定义的类Base64.java实现。//其中编码的方法为Base64.encodepackage sendmail;public class Base64 {/*** returns an array of base64-encoded characters to represent the* passed data array.** @param data the array of bytes to encode* @return base64-coded character array.*/static public char[] encode(byte[] data){char[] out = new char[((data.length + 2) / 3) * 4];//// 3 bytes encode to 4 chars. Output is always an even// multiple of 4 characters.//for (int i=0, index=0; i<data.length; i+=3, index+=4) {boolean quad = false;boolean trip = false;int val = (0xFF & (int) data[i]);val <<= 8;if ((i+1) < data.length) {val |= (0xFF & (int) data[i+1]);trip = true;}val <<= 8;if ((i+2) < data.length) {val |= (0xFF & (int) data[i+2]);quad = true;}out[index+3] = alphabet[(quad? (val & 0x3F): 64)];val >>= 6;out[index+2] = alphabet[(trip? (val & 0x3F): 64)];val >>= 6;out[index+1] = alphabet[val & 0x3F];val >>= 6;out[index+0] = alphabet[val & 0x3F];}return out;}/*** Returns an array of bytes which were encoded in the passed* character array.** @param data the array of base64-encoded characters* @return decoded data array*/static public byte[] decode(char[] data){int len = ((data.length + 3) / 4) * 3;if (data.length>0 && data[len-1] == '=') --len;if (data.length>0 && data[len-2] == '=') --len;byte[] out = new byte[len];int shift = 0; // # of excess bits stored in accumint accum = 0; // excess bitsint index = 0;for (int ix=0; ix<data.length; ix++){int value = codes[ data[ix] & 0xFF ]; // ignore high byte of charif ( value >= 0 ) { // skip over non-codeaccum <<= 6; // bits shift up by 6 each time thrushift += 6; // loop, with new bits being put inaccum |= value; // at the bottom.if ( shift >= 8 ) { // whenever there are 8 or more shifted in,shift -= 8; // write them out (from the top, leaving anyout[index++] = // excess at the bottom for next iteration.(byte) ((accum >> shift) & 0xff);} } }if (index != out.length)throw new Error("miscalculated data length!");return out;}//// code characters for values 0..63//static private char[] alphabet ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".toCharArray();//// lookup table for converting base64 characters to value in range 0..63//static private byte[] codes = new byte[256];static {for (int i=0; i<256; i++) codes[i] = -1;for (int i = 'A'; i <= 'Z'; i++) codes[i] = (byte)( i - 'A');for (int i = 'a'; i <= 'z'; i++) codes[i] = (byte)(26 + i - 'a');for (int i = '0'; i <= '9'; i++) codes[i] = (byte)(52 + i - '0');codes['+'] = 62;codes['/'] = 63;}} 关于 equals方法 一个小疑问! JAVA中的package的正确与否的检查 如何用jdbc实现授权表的访问 关于指针的替代. *_* 顺利换公司 散100分 JButton的setBackground(Color)为什么不起作用? 如何将e:\java;加入classpath变量中? 关于Component类中的paint方法的参数 关于java中常量的赋值 该不该参加SCJP的培圳啊?有没有帮助,值不值?30分 今天学java的一点感触 vj可以做纯java么?
就是说当你 Send(User) 和 Send(PassWord) 后 得到的是
535 Error: authentication failed.所以发不出去。对了,问一下你那里看到的AUTH LOGIN 在 SMTP 中的,我的文档资料太老了,还不支持这种用法呢。
void Send(String s) throws IOException {
if(s!=null)
{jTextArea1.append(s+"\n");
out.println(s);
out.flush();
}
String line;
if((line=in.readLine())!=null)
jTextArea1.append(line+"\n");
}
然后程序就可以运行了.AUTH LOGIN 我是在一本比较新的书上看到的
刘勇 Java实例入门 中国青年出版社 2002
无论我如何努力,就是出现535 Error: authentication failed.当然我没有用 Base64
base64这个类是我自己封装的,用来把用户名、密码转换为字节码,这在通过服务器验证时时必须的。
如果你不使用它,其结果当然是通不过服务器的验证。就像你遇到的错误一样。
另外,你必须注意你要事先获得通过邮件服务的服务器认证。
比如说先到263注册一个信箱,这样就可以申请获得263的邮件服务。
过几天我把整个程序的源代码全部写上,包括base64.java
因为:我使用JBuilder enterprise 6.0 438.0时遇到一个很奇怪的现象:
在帮助中有这个包javax.mail但是在用的时候
import javax.之后系统给的提示中没有javax.mail
这是为什么,请大家帮忙回答这个问题我也发了帖子。有人的回答为:需要 在 project 属性 path -> libary里面加入 jdk1.3或以上(JBuilder里面带的有) 的 jar文件然而我在project属性里加上了jdek1.4,但是不能用。
在生成project时就会默认用了jdk1.4,我从来没动过它
打import java.后会出io,*等,就是打import javax.后不会出mail,也不知道为什么。
//在本程序中263用户mailsender通过263的邮件发送服务,向地址//[email protected]发送
//邮件,内容为:this is a test for sending mail.
package sendmail;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.net.*;
import java.io.*;
import com.borland.jbcl.layout.*;
//类定义
public class Frame1 extends JFrame {
JPanel contentPane;
XYLayout xYLayout1 = new XYLayout();
JButton jButton1 = new JButton();
java.io.PrintWriter out;
BufferedReader in;
JTextArea jTextArea1 = new JTextArea(); //建立窗体
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}//设置窗体界面
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
jButton1.setText("发送");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
contentPane.setLayout(xYLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
jTextArea1.setVerifyInputWhenFocusTarget(false);
jTextArea1.setLineWrap(true);
jTextArea1.setWrapStyleWord(true);
contentPane.add(jButton1, new XYConstraints(170, 49, -1, -1));
contentPane.add(jTextArea1, new XYConstraints(81, 88, -1, 189));
}
//关闭窗体
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}//邮件发送部分
void jButton1_actionPerformed(ActionEvent e) {
String User="[email protected]";
String Password="123456";
String text="this is a test for sending mail";
try
{Socket s=new Socket("smtp.263.net",25);
out=new java.io.PrintWriter(s.getOutputStream());
in=new BufferedReader(new InputStreamReader(s.getInputStream()));
String hostname=InetAddress.getLocalHost().getHostName();
Send(null);
Send("HELO"+hostname);
Send("AUTH LOGIN");
String tmp=new String(Base64.encode(User.getBytes()));
Send(tmp);
String tp=new String(Base64.encode(Password.getBytes()));
Send(tp);
Send("MAIL FROM:"+"[email protected]");
Send("RCPT TO:"+"[email protected]");
Send("DATA");
out.println(text);
Send(".");
Send("QUIT");
s.close();}
catch(IOException ex)
{jTextArea1.append("error:"+ex);}
}//自定义的send函数,用于发送字符串s
void Send(String s) throws IOException {
if(s!=null)
{jTextArea1.append(s+"\n");
out.println(s);
out.flush();
}
String line;
if((line=in.readLine())!=null)
jTextArea1.append(line+"\n");
}
}//发送邮件时采用的编码方法由自定义的类Base64.java实现。
//其中编码的方法为Base64.encode
package sendmail;
public class Base64 {/**
* returns an array of base64-encoded characters to represent the
* passed data array.
*
* @param data the array of bytes to encode
* @return base64-coded character array.
*/
static public char[] encode(byte[] data)
{
char[] out = new char[((data.length + 2) / 3) * 4];//
// 3 bytes encode to 4 chars. Output is always an even
// multiple of 4 characters.
//
for (int i=0, index=0; i<data.length; i+=3, index+=4) {
boolean quad = false;
boolean trip = false;int val = (0xFF & (int) data[i]);
val <<= 8;
if ((i+1) < data.length) {
val |= (0xFF & (int) data[i+1]);
trip = true;
}
val <<= 8;
if ((i+2) < data.length) {
val |= (0xFF & (int) data[i+2]);
quad = true;
}
out[index+3] = alphabet[(quad? (val & 0x3F): 64)];
val >>= 6;
out[index+2] = alphabet[(trip? (val & 0x3F): 64)];
val >>= 6;
out[index+1] = alphabet[val & 0x3F];
val >>= 6;
out[index+0] = alphabet[val & 0x3F];
}
return out;
}/**
* Returns an array of bytes which were encoded in the passed
* character array.
*
* @param data the array of base64-encoded characters
* @return decoded data array
*/
static public byte[] decode(char[] data)
{
int len = ((data.length + 3) / 4) * 3;
if (data.length>0 && data[len-1] == '=') --len;
if (data.length>0 && data[len-2] == '=') --len;
byte[] out = new byte[len];int shift = 0; // # of excess bits stored in accum
int accum = 0; // excess bits
int index = 0;for (int ix=0; ix<data.length; ix++)
{
int value = codes[ data[ix] & 0xFF ]; // ignore high byte of char
if ( value >= 0 ) { // skip over non-code
accum <<= 6; // bits shift up by 6 each time thru
shift += 6; // loop, with new bits being put in
accum |= value; // at the bottom.
if ( shift >= 8 ) { // whenever there are 8 or more shifted in,
shift -= 8; // write them out (from the top, leaving any
out[index++] = // excess at the bottom for next iteration.
(byte) ((accum >> shift) & 0xff);
} } }
if (index != out.length)
throw new Error("miscalculated data length!");return out;
}//
// code characters for values 0..63
//
static private char[] alphabet =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
.toCharArray();//
// lookup table for converting base64 characters to value in range 0..63
//
static private byte[] codes = new byte[256];
static {
for (int i=0; i<256; i++) codes[i] = -1;
for (int i = 'A'; i <= 'Z'; i++) codes[i] = (byte)( i - 'A');
for (int i = 'a'; i <= 'z'; i++) codes[i] = (byte)(26 + i - 'a');
for (int i = '0'; i <= '9'; i++) codes[i] = (byte)(52 + i - '0');
codes['+'] = 62;
codes['/'] = 63;
}}