javamail error: must issue starttls command first

18,318

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

Share:
18,318
simplyblue
Author by

simplyblue

I Hate Programming. I Hate Programming. I Hate Programming. It works! I Love Programming. Java is to JavaScript what Car is to Carpet. XML is like violence - if it's not working for you, you're not using enough of it. It should work!- a programmer's last words. UNIX is simple. It just takes a genius to understand it's simplicity.-- Dennis Ritchie Any fool can write code that a computer can understand. Good programmers write code that humans can understand.-Martin Fowler

Updated on June 04, 2022

Comments

  • simplyblue
    simplyblue almost 2 years

    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