1
votes

I have read a whole lot of stuff on internet and the suggested questions before posting, but couldn't find anything that works for me.

  • JavaMail FAQ on using gmail
  • Stackoverflow questions
  • JavaMail tutorials
  • Any other results from google searches

For the past 5 days, I've been looking for a solution to this and just couldn't find one.

Related Code:

private void sendMail(){

  final String username = "foo";
  final String password = "bar";

  Properties props = System.getProperties();
  props.put("mail.smtp.starttls.enable", true); // added this line
  props.put("mail.smtp.host", "smtp.gmail.com");
  props.put("mail.smtp.user", username);
  props.put("mail.smtp.password", password);
  props.put("mail.smtp.port", "587");
  props.put("mail.smtp.auth", true);
  props.put("mail.debug", "true");

  Session session = Session.getInstance(props,null);
  MimeMessage message = new MimeMessage(session);

  System.out.println("Port: "+session.getProperty("mail.smtp.port"));

  // Create the email addresses involved
  try {
    InternetAddress from = new InternetAddress(username);
    message.setSubject("Yes we can");
    message.setFrom(from);
    message.addRecipients(Message.RecipientType.TO, InternetAddress.parse("[email protected]"));

    // Create a multi-part to combine the parts
    Multipart multipart = new MimeMultipart("alternative");

    // Create your text message part
    BodyPart messageBodyPart = new MimeBodyPart();
    String htmlMessage = "Our html text";
    messageBodyPart.setContent(htmlMessage, "text/html");

    // Add html part to multi part
    multipart.addBodyPart(messageBodyPart);

    // Associate multi-part with message
    message.setContent(multipart);

    // Send message
    Transport transport = session.getTransport("smtp");
    transport.connect("smtp.gmail.com", username, password);
    System.out.println("Transport: "+transport.toString());
    transport.sendMessage(message, message.getAllRecipients());
  } catch (MessagingException e) {
    e.printStackTrace();
  }

}

Mail Debug Log File:

DEBUG: JavaMail version 1.4.1 ..... DEBUG: !anyLoaded

DEBUG: not loading resource: /META-INF/javamail.address.map

DEBUG: not loading file: C:\Java\JDK16~1.0_4\jre\lib\javamail.address.map

DEBUG: java.io.FileNotFoundException: C:\Java\JDK16~1.0_4\jre\lib\javamail.address.map (The system cannot find the file specified)

Port: 587

DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]

DEBUG SMTP: useEhlo true, useAuth false

DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587, isSSL false 220 smtp.gmail.com ESMTP t2sm2978726wme.0 - gsmtp

DEBUG SMTP: connected to host "smtp.gmail.com", port: 587

EHLO FOO

250-smtp.gmail.com at your service, [212.156.0.126]

250-SIZE 35882577

250-8BITMIME

250-STARTTLS

250-ENHANCEDSTATUSCODES

250-PIPELINING

250-CHUNKING

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 "CHUNKING", arg ""

DEBUG SMTP: Found extension "SMTPUTF8", arg ""

Transport: smtp://[email protected]

DEBUG SMTP: use8bit false

MAIL FROM: < foo >

530 5.7.0 Must issue a STARTTLS command first. t2sm2978726wme.0 - gsmtp

DEBUG SMTP: got response code 530, with response: 530 5.7.0 Must issue a STARTTLS command first. t2sm2978726wme.0 - gsmtp

1

1 Answers

3
votes

Because you're using a VERY old version of JavaMail, you need to use this:

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

Notice the quote marks.

Better yet, upgrade to a current version.

Oh, and there is no mail.smtp.password property, and you don't need the mail.smtp.host and mail.smtp.user properties since you're passing them to the connect method explicitly.