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();
}
}
|