javax.mail.MessagingException: Unable to load BODYSTRUCTURE
at com.sun.mail.imap.IMAPMessage.loadBODYSTRUCTURE(IMAPMessage.java:1244)
at com.sun.mail.imap.IMAPMessage.getDataHandler(IMAPMessage.java:587)
at javax.mail.internet.MimeMessage.getContent(MimeMessage.java:1380)
at com.email.PMessage.<init>(PMessage.java:58)
at com.struts.action.email.ManagerEmailAction.listonefolder(ManagerEmailAction.java:117)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
  nested exception is:
java.net.SocketException: Connection reset
at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:1462)
at com.sun.mail.smtp.SMTPTransport.close(SMTPTransport.java:645)
at javax.mail.Transport.send0(Transport.java:171)

解决方案 »

  1.   


    import java.util.Properties;import javax.mail.Authenticator;
    import javax.mail.Message;
    import javax.mail.PasswordAuthentication;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.Message.RecipientType;
    import javax.mail.internet.MimeMessage;
    import javax.mail.internet.InternetAddress;public class JavaMailDemo {    public static void main(String[] args) {
                String host = "smtp.qq.com";
                String from = "[email protected]";
                String to = "[email protected]";
                String user = "[email protected]";
                String pwd = "";            // Get system properties
                Properties props = System.getProperties();
                
                // Setup mail server
                props.put("mail.smtp.auth", "true");
                props.put("mail.smtp.host", host);
                
                try {
                    // Get session
                    Session session = Session.getDefaultInstance(props, 
                            new JavaMailDemo().new MyAuthenticator(user, pwd));
        
                    //session.setDebug(true);
        
                    // Define message
                    Message message = new MimeMessage(session);
        
                    // Set the from address
                    message.setFrom(new InternetAddress(from));
        
                    // Set the to address
                    message.addRecipient(RecipientType.TO, new InternetAddress(to));
        
                    // Set the subject
                    message.setSubject("test for java mail");
                    
                    // Set the content
                    message.setText("JavaMail");
        
                    // Send message
                    Transport.send(message);
        //            Transport transport = session.getTransport("smtp");
        //            transport.connect(host, user, pwd);
        //            transport.sendMessage(message, message.getAllRecipients());
        //            transport.close();
                }
                catch (Exception e) {
                    System.err.println("error: " + e.getMessage());
                }
                
                System.err.println("Sending mail successful!!!");
        }
        
        class MyAuthenticator extends Authenticator {
            public MyAuthenticator(String user, String pwd) {
                this.user = user;
                this.pwd = pwd;
            }
            
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(user, pwd);
            }
            
            private String user;
            private String pwd;
        }
    }
      

  2.   

    给个发邮件的例子,供参考,http://mybeautiful.iteye.com/blog/746812