0

I was wondering if anyone can give me a hand on this problem, I am using javax.mail version 1.6.2 if I run locally my code it has no problem but when I am using putty to run it on server, there is an error: javax.mail.SendFailedException: Sending failed; nested exception is: javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first. l11sm980012ejd.103 - gsmtp

    at javax.mail.Transport.send0(Transport.java:219)
    at javax.mail.Transport.send(Transport.java:81)
    at myretail2021.seo.MailSenderExcelZipFile.main(MailSenderExcelZipFile.java:96)

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

The code I am using is:

        String to="ExampleEmail@gmail.com";
    String from="myEmail@gmail.com";
    Properties properties = System.getProperties();
    properties.put("mail.smtp.starttls.enable","true");
    properties.put("mail.smtp.auth", true);
    properties.put("mail.transport.protocol","smtp");
    properties.put("mail.smtp.host", "smtp.gmail.com");
    properties.put("mail.smtp.port", "587"); //even if I use port 25, there is the same problem

    Session session = Session.getInstance(properties,
            new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(from, "PASSWORD");
        }
    });
try {
            // Create a default MimeMessage object.
            Message message = new MimeMessage(session);
            // Set From: header field of the header.
            message.setFrom(new InternetAddress(from));
            // Set To: header field of the header.
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(to));
    message.setSubject(MailSubject);  
        message.setText("Something");  

        // Send message  
        //Transport.send(message);  
        System.out.println("Text message  has been sent successfully....");  
        emailResult=true;
    }catch (MessagingException mex) {mex.printStackTrace();

}

Thanks a million

shila mosammami
  • 175
  • 1
  • 10

0 Answers0