遇到了java smtp 邮件问题,哪位高手能够相助啊?

04-05-05 hgwnet
为什么smtp的DATA指令与其结束标志.之间无法执行任何动作呢,哪怕是设置邮件标题???具体错误显示如下相关中文注释。

完整代码如下:

so = new Socket("218.79.108.205", 25);

send = new PrintWriter(so.getOutputStream(), true);

recv = new BufferedReader(new InputStreamReader(so.getInputStream()));

String s1 = recv.readLine();

char c = s1.charAt(0);

if ((c == '4') | (c == '5'))

System.out.println("error");

System.out.println("Socket send cmd:" + "HELO " + "gb.com");

send.println("HELO " + "gb.com");

System.out.println("Socket received msg:" + recv.readLine());

System.out.println("Socket send cmd:" + "MAIL FROM:" + "test@localhost");

send.println("MAIL FROM:" + "test@localhost");

System.out.println("Socket received msg:" + recv.readLine());

System.out.println("Socket send cmd:" + "RCPT TO:" + "hgwstart@hotmail.com");

send.println("RCPT TO:" + "hgwstart@hotmail.com");

System.out.println("Socket received msg:" + recv.readLine());

System.out.println("Socket send cmd:" + "DATA");

send.print("DATA");

// System.out.println("Socket received msg:" + recv.readLine());这条测试语句加入后运行变成无限等待相应。

// send.print("Subject: " + "test subject" + "\r");这条语句加入后系统提示:502 Unknown command,然后运行中断。

send.print("\n.\n");

send.println("QUIT");

System.out.println("Socket received msg:" + recv.readLine());

System.out.println("End...");

//上述程序可以直接拷贝到编译运行,但前提是要本机运行一个smtp server,例如ArGoSoft mail server

hgwnet
2004-05-05 20:12
备注:so = new Socket("218.79.108.205", 25);语句中的218.79.108.205是本机的adsl网络地址。

cxj_2000
2004-05-06 15:26
/**

* 发送邮件

* @param pw

* @param message 邮件

* @param bf

* @return true 发送成功 false 发送失败

*/

private boolean sendMail(PrintWriter pw, Message message, BufferedReader bf) {

try {

String from = null;

if(message.getSender() != null) from = message.getSender().getAddress();

if(from == null && message.getFrom() != null && message.getFrom().length >= 1) {

from = message.getFrom()[0].getAddress();

} else throw new RuntimeException("message sender not exist!");

send(pw, new StringBuffer(MAIL_FROM).append(" ")

.append("<").append(from)

.append(">").toString());

if(!checkStatus(getServerResponseStr(bf), SERVER_SUCCESS_STATUS[1])) return false;

String[] toArray = getToArray(message);

for(int i = 0; i< toArray.length; i++) {

send(pw, new StringBuffer(RCPT_TO).append(" ")

.append("<").append(toArray)

.append(">").toString());

if(!checkStatus(getServerResponseStr(bf), SERVER_SUCCESS_STATUS[1])) return false;

}

send(pw, DATA);

if(!checkStatus(getServerResponseStr(bf), SERVER_SUCCESS_STATUS[3])) return false;

BufferedReader fin = new BufferedReader(new FileReader(

message.getPath() + File.separator + message.getFileName()));

String readLine = null;

while((readLine = fin.readLine()) != null) {

if(readLine.startsWith(Constants.DOT))

readLine = Constants.DOT + readLine;//点填充

send(pw, readLine);

}

fin.close();//关闭读邮件句柄

fin = null;

send(pw, Constants.DOT);

if(!checkStatus(getServerResponseStr(bf), SERVER_SUCCESS_STATUS[1])) return false;

send(pw, QUIT); //关闭连接

return true;

} catch (Exception ex) {

m_logger.warning("send message fail!", ex);

}

return false;

}

hgwnet
2004-05-07 18:56
不过还有两个难题:

1、该如何用smtp来检测邮件地址(如test@test.com

)是否正确或是否存在呢?似乎所有的邮件如sina.com.cn都不支持smtp协议的VRFY指令,用javamail一样也没有任何有关邮址校验的提示。

2、用javamail发邮件很不稳定,常出现错误:javax.mail.MessagingException: 554 Error sending mail,查了很多资料,搞不清楚这到底是什么意思,似乎是目标host有问题,但这又怎么可能经常出现呢?!

不知道哪位仁兄能够告知一二呀。

猜你喜欢