Using Gmail SMTP servers with JavaMail: Must issue a STARTTLS command first (JavaMail version 1.4.1)

This error mainly occurs when you try to configure SSL Connection with SMTP host in wrong way. javax.mail.MessagingException: A1 BAD Invalid ...

Using Gmail SMTP servers with JavaMail: Must issue a STARTTLS command first (JavaMail version 1.4.1)

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("ardilgulez@gmail.com"));

    // 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");

Click Here to Visit

What's Your Reaction?

like
0
dislike
0
love
0
funny
0
angry
0
sad
0
wow
0