2

I am using java mail api for sending mail. I am able to send mail using Google account but facing some issue sending from my domain using SSL.

Here is the implementation, please let me know where i am lacking.

// Create all the needed properties
        Properties connectionProperties = new Properties();
        // SMTP host
        connectionProperties.put("mail.smtp.host", "abc.example.com");
        // Is authentication enabled
        connectionProperties.put("mail.smtp.auth", "true");
        // Is StartTLS enabled
        connectionProperties.put("mail.smtp.starttls.enable", "true");
        // SSL Port
        connectionProperties.put("mail.smtp.socketFactory.port", "465");
        // SSL Socket Factory class
        connectionProperties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

        // SMTP port, the same as SSL port :)
        connectionProperties.put("mail.smtp.port", "465");

        // Create the session
        Session session = Session.getDefaultInstance(connectionProperties, new javax.mail.Authenticator()
        { // Define the authenticator
            protected PasswordAuthentication getPasswordAuthentication()
            {
                return new PasswordAuthentication("abc@example.net", "abcxdsjdf123");
            }
        });

        System.out.println("done!");

        // Create and send the message
        try
        {
            // Create the message
            Message message = new MimeMessage(session);
            // Set sender
            message.setFrom(new InternetAddress("abc@example.net"));
            // Set the recipients
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("xyz@example.net"));
            // Set message subject
            message.setSubject("Hello from Test");
            // Set message text
            message.setText("This is test mail;)");

            System.out.print("Sending message...");

            // Send the message
             Transport.send(message);

            System.out.println("done!");
        }
        catch (MessagingException e)
        {
            System.out.println(e.toString());
        }

Below is the exception i am getting:

javax.mail.MessagingException: Exception reading response;
  nested exception is:
    java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)

Any sort of help would be appreciable. Thanks in advance.

Jignesh Jain
  • 1,518
  • 12
  • 28

1 Answers1

0

Try this..

   public class SentMail {
        public boolean deliverMail(String username, String email, String subject, String content) {
            boolean sent = false;
            try {
                String delim = "\n\n";
                String text = "Hi ," + delim + content + delim + "Thank you ," + delim + username + "\n" + email;
                String from = "from_email";
                String pass = "from_pass";
                String to = "to_email";
                <!--Your host-->
                String host = "smtp.zoho.com";
                <!--Host port-->
                String port = "465";
                Properties properties = System.getProperties();
                properties.setProperty("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
                properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
                properties.setProperty("mail.smtp.socketFactory.fallback", "false");
                properties.setProperty("mail.smtp.socketFactory.port", port);
                properties.put("mail.smtp.startssl.enable", "true");
                Session session = Session.getDefaultInstance(properties);
                MimeMessage message = new MimeMessage(session);
                message.setFrom(new InternetAddress(from));
                message.addRecipient(Message.RecipientType.TO,
                        new InternetAddress(to));
                message.setSubject(subject);
                message.setText(text);
                Transport transport = session.getTransport("smtp");
                transport.connect(host, from, pass);
                transport.sendMessage(message, message.getAllRecipients());
                transport.close();
                sent = true;
            } catch (Exception e) {

            }
            return sent;

        }
    }
Anoop M Maddasseri
  • 10,213
  • 3
  • 52
  • 73
  • Thanks for the reply but i'm getting javax.mail.MessagingException: Could not connect to SMTP host: abc.magemojo.com, port: 25; nested exception is: javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found. – Jignesh Jain Oct 06 '15 at 11:29
  • my host is example.magemojo.com and port is 465 – Jignesh Jain Oct 06 '15 at 11:38
  • [This JavaMail FAQ entry](http://www.oracle.com/technetwork/java/javamail/faq/index.html#installcert) should help. Also, fix all these other [common mistakes](http://www.oracle.com/technetwork/java/javamail/faq/index.html#commonmistakes). – Bill Shannon Oct 06 '15 at 21:50