日期:2014-05-20  浏览次数:21072 次

java发邮件出错了,帮忙看下~~~~
按书上说的写了个邮件发送的小类,可老是发不出去,大家帮忙看看下.一起两个类,MailSedn.java是发送的,s.java是主类,代码如下:
//MailSend.java:

import   java.io.*;
import   java.text.*;
import   java.net.*;
import   java.util.Properties;
import   java.util.Date;
import   javax.mail.*;
import   javax.mail.internet.*;

public   class   MailSend   {
String   subject   =   null,text   =   null,mailhost   =   null;
public   MailSend(){
mailhost   =   "localhost ";
text   =   getUserText();
subject   =   "Welcome~ ";
}
public   String   sednMsg(String   from,String   to){
boolean   debug   =   false;
String   err   =   null;
try{
Properties   props   =   System.getProperties();
if(mailhost!=null)
props.put( "mail.smtp.host ",   mailhost);
Session   session   =   Session.getDefaultInstance(props,null);
if(debug)
session.setDebug(true);
Message   msg   =   new   MimeMessage(session);

//from   =   null;
if(from   !=   null){
msg.setFrom(new   InternetAddress(from));
System.out.print( "from   is   not   null~ ");
}
else{
msg.setFrom();
System.out.print( "from   is   null~ ");
}
msg.setRecipients(Message.RecipientType.TO,   InternetAddress.parse(to,false));

msg.setSubject(subject);
msg.setText(text);

msg.setSentDate(new   Date());
Transport.send(msg);
System.out.println( "Mail   has   been   send   successfully! ");

}
catch   (Exception   e){
e.printStackTrace();
err   =   e.toString();
err   =   "Mail   send   has   an   error! ";
}
return   err;
}

public   String   getUserText(){
String   userText   =   " ";
String   nowDate   =   DateFormat.getDateInstance().format(new   Date());
userText   =   "hello~ ";
return   userText;
}

}


//S.java


public   class   S   {

/**
  *   @param   args
  */
public   static   void   main(String[]   args)   {
//   TODO   Auto-generated   method   stub
String   mail   =   new   String( "suhuajun2001@163.com ");
MailSend   ms   =   new   MailSend();
ms.sednMsg( "shj0717@gmail.com ",   "suhuajun2001@163.com ");
//ms.sednMsg( " ",   mail);

}

}


错误提示如下:
javax.mail.SendFailedException:   Invalid   Addresses;
    nested   exception   is:
com.sun.mail.smtp.SMTPAddressFailedException:   550   5.7.1   Unable   to   relay   for   suhuajun2001@163.com

at   com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1196)
at   com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:584)
at   javax.mail.Transport.send0(Transport.java:169)
at   javax.mail.Transport.send(Transport.java:98)
at   server.MailSend.sednMsg(MailSend.java:47)
at   server.S.main(S.java:12)