以下是源码:
SmtpAuth.java文件
------
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class SmtpAuth{
        String from="[email protected]"; //发件人地址
String smtp=""; //SMTP服务器
String user="administrator"; //用户名
String pass="lxhflj760824"; //密码
String to=""; //收件人地址
String subject="user password"; //邮件标题
String body=""; //邮件正文
//设置发件人地址
public void setFrom(String from){
this.from=from;
}
        public String getFrom(){
        return this.from;
        }
//设置SMTP服务器
public void setSmtp(String smtp){
this.smtp=smtp;
}
        public String getSmtp(){
        return this.smtp;
        } //设置用户名
public void setUser(String user){
this.user=user;
}
        public String getUser(){
        return this.user;
        } //设置密码
public void setPass(String pass){
this.pass=pass;
}
        public String getPass(){
        return this.pass;
        }
//设置收件人地址
public void setTo(String to){
this.to=to;
}
        public String getTo(){
        return this.to;
        }
//设置邮件标题
public void setSubject(String subject){
try{
this.subject=new String(subject.getBytes("iso-8859-1"),"gb2312");
}catch(Exception uee){
//捕捉UnsupportedEncodingException
}
}        public String getSubject(){
                      return  this.subject;
        } //设置邮件正文
public void setBody(String body){
try{
this.body=new String(body.getBytes("iso-8859-1"),"gb2312");
}catch(Exception uee){
//捕捉UnsupportedEncodingException
}
}
        public String getBody(){
        return this.body;
        }
//发送邮件函数
public boolean send(){
try{
//获得系统属性
Properties props=System.getProperties();
//实例SMTP认证对象
SmtpAuthenticator sa=new SmtpAuthenticator(user,pass);
//设置SMTP服务器
props.put("mail.smtp.host", smtp);
//设置需要身份认证为真
props.put("mail.smtp.auth","true");
//获得一个会话
Session session=Session.getInstance(props,sa);
//生成邮件对象
Message msg = new MimeMessage(session);
//设置发件人
msg.setFrom(new InternetAddress(from));
//设置收件人地址
msg.setRecipients(Message.RecipientType.TO,
  InternetAddress.parse(to,false));
//设置邮件标题
msg.setSubject(subject);
//设置邮件正文
msg.setText(body);
//设置发信时间为系统当前时间
msg.setSentDate(new Date());
//发送邮件return
Transport.send(msg);
return true;
}catch(Exception e){
e.printStackTrace();
return false;
}
}
}

解决方案 »

  1.   

    可能是你换了一台机器不能访问你的mail服务器?
      

  2.   

    SmtpAuthenticator.java
    ---------
    package research.logic;import javax.activation.DataHandler;
    import javax.activation.FileDataSource;
    import javax.mail.*;
    import javax.mail.internet.*;public class SmtpAuthenticator extends javax.mail.Authenticator
    {
    String username; //用户名
    String password; //密码
    //实例化这个类时设置用户名和密码
    public SmtpAuthenticator(String username, String password)
    {
    this.username = username;
    this.password = password;
    } //重载getPasswordAuthentication函数
    protected PasswordAuthentication getPasswordAuthentication()
    {
    return new PasswordAuthentication(username, password);
    }
    }---------
    前台JSP文件如下:
    mailform.jsp
    package research.logic;import javax.activation.DataHandler;
    import javax.activation.FileDataSource;
    import javax.mail.*;
    import javax.mail.internet.*;public class SmtpAuthenticator extends javax.mail.Authenticator
    {
    String username; //用户名
    String password; //密码
    //实例化这个类时设置用户名和密码
    public SmtpAuthenticator(String username, String password)
    {
    this.username = username;
    this.password = password;
    } //重载getPasswordAuthentication函数
    protected PasswordAuthentication getPasswordAuthentication()
    {
    return new PasswordAuthentication(username, password);
    }
    }----
    sendmail.jsp<%@ page contentType="text/HTML;charset=GB2312"%>
    <jsp:useBean id="mailBean" scope="page" class="research.logic.SmtpAuth"/>
    <jsp:setProperty name="mailBean" property="*"/>
    <%
    mailBean.setTo("[email protected]");
    mailBean.setSubject("ccccc");
    mailBean.setBody("cccceeee");
    try{
    //调用send函数发送邮件
    if(mailBean.send())
    out.print("Send mail success. ");
    else
    out.print("Send mail failed.");
    }catch(NullPointerException npe){
    }catch(Exception e){
    out.println(e);
    }
    %>
      

  3.   

    javax.mail.AuthenticationFailedException服务器SMTP认证失败!
    访问的程序不太对,改用下你的邮箱帐号及登陆邮箱的密码访问方式
    具体请登陆上http://www.cn-java.com/index_focus.php?kind=22&title=JavaMail
    有相关的文章及源码,看看就配OK了!