20

I am trying to use Bill the Lizard's code to send an email using Google Apps. I am getting this error:

Exception in thread "main" javax.mail.SendFailedException: Sending failed;
  nested exception is: 
    javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first. f3sm9277120nfh.74

    at javax.mail.Transport.send0(Transport.java:219)
    at javax.mail.Transport.send(Transport.java:81)
    at SendMailUsingAuthentication.postMail(SendMailUsingAuthentication.java:81)
    at SendMailUsingAuthentication.main(SendMailUsingAuthentication.java:44)

Bill's code contains the next line, which seems related to the error:

   props.put("mail.smtp.starttls.enable","true");

However, it does not help.

These are my import statements:

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.InternetAddress;
import javax.mail.internet.MimeMessage;

Does anyone know about this error?

Community
  • 1
  • 1
Sergio del Amo
  • 76,835
  • 68
  • 152
  • 179

6 Answers6

20

I found the problem. Previously i was using j2ee.jar to import javax.mail.

I removed j2ee.jar from the classpath and downloaded JavaMail 1.4.1 and put into my classpath two jars, smtp.jar and mailapi.jar. I use now smtps instead smtp

Transport transport = session.getTransport("smtps");            

Now Bill the Lizard's code works.

Sergio del Amo
  • 76,835
  • 68
  • 152
  • 179
17

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"); 
Mahm00d
  • 3,881
  • 8
  • 44
  • 83
Rupal K
  • 171
  • 1
  • 2
  • 1
    seems to be sensible, but why do you mess with port 25? it's mostly always blocked out by ISPs and is actually reset back to 465 several lines later... – Anton Kraievyi Apr 23 '12 at 10:37
9

It is the version of java mail API. I was facing this issue and I just updated the java mail API to 1.4.3 It works fine for me!

Thanks!

Cleber
  • 91
  • 1
  • 1
  • I had the same issue when using 1.4.2, updating to 1.4.3 was not a solution. Not sure is a version problem. – TheGabiRod Jun 22 '18 at 14:23
4

I think it's got to do with using SMTPS instead of SMTP for mail transport. Here's a different version, modeled after the JavaMail FAQ on accessing Gmail. Note that I have left out all of the finer level exception handling for clarity.

private static void send(
        final String username,
        final String password,
        final String recipients,
        final String subject,
        final String body)
        throws Exception
{
    final Session session = Session.getInstance(System.getProperties(), null);
    final Message msg = new MimeMessage(session);
    final String senderEmail = username.contains("@") ? username : (username + "@gmail.com");
    msg.setFrom(new InternetAddress(senderEmail));

    final Address[] recipientAddresses = InternetAddress.parse(recipients);
    msg.setRecipients(Message.RecipientType.TO, recipientAddresses);

    msg.setSentDate(new Date());
    msg.setSubject(subject);
    msg.setText(body);

    final Transport transport = session.getTransport("smtps");
    transport.connect(GMAIL_SMTP_HOST, GMAIL_SMTP_PORT, username, password);
    transport.sendMessage(msg, recipientAddresses);
    transport.close();
}
Zach Scrivena
  • 29,073
  • 11
  • 63
  • 73
  • I get the next exception while running your code: Exception in thread "main" javax.mail.NoSuchProviderException: No provider for smtps – Sergio del Amo Dec 22 '08 at 12:59
  • Seems like the JavaMail resource files are missing or corrupted (see http://java.sun.com/products/javamail/javadocs/javax/mail/Session.html). There are default copies of the files inside mail.jar/META-INF. – Zach Scrivena Dec 22 '08 at 13:11
2

This was driving me crazy and so just wanted to add what worked for me. I had to update my version of JavaMail (1.4.5) for this to work - not sure what version was being used before.

Once I updated to new version of JavaMail, the following code worked for me (can uncomment the debug lines to get additional debugging info - port was 587 and host was smtp.gmail.com):

public void sendMailWithAuth(String host, String user, String password, 
    String port, List<String> toList, String htmlBody, 
        String subject) throws Exception {

    Properties props = System.getProperties();

    props.put("mail.smtp.user",user); 
    props.put("mail.smtp.password", password);
    props.put("mail.smtp.host", host); 
    props.put("mail.smtp.port", port); 
    //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");

    Session session = Session.getInstance(props, null);
    //session.setDebug(true);

    MimeMessage message = new MimeMessage(session);
    message.setFrom(new InternetAddress(user));

    // To get the array of addresses
    for (String to: toList) {
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
    }

    message.setSubject(subject);
    message.setContent(htmlBody, "text/html");

    Transport transport = session.getTransport("smtp");
    try {
        transport.connect(host, user, password);
        transport.sendMessage(message, message.getAllRecipients());
    } finally {
        transport.close();
    }
}
csheets
  • 433
  • 2
  • 4
  • 14
2

try this:

turn on: Allow less secure apps for your gmail account

https://myaccount.google.com/lesssecureapps