public static void main(String[] args) {  
String mailServer, From, To, mailSubject, MailContent;        
String username, password;      
Session mailSession;    
     Properties prop;       
Message message;   
username = "kakaxi";    //
        password = "kakaxi";    
        mailServer = "localhost";    
        From = "kakaxi@localhost";    
        To = "mingren@localhost";    
        mailSubject = "Hello Scientist";    
        MailContent = "How are you today!";  
        System.out.println("sending......");   
       
        prop = new Properties();
        prop.put("mail.transport.protocol", "smtp");
        prop.put("mail.smtp.auth", "true");    
        prop.put("mail.smtp.host", mailServer);    
        // 产生新的Session服务    
        //mailSession = mailSession.getDefaultInstance(prop,    
         //       (Authenticator) mailauth);  
        mailSession=Session.getInstance(prop);
        message = new MimeMessage(mailSession);    
   
        try {    
        message.setFrom(new InternetAddress(From)); // 设置发件人    
        message.setRecipient(Message.RecipientType.TO,     
       new InternetAddress(To));// 设置收件人    
        message.setSubject(mailSubject);// 设置主题    
        message.setContent(MailContent, "text/plain");// 设置内容    
        message.setSentDate(new Date());// 设置日期    
        Transport tran = mailSession.getTransport("smtp");    
        tran.connect(mailServer, username, password);    
        tran.send(message, message.getAllRecipients());    
        tran.close();    
        } catch (Exception e) {    
            e.printStackTrace();    
        }     
        System.out.println("finished!");   
  }}
大家看看这段程序没问题吧。。我用james邮件服务器测试的时候总是抛这个javax.mail.AuthenticationFailedException
大家谁惹有能调的通的程序发小弟一用,,

解决方案 »

  1.   

    目前几乎90%的免费邮箱不提供smtp支持.楼主不要在看技术了,多关注下服务商.
    http://topic.csdn.net/u/20081024/17/81c59ed7-ac70-485e-b4c9-145464dd0b59.html上千次点击率见证,下面的程序没问题
    http://blog.csdn.net/sunyujia/archive/2008/06/10/2528696.aspx
      

  2.   

    应该是你服务商需要smtp身份验证.
    可尝试的解决方法:mailSession=Session.getInstance(prop);
    改为:
    mailSession=Session.getInstance(prop, new MyAuthenticator()); 然后创建一个MyAuthenticator类如下:
    public class MyAuthenticator extends Authenticator
    {
    protected PasswordAuthentication getPasswordAuthentication()
    {
    return new PasswordAuthentication("yourUserName", "yourPassword");//这两个参数就是你邮箱smtp身份验证的账号和密码
    }
    }
      

  3.   

    申请了一个新邮箱,试了试,还的不行了!163也太bt了!/*
    250-mail
    250-PIPELINING
    250-AUTH LOGIN PLAIN
    250-AUTH=LOGIN PLAIN
    250 8BITMIME
    DEBUG SMTP: Found extension "PIPELINING", arg ""
    DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN"
    DEBUG SMTP: Found extension "AUTH=LOGIN", arg "PLAIN"
    DEBUG SMTP: Found extension "8BITMIME", arg ""
    DEBUG SMTP: Attempt to authenticate
    AUTH LOGIN
    334 dXNlcm5hbWU6
    dG9tY3Rlc3Q=
    334 UGFzc3dvcmQ6
    YXNkZjEzMzIwNQ==
    550 用户被锁定
    Exception in thread "main" org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.163.com:25
    at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1138)
    at org.apache.commons.mail.Email.send(Email.java:1163)
    at TestMail.sendMain(TestMail.java:16)
    at TestMail.main(TestMail.java:20)
    Caused by: javax.mail.AuthenticationFailedException
    at javax.mail.Service.connect(Service.java:319)
    at javax.mail.Service.connect(Service.java:169)
    at javax.mail.Service.connect(Service.java:118)
    at javax.mail.Transport.send0(Transport.java:188)
    at javax.mail.Transport.send(Transport.java:118)
    at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1128)
    ... 3 more
    */
      

  4.   

    不用程序试,用foxmail或者outlook试就行了,不会你们平时都用在线邮箱吧?
    你恢复正身了.
      

  5.   

    这个是权限验证的问题,就是的用户名和密码不对,你必须要写对你的发送邮件的邮箱,也就是用户名,密码是你的邮箱的密码,发送邮件的服务器必须要是smtp.126.com这种形式的,否则就会报错的.
      

  6.   

    组合着用,在线的也挺方便的,不用什么东西都往本地放。
    是呀,偶又被放出来了,哇嘎嘎。There is no accident!