Problem sending mail via Javamail

Problem sending mail via Javamail


I am trying to send email via Javamail API. My code is below. But it keeps throwing error.

Output:
DEBUG: setDebug: JavaMail version 1.5.4 
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc] 
DEBUG SMTP: useEhlo true, useAuth true 
DEBUG SMTP: trying to connect to host "smtp.zoho.com", port 587, isSSL false 
220 mx.zohomail.com SMTP Server ready 26 December, 2015 8:16:27 PM PST 
DEBUG SMTP: connected to host "smtp.zoho.com", port: 587 

EHLO -PC 
250-mx.zohomail.com Hello -PC (103.27.48.70 (103.27.48.70)) 
250-STARTTLS 
250 SIZE 52428800 
DEBUG SMTP: Found extension "STARTTLS", arg "" 
DEBUG SMTP: Found extension "SIZE", arg "52428800" 
DEBUG SMTP: use8bit false 
MAIL FROM:< *****> 
530 Must issue a STARTTLS command first. 
DEBUG SMTP: got response code 530, with response: 530 Must issue a STARTTLS command first. 

RSET 
250 state reset OK 
DEBUG SMTP: MessagingException while sending, THROW: 
com.sun.mail.smtp.SMTPSendFailedException: 530 Must issue a STARTTLS command first. 

at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2203) 
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1694) 
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1194) 
at testemail.sendSSLMessage(testemail.java:79) 
at testemail.main(testemail.java:29) 
Exception in thread "main" com.sun.mail.smtp.SMTPSendFailedException: 530 Must issue a STARTTLS command first. 

at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2203) 
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1694) 
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1194) 
at testemail.sendSSLMessage(testemail.java:79) 
at testemail.main(testemail.java:29) 

If I use port 465, it is just stuck at :

DEBUG: setDebug: JavaMail version 1.5.4
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.zoho.com", port 465, isSSL false


***************

import java.security.Security; 
import java.util.Properties; 

import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 

public class testemail { 

/** 
* @param args 
*/ 
private static final String SMTP_HOST_NAME = "smtp.zoho.com"; 
private static final String SMTP_PORT = "587"; 
private static final String emailMsgTxt = "Test Message Contents"; 
private static final String emailSubjectTxt = "A test from gmail"; 
private static final String emailFromAddress = "****"; 
private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; 
private static final String[] sendTo = { "****"}; 


public static void main(String args[]) throws Exception { 
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); 

new testemail().sendSSLMessage(sendTo, emailSubjectTxt, emailMsgTxt, emailFromAddress); 
System.out.println("Sucessfully mail to All Users"); 


public void sendSSLMessage(String recipients[], String subject, 
String message, String from) throws MessagingException { 
boolean debug = true; 


Properties props = new Properties(); 
props.put("mail.smtp.host", "smtp.zoho.com"); 

props.put("mail.smtp.auth", "true"); 
props.put("mail.smtp.port", "587"); 


props.setProperty("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
props.setProperty("mail.smtp.socketFactory.fallback", "false"); 
props.setProperty("mail.smtp.socketFactory.port", "587"); 
props.put("mail.smtp.startssl.enable", "true"); 




Session session = Session.getInstance(props, 
new javax.mail.Authenticator() { 
protected PasswordAuthentication getPasswordAuthentication() { 
return new PasswordAuthentication(emailFromAddress, "***"); 

}); 

session.setDebug(debug); 

Message msg = new MimeMessage(session); 
InternetAddress addressFrom = new InternetAddress(from); 
msg.setFrom(addressFrom); 

InternetAddress[] addressTo = new InternetAddress[recipients.length]; 
for (int i = 0; i < recipients.length; i++) { 
addressTo[i] = new InternetAddress(recipients[i]); 

msg.setRecipients(Message.RecipientType.TO, addressTo); 

// Setting the Subject and Content Type 
msg.setSubject(subject); 
msg.setContent(message, "text/plain"); 
Transport transport = session.getTransport("smtp"); 
//transport.send(msg); 
transport.connect(SMTP_HOST_NAME, 587, emailFromAddress, "*****"); 
transport.sendMessage(msg, addressTo); 
transport.close(); 





      • Sticky Posts

      • Zoho Mail POP & IMAP Server Details

        Hello all! We have been receiving a number of requests regarding the errors while configuring or using Zoho Mail account in POP/ IMAP clients. The server details vary based on your account type and the Datacenter in which your account is setup. Ensure
      • Important update on Group Management | Zoho Mail

        Dear Zoho Mail Community, This post is to inform you all of the following updates on the Group Management in Zoho Mail. Group Member Addition: If a group member is added to an organization group in a Zoho Application, the member will be automatically
      • Mastering email etiquette and best practices to follow in Zoho Mail

        Hello Zoho Mail Community, We’re thrilled to invite you all to our upcoming webinar: Mastering email etiquette and best practices to follow in Zoho Mail. Join our live webinar for expert etiquette tips to enhance your professionalism and elevate your
      • 📣 📣 ZUG Meetups India - Explore Hybrid Workspaces using Zoho Workplace - Meet and Learn! 🧑🏽‍💻🧑🏽‍💻

        We are excited to announce the User Community initiative to meet you all in person and help you explore the need of the hour - Hybrid Workspaces using Zoho Workplace. We aim to help you to setup your Digital Workplace effectively so that your employees
      • Announcing Zoho Mail - Inactive user Policy

        Dear Zoho Mail users, Thanks to all of you for your continuous support.  We have recently started implementing the Inactive User Policy, for Zoho Mail.  A user is considered as inactive, if the user has not logged in to the Zoho mail account for a period of more than 120 days. The login through POP, IMAP and Active Sync are all taken into account for tracking the period of inactivity.  Whoever received the communication need not panic, as we have just communicated our policy to all the users.  To