qq smtp pop都已成功开启,为什么发送邮件失败!
  去年第一个邮件程序总是失败,今年再试,还是失败!
  等很久,却出现异常:DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.qq.com", port 587, isSSL false
DEBUG SMTP: exception reading response: java.net.SocketException: Connection reset
Exception in thread "main" javax.mail.MessagingException: Exception reading response;
  nested exception is:
java.net.SocketException: Connection reset
at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:1462)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1260)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)
at javax.mail.Service.connect(Service.java:275)
at javamail.FirstMail.main(FirstMail.java:40)public class FirstMail { public static void main(String[] args) throws Exception { // 构造属性
Properties props = new Properties();
// 设置是否认证
props.setProperty("mail.smtp.auth", "true");
// 设置邮件传输协议
props.setProperty("mail.transport.protocol", "smtp");
      
// 根据属性获得一个新的会话实例
Session session = Session.getInstance(props);
// 打印调试信息
session.setDebug(true);
         
// 构造邮件内容
Message msg = new MimeMessage(session);
msg.setText("火影fans正在练习第一个邮件程序");
msg.setFrom(new InternetAddress("[email protected]"));
         
// 发送邮件
Transport transport = session.getTransport();
// qq邮箱POP3服务器(端口995)
// SMTP服务器(端口465或587)
transport.connect("smtp.qq.com", 587, "[email protected]", "password");
//transport.connect("smtp.sohu.com", 25, "[email protected]", "password");
transport.sendMessage(msg, new Address[] { new InternetAddress(
"[email protected]") });
          
// transport.send(msg,new Address[]{new
// InternetAddress("[email protected]")});
// 关闭连接
transport.close();
}}

解决方案 »

  1.   

    1、发送邮件是不需要用户名和密码的,收邮件是需要的。
    2、构造Properties和Session的地方要改给你一个标准的发送邮件代码:
    import java.net.InetAddress;
    import java.util.Properties;import javax.mail.Message;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;public class SendMail {
    /**
     * 发送邮件测试
     */
    public static void main(String[] args) {
    try {
    // Get system properties
    Properties props = System.getProperties();

    // 设置smtp邮件服务器
    props.put("mail.smtp.host", "192.168.0.61");
         props.setProperty("mail.smtp.port", "25");
    props.put("mail.smtp.auth", false);

    // 取得连接
    Session session = Session.getDefaultInstance(props, null);
         Transport transport = session.getTransport("smtp");
    transport.connect();

    // 邮件定义
    MimeMessage mail = new MimeMessage(session);

    mail.addRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
    mail.setSubject("邮件标题……");
    mail.setText("邮件正文……");
    mail.setFrom(new InternetAddress("[email protected]"));
    transport.sendMessage(mail, mail.getAllRecipients());
    Transport.send(message);
    transport.close();

    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
    希望对你有所帮助,记得给分。
      

  2.   

    更正一下,这行代码不要
    Transport.send(message);
      

  3.   

    代码没有错,我实习的时候也碰到过这种情况,你是发送方,用QQ邮箱发送邮件出去(msg.setFrom(new InternetAddress("[email protected]"));),
    因为QQ邮箱(默认是不开启SMTP/POP3的),所以你用QQ邮箱发送邮件会失败,开启SMTP/POP3就OK;
    楼主的发送方也可以换用163邮箱(默认开启SMTP/POP3);
    而接收方是QQ邮箱的话就不会出现这种问题