Topic: 请教高手,我的程序哪有问题? 谢 (JavaMail SMTP 发信权限验证)

  Print this page

1.请教高手,我的程序哪有问题? 谢 (JavaMail SMTP 发信权限验证) Copy to clipboard
Posted by: fatboy2
Posted on: 2003-05-24 10:29

一个简单到极点的程序
可发不出去邮件

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

class SendMail
{public static void main(String[] args){
String msg;
String response;
try {
String port="25";
String host="smtp.mail.yahoo.com.cn";
Socket cs = new Socket(InetAddress.getByName(host),Integer.parseInt(port));
BufferedWriter outData = new BufferedWriter(new OutputStreamWriter(cs.getOutputStream()));
// BufferedReader inData=new BufferedReader(new InputStreamReader(cs.getInputStream()));


msg="HELO "+host;
System.out.println(msg);
for(int i=0;i<msg.length();i++)
outData.write(msg.charAt(i));
outData.write('\r');
outData.write('\n');
outData.flush();




String fromAddress="a_meng2001@sohu.com";
msg = "MAIL FROM:<" + fromAddress + ">" ;
System.out.println(msg);
for(int i=0;i<msg.length();i++)
outData.write(msg.charAt(i));
outData.write('\r');
outData.write('\n');
outData.flush();


String toAddress="a_meng2001@sohu.com";
msg="RCPT TO:<" + toAddress + ">";
System.out.println(msg);
for(int i=0;i<msg.length();i++)
outData.write(msg.charAt(i));
outData.write('\r');
outData.write('\n');
outData.flush();

msg = "DATA";
System.out.println(msg);
for(int i=0;i<msg.length();i++)
outData.write(msg.charAt(i));
outData.write('\r');
outData.write('\n');
outData.flush();

Date sentDate = new Date();
SimpleDateFormat formatter1=
new SimpleDateFormat("EEE,d MMM yyyy hh:mm:ss z");
msg = "Date: " + formatter1.format(sentDate);
System.out.println(msg);
for(int i=0;i<msg.length();i++)
outData.write(msg.charAt(i));
outData.write('\r');
outData.write('\n');
//outData.flush();

msg="From: "+ fromAddress;
System.out.println(msg);
for(int i=0;i<msg.length();i++)
outData.write(msg.charAt(i));
outData.write('\r');
outData.write('\n');
//outData.flush();

msg="To: "+toAddress;
System.out.println(msg);
for(int i=0;i<msg.length();i++)
outData.write(msg.charAt(i));
outData.write('\r');
outData.write('\n');
//outData.flush();

String subject="help";
msg = "Subject: "+subject ;
System.out.println(msg);
for(int i=0;i<msg.length();i++)
outData.write(msg.charAt(i));
outData.write('\r');
outData.write('\n');
//outData.flush();

String content="hello";
msg = content;
System.out.println(msg);
for(int i=0;i<msg.length();i++)
outData.write(msg.charAt(i));
outData.write('\r');
outData.write('\n');
//outData.flush();

outData.write('\r');
outData.write('\n');
outData.write('.');
outData.write('\r');
outData.write('\n');
outData.flush();

msg="QUIT" ;
System.out.println(msg);
for(int i=0;i<msg.length();i++)
outData.write(msg.charAt(i));
outData.write('\r');
outData.write('\n');
outData.flush();

outData.close();
cs.close();
}
catch(IOException ex){
  ex.printStackTrace();
  }
}

}

2.Re:请教高手,我的程序哪有问题? 谢 [Re: fatboy2] Copy to clipboard
Posted by: nothing
Posted on: 2003-05-24 10:35

楼上的,是不是smtp.mail.yahoo.com.cn不支持使用smtp的方式了啊。
试试别的smtp站点,比如: smtp.mail.yahoo.com
或是。。

3.Re:请教高手,我的程序哪有问题? 谢 [Re: fatboy2] Copy to clipboard
Posted by: fatboy2
Posted on: 2003-05-24 10:40

谢了,,

我只想知道我的程序是不是有问题

我换过了也不行

比如 stmp.mail.sohu.com

4.Re:请教高手,我的程序哪有问题? 谢 [Re: fatboy2] Copy to clipboard
Posted by: fatboy2
Posted on: 2003-05-24 11:28

哪位大侠有时间帮我看看了。。

一个java 小菜鸟在哭泣了

这是我的网络编程的开始,

55555,挫折呀

5.Re:请教高手,我的程序哪有问题? 谢 [Re: fatboy2] Copy to clipboard
Posted by: why
Posted on: 2003-05-24 11:34

程式应该没有问题
让小的试试看……
真的没问题哟!
那么多半是大侠的SMTP不行啦!Smile
大侠的ISP是否需要auth呢?

6.Re:请教高手,我的程序哪有问题? 谢 [Re: fatboy2] Copy to clipboard
Posted by: fatboy2
Posted on: 2003-05-24 11:39

楼上的搞错了,
我不是大侠,只是小菜鸟

isp为何物??
什么叫需要 auth

版主救命了。。。。

我忙了两天了,这个小程序就是不干活

7.Re:请教高手,我的程序哪有问题? 谢 [Re: fatboy2] Copy to clipboard
Posted by: nothing
Posted on: 2003-05-24 11:45

就是说,有些smtp服务器,是要发信权限验证的,不是你发到那个25端口就可以了的.

8.Re:请教高手,我的程序哪有问题? 谢 [Re: fatboy2] Copy to clipboard
Posted by: fatboy2
Posted on: 2003-05-24 11:49

噢,我也这样想过。。

但是如何解决这样的问题呢。。。

书上没说过。。。

9.Re:请教高手,我的程序哪有问题? 谢 [Re: fatboy2] Copy to clipboard
Posted by: fatboy2
Posted on: 2003-05-24 13:16

多谢楼上大侠们的指点。

关于这个问题,我已经明白了

smtp 是需要认证的。。。

详细情况在

http://www.cn-java.com/target/news.php?news_id=1511

10.Re:请教高手,我的程序哪有问题? 谢 [Re: fatboy2] Copy to clipboard
Posted by: nothing
Posted on: 2003-05-24 22:22

用javamail进行认证发信 阅读次数4016

出处 CN-JAVA原创:net.fan



[中文JAVA技术网 2002-04-08]

当今mail服务器大多都是通过认证才能发信的,现在的网上介绍javamail发信的文章都没有深入到有关认证的方面,除非自己装一个open relay的mail服务器,但是这样有很危险,本人根据自己工作中用的javamail的方法说一下自己的用法,不对的地方请大家多指教.
  首先设置属性Properties props = new Properties();
props.put("mail.smtp.host",host);
props.put("mail.smtp.auth","true");注意的是此处必须加上true要不然stmp连接的时候不会认证
  用Authenticator写认证类下面是本人的认证类
package org.xxx;
import javax.mail.*;
import javax.mail.internet.*;
public class PopupAuthenticator extends Authenticator{
String username=null;
String password=null;
public PopupAuthenticator(){}
public PasswordAuthentication performCheck(String user,String pass){
username = user;
password = pass;
return getPasswordAuthentication();
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}

}
认证类写完后,在发信的程序中加上
PasswordAuthentication pop = popAuthenticator.performCheck(username,password);
Session mysession=Session.getInstance(props,popAuthenticator);
mailsession加的popAuthenticator
其他的方法和javamail发信的用法相似,在此不累述。

11.Re:请教高手,我的程序哪有问题? 谢 [Re: fatboy2] Copy to clipboard
Posted by: chenyajun5
Posted on: 2003-06-02 22:07

请附上详细代码啦,谢谢!!


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923