2
votes

I'm trying to send a mail using javamail api using the below code: when I compile the class file I'm getting the below error which says 'must issue starttls command first' I have mentioned the error below. And also getProvider() function error I think so... I don' t know what the errors mean.

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.event.*;
import javax.mail.Authenticator;
import java.net.*;
import java.util.Properties;
public class mailexample 
    {
  public static void main (String args[]) throws Exception {

    String from = args[0];
    String to = args[1];
try
{
Properties props=new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host","smtp.gmail.com");
props.put("mail.smtp.port", "25");
props.put("mail.smtp.auth", "true");
javax.mail.Authenticator authenticator = new javax.mail.Authenticator()
    {
    protected javax.mail.PasswordAuthentication getPasswordAuthentication() 
        {
        return new javax.mail.PasswordAuthentication("[email protected]", "pass");
    }
};
Session sess=Session.getDefaultInstance(props,authenticator);
sess.setDebug (true);
Transport transport =sess.getTransport ("smtp");
Message msg=new MimeMessage(sess);
msg.setFrom(new InternetAddress(from));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
msg.setSubject("Hello JavaMail");
msg.setText("Welcome to JavaMail");
transport.connect();
transport.send(msg);

}
catch(Exception e)
{
System.out.println("err"+e);
}
}
}

error:

C:\Users\bobby\Desktop>java mailexample [email protected] abc@gmail.
com

DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.s
mtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true

DEBUG: SMTPTransport trying to connect to host "smtp.gmail.com", port 25

DEBUG SMTP RCVD: 220 mx.google.com ESMTP q10sm12956046rvp.20

DEBUG: SMTPTransport connected to host "smtp.gmail.com", port: 25

DEBUG SMTP SENT: EHLO bobby-PC
DEBUG SMTP RCVD: 250-mx.google.com at your service, [60.243.184.29]
250-SIZE 35651584
250-8BITMIME
250-STARTTLS
250 ENHANCEDSTATUSCODES


DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.s
mtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true

DEBUG: SMTPTransport trying to connect to host "smtp.gmail.com", port 25

DEBUG SMTP RCVD: 220 mx.google.com ESMTP l29sm12930755rvb.16

DEBUG: SMTPTransport connected to host "smtp.gmail.com", port: 25

DEBUG SMTP SENT: EHLO bobby-PC
DEBUG SMTP RCVD: 250-mx.google.com at your service, [60.243.184.29]
250-SIZE 35651584
250-8BITMIME
250-STARTTLS
250 ENHANCEDSTATUSCODES

DEBUG SMTP SENT: MAIL FROM:
DEBUG SMTP RCVD: 530 5.7.0 Must issue a STARTTLS command first. l29sm12930755rvb
.16

DEBUG SMTP SENT: QUIT
errjavax.mail.SendFailedException: Sending failed;
  nested exception is:
        javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command f
irst. l29sm12930755rvb.16
5
@bobby also see my updates to your earlier question stackoverflow.com/questions/2963625/help-with-javamail-apiJoseK
@bobby - did you try all the steps on the link from Matthew Flaschen?JoseK

5 Answers

3
votes

Probably, you need to put props.put("mail.smtp.starttls.enable","true"); before you authenticate.

0
votes

This code seems to work fine using the Gmail SMTP server to send emails. Note - This does not have an attachment.

(Source : Modified from the example on https://forums.oracle.com/forums/thread.jspa?threadID=1587188)

package org.ssb.mail;

import java.util.Date;
import java.util.Properties;

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

public class MailClient {

/**
 * Entry method
 * 
 * @param args
 *            String[]
 */
public static void main(String[] args) {

    MailClient client = new MailClient();

    try {
        client.sendMail();
    } catch (AddressException ae) {
        ae.printStackTrace();
    } catch (MessagingException me) {
        me.printStackTrace();
    }

}

/**
 * Sends an email
 * 
 * @param none
 */
private void sendMail() throws AddressException, MessagingException {

    // Get a Properties object
    Properties props = System.getProperties();

    // ******************** FOR PROXY ******************

    // props.setProperty("proxySet","true");
    // props.setProperty("socksProxyHost","9.10.11.12");
    // props.setProperty("socksProxyPort","80");
    // props.setProperty("socksProxyVersion","5");

    props.setProperty("mail.smtp.host", "smtp.gmail.com");

    // ******************** FOR SSL ******************
    //final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
    //props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
    //props.setProperty("mail.smtp.socketFactory.fallback", "false");
    //props.setProperty("mail.smtp.port", "465");
    //props.setProperty("mail.smtp.socketFactory.port", "465");

    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.auth", "true");
    props.put("mail.debug", "true");
    props.put("mail.store.protocol", "pop3");
    props.put("mail.transport.protocol", "smtp");
    final String username = "sender-username";
    final String password = "sender-password";
    Session session = Session.getDefaultInstance(props,
            new Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
            });

    // -- Create a new message --
    Message msg = new MimeMessage(session);
    msg.setFrom(new InternetAddress("[email protected]"));
    msg.setRecipients(Message.RecipientType.TO,
            InternetAddress.parse("[email protected]", false));
    msg.setSubject("Hello");
    msg.setSentDate(new Date());

    // **************** Without Attachments ******************
    msg.setText("How are you");


    Transport.send(msg);
    System.out.println("Message sent.");

}

}
0
votes

I had the same problem as you described. In my case, replacing

Session session = Session.getDefaultInstance(props);

with

Session session = Session.getInstance(props);

did the trick. I hope it will be helpful.

0
votes

This work for me. Set the following tags. It will work.

props.put("mail.smtp.user","username"); 
props.put("mail.smtp.host", "smtp.gmail.com"); 
props.put("mail.smtp.port", "25"); 
props.put("mail.debug", "true"); 
props.put("mail.smtp.auth", "true"); 
props.put("mail.smtp.starttls.enable","true"); 
props.put("mail.smtp.EnableSSL.enable","true");

props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");   
props.setProperty("mail.smtp.socketFactory.fallback", "false");   
props.setProperty("mail.smtp.port", "465");   
props.setProperty("mail.smtp.socketFactory.port", "465"); 
0
votes

mail.jar upto 1.4 v dont need props.put("mail.smtp.starttls.enable","true"); later versions it is required to setup.