简体   繁体   中英

Unable to send mail using zoho smtp server

I am trying to send a mail using Zoho SMTP server using the following code :

 public void sendEmail(Email email) {
        Properties props = setupMailEnv();

        Session session = Session.getDefaultInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication("sssxxxx@xxx.com", "xxxxx");
                    }
                });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(email.getFromMailId()));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(email.getToEmailId()));
            message.setSubject(email.getSubject());
            message.setText(email.getBody());

            Transport.send(message);

            log.info("Mail Sent.");

        } catch (MessagingException e) {
            throw e;
        }

    }

    private Properties setupMailEnv() {
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.zoho.com");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class", javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");
        props.setProperty("mail.smtp.socketFactory.fallback", "false");
        props.put("mail.smtp.startssl.enable", "true");
        props.put("mail.smtp.starttls.enable", "false");
        return props;
    }

When I execute this program it is giving the following exception :

javax.mail.MessagingException: Can't send command to SMTP host;
  nested exception is:
    java.net.SocketException: Connection closed by remote host

I have referred Zoho forums but none of them gave the solution. How can I resolve this issue?

I used the following property to debug the error:

props.put("mail.debug", "true");

The issue was with from address. The from address should match with the address in :

new PasswordAuthentication("sssxxxx@xxx.com", "xxxxx");

I changed it and everything is working fine as expected.

I'm contacting you on behalf of Zoho Mail.

Please set starttls.enable as True and check whether you're able to send mails. Also try using the port number '587' and check whether it works.

If the issue persists, drop an email to support[at]zohomail[dot]com with the complete error logs. Also mention the Zoho email address from which you're trying to send mails. This information will help us assist you further.

Regards, Karthik.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM