报错 如下:
javax.mail.SendFailedException: Sending failed;
  nested exception is:
class javax.mail.AuthenticationFailedException
at javax.mail.Transport.send0(Transport.java:218)
at javax.mail.Transport.send(Transport.java:80)
at com.test.mail.SendMail.main(SendMail.java:49)
源码:
package com.test.mail;import java.util.Properties;import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;public class SendMail {
 public static void main(String[] args) {
  try {
  
   String username="";
   String password="";
   String smtp_server="smtp.163.com";
   String from_mail_address=username;
   String to_mail_address="[email protected]";
   
   Authenticator auth = new PopupAuthenticator(username,password);
   
   Properties mailProps = new Properties();
   
   mailProps.put("mail.smtp.auth", "true");
   
   mailProps.put("username", username);
   
   mailProps.put("password", password);
   
   mailProps.put("mail.smtp.host", smtp_server);
   Session mailSession = Session.getDefaultInstance(mailProps, auth);
   MimeMessage message = new MimeMessage(mailSession);
   message.setFrom(new InternetAddress(from_mail_address));
   message.setRecipient(Message.RecipientType.TO, new InternetAddress(to_mail_address));
   message.setSubject("Mail Test");
   
   MimeMultipart multi = new MimeMultipart();
   BodyPart textBodyPart = new MimeBodyPart();
   textBodyPart.setText("电子邮件测试内容!");
   multi.addBodyPart(textBodyPart);
   message.setContent(multi);
   message.saveChanges();
   Transport.send(message);
  } catch (Exception ex) {
   ex.printStackTrace();
  }
 }
}class PopupAuthenticator extends Authenticator {
 private String username;
 private String password;
 public PopupAuthenticator(String username,String password){
  this.username=username;
     this.password=password;
 }
 public PasswordAuthentication getPasswordAuthentication() {
  return new PasswordAuthentication(this.username, this.password);
 }

解决方案 »

  1.   

    你看看这个位置怎么了
    SendMail.java:49
      

  2.   

    直接粘贴大段代码,和异常堆栈,不容易查看,请做详细点的描述。
    你看看这个位置怎么了 
    SendMail.java:49
    同意
      

  3.   

    建议楼主用commons-email吧,三个jar包(activation.jar,commons-email-1.0.jar,mail.jar),去官方网站下载即可,附我测试成功的代码:
    package sendmail;
    import org.apache.commons.mail.SimpleEmail;
    import org.apache.commons.mail.MultiPartEmail;
    import org.apache.commons.mail.HtmlEmail;
    import org.apache.commons.mail.EmailAttachment;
    import org.apache.commons.mail.EmailException;import org.apache.commons.lang.StringUtils;import javax.mail.Part;
    import javax.mail.Header;
    import javax.mail.Message;
    import javax.mail.internet.MimeUtility;
    import javax.mail.MessagingException;import java.net.URL;import java.util.Enumeration;import java.io.*;
    public class TestMail{
    private String mail_host     = "smtp.163.com";  //mail服务器地址 
    private String mail_username = "[email protected]";  //发送mail信箱的用户名  [email protected]
    private String mail_username_jx = "邮件列表"; 
    private String mail_password = "111111";  //发送mail信箱的密码  b3c6d3d5
    private String toMail        = "[email protected]";//接收地址 [email protected] [email protected]
    private String toMail_jx        = "徐翠";//接收地址 [email protected] [email protected]
    private String mail_subject  = "20070725Test邮件测试";
    private String sTotalString  = "20070725Test中文正常";
    private String mail_msg = "20070907Test邮件测试";
    /**
     * sendSimpleEmail
     * 功能:发送简单邮件
     */   
        public void sendSimpleEmail()
        {
         try{ 
             SimpleEmail email = new SimpleEmail();
            
             email.setDebug(true);
             email.setAuthentication(mail_username,mail_password);
    email.setHostName(mail_host);
    email.addTo(toMail, toMail_jx);
    email.setFrom(mail_username, mail_username_jx);
    email.setSubject(mail_subject);

    //发送简单邮件
    email.setMsg(new String(mail_subject.getBytes("GBK"),"iso-8859-1"));
    //发送
    email.send();
    }
            catch(Exception e)
            {
                  e.printStackTrace();
            }
        }
       
      

  4.   

     /**
     * sendSimpleEmail
     * 功能:发送带附件的邮件
     */   
        public void sendMultiPartEmail ()
        {
         try{ 
             MultiPartEmail  email = new MultiPartEmail ();
            
             email.setDebug(true);
             email.setAuthentication(mail_username,mail_password);
    email.setHostName(mail_host);
    email.addTo(toMail, toMail_jx);
    email.setFrom(mail_username, mail_username_jx);
    email.setSubject(mail_subject);
    //发送简单邮件
    email.setMsg(new String(mail_subject.getBytes("GBK"),"iso-8859-1"));
    // 创建附件
    EmailAttachment attachment = new EmailAttachment();

    attachment.setPath("c:/tt.jpg");
    // attachment.setURL(new URL("http://www.apache.org/images/asf_logo_wide.gif"));
    attachment.setDisposition(EmailAttachment.ATTACHMENT);
    attachment.setDescription("Picture of John");
    attachment.setName("John");

    EmailAttachment attachment2 = new EmailAttachment();
    File f = new File("c:/中文福建.txt");
    attachment2.setPath(f.getPath());
    attachment2.setDisposition(EmailAttachment.ATTACHMENT);
    attachment2.setDescription(new String("Txt of 中文福建".getBytes("GBK"),"iso-8859-1"));
    //attachment2.setName(new String("中文福建".getBytes("GBK"),"iso-8859-1"));
    attachment2.setName(new String(f.getName().getBytes("GBK"),"iso-8859-1"));

    // add the attachment
                email.attach(attachment);
                email.attach(attachment2);
                  
    //发送
    email.send();
    }
            catch(Exception e)
            {
                  e.printStackTrace();
            }
        }
        /**
     * sendHtmlEmail 
     * 功能:发送HTML格式的邮件
     */   
        public void sendHtmlEmail ()
        {
         try{ 
             HtmlEmail   email = new HtmlEmail  ();
            
             email.setDebug(true);
             email.setAuthentication(mail_username,mail_password);
    email.setHostName(mail_host);
    email.addTo(toMail, toMail_jx);
    email.setFrom(mail_username, mail_username_jx);
    email.setCharset("UTF-8");
        email.setSubject(mail_subject);
    //email.setSubject(new String(mail_subject.getBytes("iso-8859-1"),"GBK"));
    // embed the image and get the content id
    URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");
    String cid = email.embed(url, "Apache logo");
    System.out.println("cid="+cid);
    // set the html message
    email.setHtmlMsg(mail_msg);

    // set the alternative message
    email.setTextMsg("Your email client does not support HTML messages"); email.buildMimeMessage();   
    //设置内容的字符集为UTF-8,先buildMimeMessage才能设置内容文本
    email.getMimeMessage().setText("UTF-8");       //发送
    email.send();
    }
            catch(Exception e)
            {
                  e.printStackTrace();
            }
        }
        //邮件附近中文名字的乱码也可以用这个解决
        private static String emailStringToChinese(String string) throws Exception {      String lowcase = string.toLowerCase();      //System.out.println(string);      if (lowcase.startsWith("=?gb2312") || lowcase.startsWith("=?gbk")                    || lowcase.startsWith("=?utf-8"))             string = MimeUtility.decodeText(string);      else if (lowcase.startsWith("=?unknown")) {                        sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();             byte[] b = decoder.decodeBuffer(string);             string = new String(b);      }             string = new String(string.getBytes("ISO-8859-1"), "gb2312");      return string;       }
         /**该函数用于获取一个消息或者Part的头信息,对于处理各类问题很有帮助**/       public static String getMessageHeaders(Part message) throws EmailException {              String headerStr = "";              try {                     Enumeration headerParts = message.getAllHeaders();                     Header header;                     while (headerParts.hasMoreElements()) {                            header = (Header) headerParts.nextElement();                            headerStr = headerStr + header.getName() + ":"                                          + header.getValue() + "\n";                            // System.out.println(header.getName()+header.getValue());                     }              } catch (MessagingException e) {                     // TODO Auto-generated catch block                     System.out                                   .println("Exception@Class PraseMimeMessage:Method getMessageHeaders ;Exception info:"                                                 + e.getMessage());                     throw new EmailException(e);              }              return headerStr;       }
       
      

  5.   

     //对于邮件主题中的乱码问题,我是采用下面的函数解决的:
        public static String getSubject(Message mimeMessage) throws EmailException {              String subject = null;              try {                     String[] header = mimeMessage.getHeader("Subject");                     if (header != null)                            subject = emailStringToChinese(header[0]);                     else                            subject = "(无标题)";              } catch (Exception e) {                     System.out                                   .println("Exception@Class PraseMimeMessage:Method getSubject ;Exception info:"                                                 + e.getMessage());                     throw new EmailException(e);              }              return subject;       }

        public static String toChinese(String strvalue) {// ISO-8859-1          try {                 if (strvalue == null)                        return null;                 else {                        strvalue = new String(strvalue.getBytes("ISO-8859-1"), "gb2312");                        return strvalue;                 }          } catch (Exception e) {                 e.printStackTrace();                 return null;          }       } /**        * @param path        * @param fileName        * @param input        * @param overcast 是否覆盖已有文件        */       public static void saveFile(String path, String fileName,                     InputStream input, boolean overcast) {              System.out.println("Orignal file name:" + fileName);             /* if (!StringUtils.validateFileName(fileName)) {                     fileName = StringUtils.filNameFormat(fileName);                     System.out.println("Warning:the file name have invalid charactors and have been formated to a valid name!");              }*/              System.out.println("Formated file name:" + fileName);              try {                     File file;                     file = new File(path);
                         if (!file.exists())                            file.mkdirs();
                         // Do no overwrite existing file!好像不需要一定这么做,可以考虑跳过写操作或者,选择覆盖已有文件                     /*file = new File(path + "\\" + fileName);                      int lastDot = fileName.lastIndexOf(".");                      String extension = fileName.substring(lastDot);                      fileName = fileName.substring(0, lastDot);                      int i = 0;                      while (file.exists()) {                      file = new File(path + "\\" + fileName + i + extension);                      i++;                      }*/
                         file = new File(path + "\\" + fileName);                     //System.out.println(file.getAbsolutePath());                     if (!file.exists())                            file.createNewFile();                     if (overcast) {                            OutputStream out = new BufferedOutputStream(                                          new FileOutputStream(file));                            byte[] buf = new byte[8192];                            int len;                            while ((len = input.read(buf)) > 0)                                   out.write(buf, 0, len);                            out.close();                     }              } catch (IOException e) {                     // TODO Auto-generated catch block                     e.printStackTrace();              }       }
      
    public static void main(String[] args) {
            try{
             TestMail testMail= new TestMail();
            
    //发送简单邮件
    testMail.sendSimpleEmail();

    //发送带附件的邮件
    //testMail.sendMultiPartEmail();

    //发送html格式的邮件
    testMail.sendHtmlEmail();
            }
            catch(Exception e)
            {
                  e.printStackTrace();
            }
        }
    public String getMail_host() {
    return mail_host;
    }
    public void setMail_host(String mail_host) {
    this.mail_host = mail_host;
    }
    public String getMail_msg() {
    return mail_msg;
    }
    public void setMail_msg(String mail_msg) {
    this.mail_msg = mail_msg;
    }
    public String getMail_password() {
    return mail_password;
    }
    public void setMail_password(String mail_password) {
    this.mail_password = mail_password;
    }
    public String getMail_subject() {
    return mail_subject;
    }
    public void setMail_subject(String mail_subject) {
    this.mail_subject = mail_subject;
    }
    public String getMail_username() {
    return mail_username;
    }
    public void setMail_username(String mail_username) {
    this.mail_username = mail_username;
    }
    public String getMail_username_jx() {
    return mail_username_jx;
    }
    public void setMail_username_jx(String mail_username_jx) {
    this.mail_username_jx = mail_username_jx;
    }
    public String getSTotalString() {
    return sTotalString;
    }
    public void setSTotalString(String totalString) {
    sTotalString = totalString;
    }
    public String getToMail() {
    return toMail;
    }
    public void setToMail(String toMail) {
    this.toMail = toMail;
    }
    public String getToMail_jx() {
    return toMail_jx;
    }
    public void setToMail_jx(String toMail_jx) {
    this.toMail_jx = toMail_jx;
    }}
      

  6.   

    49行就是这个代码:Transport.send(message);indeed说的那个方法现在试验不了,公司下载不了东西 只能回家再试了。
      

  7.   

    163是不行换了个sina的也不好用
      

  8.   

    indeed那个方法也试了  还是验证失败。。
    疯了
      

  9.   

    [email protected]你有权限使用smtp吗,你在foxmail试试outlook也行,不是随便一个163账户就行了,
    1。积分要够
    2。老会员我认为是这个原因,没多看大概一看猜是这个问题。
      

  10.   

    sunyujia:
    我换了sina的邮箱了  还是不好用
    package com.test.mail;import java.util.*;import javax.mail.Message;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeBodyPart;
    import javax.mail.internet.MimeMessage;
    import javax.mail.internet.MimeMultipart;import com.sun.mail.smtp.SMTPTransport;public class Mail{
        public static void main(String[] args){
            Mail a = new Mail();
            a.ssss();
        }
        public void ssss(){
            String from =null;
            String to =null;
            String title =null;
            String content =null;
            try{
                from = "[email protected]";//sendEmailForm.getEmail_useName().trim();//sendMailForm.getFrom().trim();
                to = "[email protected]";//sendEmailForm.getTo().trim();
                title = "[email protected]";//sendEmailForm.getTitle();
                content = "[email protected]";//sendEmailForm.getContent();
                 Properties props = System.getProperties();
                  props.put("mail.smtp.host", "smtp.sina.com.cn");
                  props.put("mail.smtp.auth", "true");
                  javax.mail.Session mailSession = javax.mail.Session.getInstance(props, null);
                  MimeBodyPart   mbp1=   new   MimeBodyPart();
                  mbp1.setContent(content,"text/html");
                  Message msg = new MimeMessage(mailSession);
                  msg.setFrom(new InternetAddress("[email protected]"));
                  msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to, false));
                  msg.setSubject(title);
                  msg.setContent(content,"text/html;charset=UTF-8");
                  //msg.setText(content);
                  msg.setSentDate(new java.util.Date());
                  
                  MimeMultipart   mp   =   new   MimeMultipart("related");//alternative   
                  mp.addBodyPart(mbp1);   
                  msg.setContent(mp);   
                  
                  SMTPTransport t = (SMTPTransport)mailSession.getTransport("smtp");  
                  t.connect("smtp.sina.com.cn", "[email protected]", "*********");  
                  t.sendMessage(msg, msg.getAllRecipients());  
                  t.close();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }错误提示:
    javax.mail.MessagingException: Could not connect to SMTP host: smtp.sina.com.cn, port: 25;
      nested exception is:
    java.net.ConnectException: Connection refused: connect
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1008)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:197)
    at javax.mail.Service.connect(Service.java:233)
    at javax.mail.Service.connect(Service.java:134)
    at com.test.mail.Mail.ssss(Mail.java:47)
    at com.test.mail.Mail.main(Mail.java:16)
      

  11.   

    http://blog.csdn.net/AWUSOFT/archive/2008/04/16/2297839.aspx
      

  12.   

    晕,难到我的帖子沉掉了
    http://topic.csdn.net/u/20080518/22/5a21ffa0-1b72-4298-ab7f-24e096ff86de.html好久不见AWUSOFT 
      

  13.   

    可能是反向email地址解析不通过吧!我的站点http://www.webkaifa.com,有时候也报告这个错误,为了防止垃圾邮件用的!
      

  14.   

    我在163测试通过,我的那个行的,不过介于这个帖子分比较多,决定给你写个public class SendMail extends Authenticator {
    private String userName;
    private String pwd; public SendMail(String userName, String pwd) {
    super();
    this.userName = userName;
    this.pwd = pwd;
    } protected PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication(userName, pwd);
    } public static void main(String args[]) {
    try {
    String to = "[email protected]";
    String from = "[email protected]";
    String subject = "测试"; Properties props = System.getProperties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.host", "smtp.163.com");
    SendMail sa = new SendMail("newsunyujia", "密码");
    Session sess = Session.getInstance(props, sa); Message msg = new MimeMessage(sess);
    msg.setFrom(new InternetAddress(from));
    msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(
    to, false));
    msg.setSubject(subject);
    msg.setSentDate(new Date());
    msg.setText("你好");
    Transport.send(msg);
    } catch (Exception e) {
    System.out.println(e);
    }
    }
    }
      

  15.   

    Properties props = new Properties();
    new 一个也行纠正了你的错误public class SendMail {
    public static void main(String[] args) {
    try { String username = "newsunyujia";
    String password = "密码";
    String smtp_server = "smtp.163.com";
    String from_mail_address = "[email protected]";//错在这
    String to_mail_address = "[email protected]"; Authenticator auth = new PopupAuthenticator(username, password); Properties mailProps = new Properties(); mailProps.put("mail.smtp.auth", "true"); // mailProps.put("username", username);
    // mailProps.put("password", password); mailProps.put("mail.smtp.host", smtp_server);
    Session mailSession = Session.getDefaultInstance(mailProps, auth);
    MimeMessage message = new MimeMessage(mailSession);
    message.setFrom(new InternetAddress(from_mail_address));
    message.setRecipient(Message.RecipientType.TO, new InternetAddress(
    to_mail_address));
    message.setSubject("Mail Test"); MimeMultipart multi = new MimeMultipart();
    BodyPart textBodyPart = new MimeBodyPart();
    textBodyPart.setText("电子邮件测试内容!");
    multi.addBodyPart(textBodyPart);
    message.setContent(multi);
    message.saveChanges();
    Transport.send(message);
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    }
    }class PopupAuthenticator extends Authenticator {
    private String username;
    private String password; public PopupAuthenticator(String username, String password) {
    this.username = username;
    this.password = password;
    } public PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication(this.username, this.password);
    }
    }