public class testJavaMail { /**
 * @param args
 */
 public final static String SMTPSERVER = "smtp.163.com";
     public final static String POPSERVER = "pop3.163.com";
     public final static String ACCOUNT = "ywasp";
     public final static String PWD = "xx";
     public final static String MAILADDR = "[email protected]";   //发件人
     public final static String ToMAILADDR = "[email protected]"; //收件人
     
     public void sendMail(String to, String from, String subject, String body) throws AddressException, MessagingException {
             Properties pro = System.getProperties();
             pro.put("mail.smtp.host", SMTPSERVER);
             pro.put("mail.smtp.auth", "true");
             Session session = Session.getDefaultInstance(pro, null);
             
             Message msg = new MimeMessage(session);
             msg.setFrom(new InternetAddress(from));
             msg.setRecipient(Message.RecipientType.TO, InternetAddress.parse(to, false)[0]);
             msg.setSubject(subject);
             msg.setText(body);
             
             msg.setContent("测试(邮件内容)", "text/plain;charset=gb2312");
             msg.setHeader("X-Mailer", "LOTONtechEmail");
             msg.setSentDate(new Date());
             
             
             Transport transport = session.getTransport("smtp");
             System.out.println("connecting...");
             transport.connect(SMTPSERVER, ACCOUNT, PWD);
             System.out.println("Sending message");
             transport.sendMessage(msg, msg.getAllRecipients());
             
             System.out.println("Send Success!");
             transport.close();
     }
     
     public static void main(String[] args) {
      testJavaMail test = new testJavaMail();
             try {
                     test.sendMail(ToMAILADDR, MAILADDR, "邮件标题!!!!!!!!!", "我的一个测试");
             } catch (AddressException e) {
                     e.printStackTrace();
             } catch (MessagingException e) {
                     e.printStackTrace();
             }
     }
}
控制台输出connecting...
javax.mail.MessagingException: Could not connect to SMTP host: smtp.163.com, port: 25;
  nested exception is:
java.net.SocketException: Software caused connection abort: 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 test.testJavaMail.sendMail(testJavaMail.java:43)
at test.testJavaMail.main(testJavaMail.java:54)

解决方案 »

  1.   

    你是不是在局域网里面啊!网关也许不让直接连接  smtp.163.com 25 端口!
      

  2.   

    我用的是apache的commons-email组件,使用很简单。
    用自己的邮箱就可以,用163就会提示拒绝连接:
    javax.mail.MessagingException: Could not connect to SMTP host: mail.163.com, port: 25;
    ...
    java.net.ConnectException: Connection refused: connect
    ...package com.fastunit.demo.mail;import org.apache.commons.mail.SimpleEmail;public class MailTest {  public static void sendMail() {
        String host = "mail.fastunit.com";
        String charset = "UTF-8";
        String username = "[email protected]";
        String password = "***";
        String to = "[email protected]";
        String from = "[email protected]";
        String fromName = "support";
        String subject = "subject标题";
        String msg = "msg内容";
        try {
          SimpleEmail email = new SimpleEmail();
          email.setHostName(host);
          email.setAuthentication(username, password);
          // 必须放在setMsg()前面,否则乱码
          email.setCharset(charset);
          email.addTo(to);
          email.setFrom(from, fromName);
          email.setSubject(subject);
          email.setMsg(msg);
          email.send();
          System.out.println("successfull");
        } catch (Exception e) {
          e.printStackTrace();
        }
      }}
      

  3.   

    pro.put("mail.smtp.auth",   "true"); 既然是允许验证,那么你下面这么写就不对了
    Session   session   =   Session.getDefaultInstance(pro,   null); 换成这样试试[code=Java
    Session session = Session.getDefaultInstance(pro, new Authenticator() {
    public PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication(ACCOUNT, PWD);
    }
    });
    ][/code]
      

  4.   

    晕,想让用JAVA CODE显示的,结果那个中间居然弄做了换成这样试试
    Session session = Session.getDefaultInstance(pro, new Authenticator() {
    public PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication(ACCOUNT, PWD);
    }
    });
      

  5.   

    Wじ☆F贝o 帖代码的位置搞错了 我帮你
    Session   session   =   Session.getDefaultInstance(pro,   new   Authenticator()   { 
    public   PasswordAuthentication   getPasswordAuthentication()   { 
    return   new   PasswordAuthentication(ACCOUNT,   PWD); 

    });
      

  6.   

    验证了一下,163应当可以发送的。import javax.mail.*;
    import javax.mail.internet.*;
    import java.util.*;
    class MyAuthenticator extends javax.mail.Authenticator 
    {
    private String strUser;private String strPwd;public MyAuthenticator(String user, String password) 
    {
    this.strUser = user;
    this.strPwd = password;
    }
    }public class MyMail
    {
    String from="你的163邮箱,to="任意邮箱",host="smtp.163.com";
    MyMail()
    {
    try{Properties props=System.getProperties();
    props.put("mail.smtp.host",host);
    props.put("mail.smtp.auth",   "true");
    Authenticator auth=new MyAuthenticator("","");
    Session session=Session.getDefaultInstance(props,auth);
    MimeMessage message=new MimeMessage(session);
    message.setFrom(new InternetAddress(from));
    //message.setAuthenticator(auth);
    message.addRecipient(Message.RecipientType.TO,new InternetAddress(to,false));
    message.setSubject("Hello!");
    message.setText("This is a Java Mail.");
    message.saveChanges(); Transport trans=session.getTransport("smtp");
    trans.connect(host,"你的163用户名","你的密码");
    trans.sendMessage(message,message.getAllRecipients());
    System.out.println("mail is sent");
    }
     catch   (AddressException   e)   {
                                              e.printStackTrace();
                              }   catch   (MessagingException   e)   {
                                              e.printStackTrace();
                              }
           
    }
    public static void main(String args[])
    {
    new MyMail();
    }

    }
      

  7.   

    我已经测试正确发送了邮件。程序和163的邮件服务器应当都没有问题。如果楼主仍然运行不了上述程序,那问题肯定是出在楼主的上网方式上,也就是说,楼主连通intenet的网关或者防火墙可能封锁了它。