javax.mail.MessagingException的

时间:2012-03-31 18:47:41

标签: java-ee

为什么我会收到此异常?请建议任何解决方案。我已将mail.jar和activation.jar正确复制到类路径。

javax.mail.MessagingException: Could not connect to SMTP host: your.smtp.host, port: 25;
  nested exception is:
    java.net.SocketException: Software caused connection abort: connect
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1545)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:453)
    at javax.mail.Service.connect(Service.java:291)
    at javax.mail.Service.connect(Service.java:172)
    at javax.mail.Service.connect(Service.java:121)
    at javax.mail.Transport.send0(Transport.java:190)
    at javax.mail.Transport.send(Transport.java:120)
        ...

Caused by: java.net.SocketException: Software caused connection abort: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
    at java.net.Socket.connect(Socket.java:519)
    at java.net.Socket.connect(Socket.java:469)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:267)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:227)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1511)
    ... 40 more

1 个答案:

答案 0 :(得分:1)

我猜你没有提供你的SMTP连接。 在身份验证中检查您的smtp设置.. !!

第一行说明了一切:

javax.mail.MessagingException: Could not connect to SMTP host: your.smtp.host, port: 25;
  nested exception is:

而不是 your.smtp.host 必须有你喜欢的smtp的网址,如 smtp.gmail.com

编辑:示例gmail连接代码

Session session = Session.getDefaultInstance(mailProperties, new javax.mail.Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(mailProperties.getProperty("mail.smtp.from"), "mail.smtp.password");
            }
        });
        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("xyz@gmail.com"));
            InternetAddress address[] = new InternetAddress[4];
            address = InternetAddress.parse(toMail, false);
            message.setRecipients(Message.RecipientType.TO, address);
            address = InternetAddress.parse("reciepents@xyz.com", false);
            message.addRecipients(Message.RecipientType.CC, address);
            message.setSubject(subject);

            BodyPart messageBodyPart = new MimeBodyPart();
            messageBodyPart.setText(body);
            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(messageBodyPart);
            Transport transport = session.getTransport("smtps");
            transport.connect("smtp.gmail.com", 465, "xyz@gmail.com", "pasword here");
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
        } catch (MessagingException e) {
            e.printStackTrace();
        }