user1305398
user1305398

Reputation: 3690

javax.mail.messagingexception

why do I get this exception?please suggest any solutions.i have properly copied mail.jar and activation.jar to the class path.

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

Upvotes: 0

Views: 2425

Answers (1)

Anuj Patel
Anuj Patel

Reputation: 17869

You havent provided your SMTP connection right i guess. Check your smtp settings in authentication..!!

The first line says it all :

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

instead of your.smtp.host there must be url of the smtp you ar eusing like smtp.gmail.com etc

EDIT : sample gmail connection code

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("[email protected]"));
            InternetAddress address[] = new InternetAddress[4];
            address = InternetAddress.parse(toMail, false);
            message.setRecipients(Message.RecipientType.TO, address);
            address = InternetAddress.parse("[email protected]", 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, "[email protected]", "pasword here");
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
        } catch (MessagingException e) {
            e.printStackTrace();
        }

Upvotes: 1

Related Questions