-1

I can't send an e-mail with java code via Gmail.

CODE:

import java.util.Properties;

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 b {

    public static void sendMessage(String to, String from, String subject, String text){



        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "false");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");
        props.put("mail.transport.protocol", "smtp");


        Session session = Session.getInstance(props, null);

        try {

            Message message = new MimeMessage(session);
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(to));
            message.setSubject(subject);
            message.setText(text);

            Transport transport = session.getTransport("smtp");
            String mfrom = "fromemail";
            transport.connect("smtp.gmail.com", mfrom, "fromemailpassword");
            transport.sendMessage(message, message.getAllRecipients());

            System.out.println("Done");

        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

I get the error:

javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first. b3sm24500496wiw.22 - gsmtp

I am trying to send an e-mail from a Gmail account to a Gmail accout without success. I need to do this for my business e-mail to send out newsletters ect.

EDIT:

I changed a section of code to this:

        Properties properties = new Properties();
        properties.put("mail.smtp.host", "smtp.gmail.com");
        properties.put("mail.smtp.port", "25");
        properties.put("mail.smtp.auth", "true");   //make it true
        properties.put("mail.smtp.starttls.enable", "true"); //make it true
        properties.put("mail.smtp.ssl.trust", "smtp.gmail.com");
        //Authentication is needed use your gmail user and password"smtp.gmail.com"
        Authenticator auth = new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("email@gmail.com", "password");
            }
        };
        Session session = Session.getInstance(properties, auth);

And now I get the exception: javax.mail.AuthenticationFailedException

Lewis Holmes
  • 15
  • 1
  • 10
  • did you try a google search first? many net examples of this. see [FAQ] on need to search first – tgkprog Aug 29 '14 at 10:06
  • possible duplicate of [JavaMail smtp properties (for STARTTLS)](http://stackoverflow.com/questions/5592112/javamail-smtp-properties-for-starttls) – tgkprog Aug 29 '14 at 10:07

4 Answers4

0

starttls should be true.

means add :

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

then post me that working or not ?

user3145373 ツ
  • 7,858
  • 6
  • 43
  • 62
0

The response code 530 means access denied

The accompanying message indicates that gmail is expecting a STARTTLS. Try with

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

Although it may need more properties to work properly

Besides: non-secure SMTP is always a bad idea unless its local.

nablex
  • 4,635
  • 4
  • 36
  • 51
0

I think that the problem is that your properties are contradictory.

You are setting the SMTP port to 587, which (according to this page) is the server port for SMTP over TLS. But then you are setting mail.smtp.starttls.enable to false.

Naturally the Google SMTP server is confused ... and it tells you that it was expecting the client to send a STARTTLS command.

Solutions:

  • Change the mail.smtp.starttls.enable property to true.

  • Alternatively, change the mail.smtp.port property to 465 (the SSL port)

You don't have the option of using "vanilla" SMTP ... which is a good thing if you care about securing your email traffic.


According to this resource, you also need to enable authentication. (The page has extensive Java example code ...)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

Do it like this

 Properties properties = new Properties();
 properties.put("mail.smtp.host", "smtp.gmail.com");
 properties.put("mail.smtp.port", "465");
 properties.put("mail.smtp.auth", "true");   //make it true
 properties.put("mail.smtp.starttls.enable", "true"); //make it true
 properties.put("mail.smtp.ssl.trust", "smtp.gmail.com");
 //Authentication is needed use your gmail user and password"smtp.gmail.com"
 Authenticator auth = new Authenticator() {
  public PasswordAuthentication getPasswordAuthentication() {
     return new PasswordAuthentication("yourGmailUserName", "yourGmailpassword");
     }
 };
 Session session = Session.getInstance(properties, auth);
SparkOn
  • 8,806
  • 4
  • 29
  • 34