H
h_o_u_l_i
Unregistered / Unconfirmed
GUEST, unregistred user!
我在学习javamail api照书写了一个程序但无法执行, 代码如下:
import javax.mail.*;
import javax.activation.*;
import javax.mail.internet.*;
import java.util.*;
class EmailAuthen
ticator_ extends Authen
ticator
{
private String m_username=null;
private String m_userpass=null;
void setUsername(String username)
{
m_username=username;
}
void setUserpass(String userpass)
{
m_userpass=userpass;
}
//构造函数
EmailAuthen
ticator_(String username, String userpass)
{
super();
setUsername(username);
setUserpass(userpass);
}
public PasswordAuthen
tication getPasswordAuthen
tication()
{
return new PasswordAuthen
tication(m_username,m_userpass);
}
}
public class newSendMail
{
public static void main(String args[])
{
//设置邮件服务器及发件和收件地址;
String host="smtp.yahoo.com";
String from="MyMailBox@yahoo.com.cn";
String to="MyMailBox@yahoo.com.cn";
//设置用户信息
String username="MyUsername";
String password="MyPassword";
EmailAuthen
ticator_ emailAuth=new EmailAuthen
ticator_(username,password);
try
{
Properties props=System.getProperties();
//设置邮件服务器
props.put("mail.smtp.host",host);
//此语句用于指明当前使用的邮件服务器是需要认证的服务器;
//?请问我如何知道Properties中有 mail.smtp.host, mail.smtp.auth项??????????????????
props.put("mail.smtp.auth","true");
Session session=Session.getDefaultInstance(props,(Authen
ticator)emailAuth);
session.setDebug(true);//要观察传到邮件服务器上的邮件命令
MimeMessage message=new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject("你好JavaMail");
message.setText("欢迎来到JavaMail的世界");
message.saveChanges();
Transport transport=session.getTransport("smtp");
transport.connect(host,username,password);
transport.sendMessage(message,message.getAllRecipients());
transport.close();
System.out.println("邮件发送成功!");
}
catch(Exception e)
{
System.out.println("邮件发送错误!原因:/n==============================");
e.printStackTrace();
}
}
}
程序编译没有问题,但运行时错误显示:
javax.mail.MessagingException: Could not connect to SMTP host: smtp.yahoo.com, port: 25;
nested exception is:
java.net.ConnectException: Connection timed out: connect at
com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:867) at
com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:156) at javax.mail.Service.connect(Service.java:234) at
javax.mail.Service.connect(Service.java:135) at newSendMail.main(newSendMail.java:70)
问题:
请问我在调试此类程序时是否还要进行什么系统设置,比如SMTP,pop3服务器设置.我用的是win2k server+JBuilder,并在调试过程中连接到
internet上.我该怎么办?希望大家指教!
import javax.mail.*;
import javax.activation.*;
import javax.mail.internet.*;
import java.util.*;
class EmailAuthen
ticator_ extends Authen
ticator
{
private String m_username=null;
private String m_userpass=null;
void setUsername(String username)
{
m_username=username;
}
void setUserpass(String userpass)
{
m_userpass=userpass;
}
//构造函数
EmailAuthen
ticator_(String username, String userpass)
{
super();
setUsername(username);
setUserpass(userpass);
}
public PasswordAuthen
tication getPasswordAuthen
tication()
{
return new PasswordAuthen
tication(m_username,m_userpass);
}
}
public class newSendMail
{
public static void main(String args[])
{
//设置邮件服务器及发件和收件地址;
String host="smtp.yahoo.com";
String from="MyMailBox@yahoo.com.cn";
String to="MyMailBox@yahoo.com.cn";
//设置用户信息
String username="MyUsername";
String password="MyPassword";
EmailAuthen
ticator_ emailAuth=new EmailAuthen
ticator_(username,password);
try
{
Properties props=System.getProperties();
//设置邮件服务器
props.put("mail.smtp.host",host);
//此语句用于指明当前使用的邮件服务器是需要认证的服务器;
//?请问我如何知道Properties中有 mail.smtp.host, mail.smtp.auth项??????????????????
props.put("mail.smtp.auth","true");
Session session=Session.getDefaultInstance(props,(Authen
ticator)emailAuth);
session.setDebug(true);//要观察传到邮件服务器上的邮件命令
MimeMessage message=new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject("你好JavaMail");
message.setText("欢迎来到JavaMail的世界");
message.saveChanges();
Transport transport=session.getTransport("smtp");
transport.connect(host,username,password);
transport.sendMessage(message,message.getAllRecipients());
transport.close();
System.out.println("邮件发送成功!");
}
catch(Exception e)
{
System.out.println("邮件发送错误!原因:/n==============================");
e.printStackTrace();
}
}
}
程序编译没有问题,但运行时错误显示:
javax.mail.MessagingException: Could not connect to SMTP host: smtp.yahoo.com, port: 25;
nested exception is:
java.net.ConnectException: Connection timed out: connect at
com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:867) at
com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:156) at javax.mail.Service.connect(Service.java:234) at
javax.mail.Service.connect(Service.java:135) at newSendMail.main(newSendMail.java:70)
问题:
请问我在调试此类程序时是否还要进行什么系统设置,比如SMTP,pop3服务器设置.我用的是win2k server+JBuilder,并在调试过程中连接到
internet上.我该怎么办?希望大家指教!