1

It give error about some problem with STARTTLS command . it gives the error like this

javax.mail.sendfailedexception sending failed nested exception is class javax.mail.MessagingException So please help me out of it.

Java Code

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


public class MailDemo {
    public static void main(String[] args) {
        Properties properties=new Properties();
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable","true");
        properties.put("mail.smtp.host","smtp.gmail.com");
        properties.put("mail.smtp.port", "587");
        Scanner scn=new Scanner(System.in);
        System.out.println("Username for Authentication :");
        final String username=scn.nextLine();
        System.out.println("Password for Authentication :");
        final String password=scn.nextLine();
        System.out.println("From Email..");
        String fromEmailAddrs=scn.nextLine();
        System.out.println("To Email..");
        String toEmail=scn.nextLine();
        System.out.println("Subject..");
        String subject=scn.nextLine();
        System.out.println("Message..");
        String textMessage=scn.nextLine();
        Session session=Session.getDefaultInstance(properties,new Authenticator() {
            @Override
            protected javax.mail.PasswordAuthentication getPasswordAuthentication(){
                return new javax.mail.PasswordAuthentication(username, password);
            }
        });
        try{
            Message msg=new MimeMessage(session);
            msg.setFrom(new InternetAddress(fromEmailAddrs));
            msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail));
            msg.setSubject(subject);
            msg.setText(textMessage);
            Transport.send(msg);
            System.out.println("/n Your Message Delivered Succesfully");
        }
        catch(MessagingException m){
            throw new RuntimeException(m);
        }

    }
}

Console

Username for Authentication :
bijaybhaskar01
Password for Authentication :
abcdef@456789
From Email..
[email protected]
To Email..
[email protected]
Subject..
Hello
Message..
hai

Output

Exception in thread "main" java.lang.RuntimeException: javax.mail.SendFailedException: Sending failed;
  nested exception is:
    class javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first. fh14sm3131583pab.31 - gsmtp

    at com.mail.bhaskar.MailDemo.main(MailDemo.java:52)
Caused by: javax.mail.SendFailedException: Sending failed;
  nested exception is:
    class javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first. fh14sm3131583pab.31 - gsmtp

    at javax.mail.Transport.send0(Transport.java:218)
    at javax.mail.Transport.send(Transport.java:80)
    at com.mail.bhaskar.MailDemo.main(MailDemo.java:48)

Debug output

DEBUG: JavaMail version 1.3.1
DEBUG: java.io.FileNotFoundException: C:\Program Files\Java\jre1.8.0_77\lib\javamail.providers (The system cannot find the file specified)
DEBUG: URL jar:file:/E:/Study/Java/Jars/MailJars/gimap.jar!/META-INF/javamail.providers
DEBUG: successfully loaded resource: jar:file:/E:/Study/Java/Jars/MailJars/gimap.jar!/META-INF/javamail.providers
DEBUG: URL jar:file:/E:/Study/Java/Jars/MailJars/imap.jar!/META-INF/javamail.providers
DEBUG: successfully loaded resource: jar:file:/E:/Study/Java/Jars/MailJars/imap.jar!/META-INF/javamail.providers
DEBUG: URL jar:file:/E:/Study/Java/Jars/MailJars/pop3.jar!/META-INF/javamail.providers
DEBUG: successfully loaded resource: jar:file:/E:/Study/Java/Jars/MailJars/pop3.jar!/META-INF/javamail.providers
DEBUG: URL jar:file:/E:/Study/Java/Jars/MailJars/smtp.jar!/META-INF/javamail.providers
DEBUG: successfully loaded resource: jar:file:/E:/Study/Java/Jars/MailJars/smtp.jar!/META-INF/javamail.providers
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.gimap.GmailSSLStore=javax.mail.Provider[STORE,gimaps,com.sun.mail.gimap.GmailSSLStore,Oracle], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], com.sun.mail.gimap.GmailStore=javax.mail.Provider[STORE,gimap,com.sun.mail.gimap.GmailStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], gimap=javax.mail.Provider[STORE,gimap,com.sun.mail.gimap.GmailStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], gimaps=javax.mail.Provider[STORE,gimaps,com.sun.mail.gimap.GmailSSLStore,Oracle]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: URL jar:file:/E:/Study/Java/Jars/MailJars/smtp.jar!/META-INF/javamail.address.map
DEBUG: successfully loaded resource: jar:file:/E:/Study/Java/Jars/MailJars/smtp.jar!/META-INF/javamail.address.map
DEBUG: java.io.FileNotFoundException: C:\Program Files\Java\jre1.8.0_77\lib\javamail.address.map (The system cannot find the file specified)
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587

220 smtp.gmail.com ESMTP n6sm3627811pfa.2 - gsmtp
DEBUG SMTP: connected to host "smtp.gmail.com", port: 587

EHLO Bhaskar
250-smtp.gmail.com at your service, [119.82.116.106]
250-SIZE 35882577
250-8BITMIME
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-PIPELINING
250 SMTPUTF8
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "SMTPUTF8", arg ""
DEBUG SMTP: use8bit false
MAIL FROM:<[email protected]>
530 5.7.0 Must issue a STARTTLS command first. n6sm3627811pfa.2 - gsmtp
QUIT
5
  • What does the JavaMail debug output show? What version of JavaMail are you using? Commented Apr 27, 2016 at 6:38
  • That's a pretty old version, you should upgrade if you can, although I don't think that's the source of your problem. Please post the debug output. Commented Apr 27, 2016 at 6:57
  • [1]: i.sstatic.net/vul2V.png please go through this link for debug output Commented Apr 27, 2016 at 7:03
  • That's not the debug output, read my link above. Commented Apr 27, 2016 at 7:06
  • Hai, @BillShannon i have edited my question with debug output please check that Commented Apr 27, 2016 at 7:22

1 Answer 1

1

You're using JavaMail 1.3.1, which is ancient and does not support STARTTLS.

Upgrade to a newer version.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.