Must issue a STARTTLS command first. - gsmtp ; Sending email with attachment using gmail account

SayedRakib picture SayedRakib · Dec 3, 2014 · Viewed 10.2k times · Source

I am quite new in programming, sorry for weak explanation. I have tried with almost all available answers in google. but could not figure out why the error is coming.The purpose of this code is to attach a file and send it to a email address. I dont have any email server.

import java.util.Properties;    
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class SendEmailMain {

public static void main(String[] args) {

    final String username = "[email protected]";
    final String password = "mypassword";
    String emailID = "[email protected]";

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

    Session session = Session.getInstance(props,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
            });

    try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(username));
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(emailID));
        message.setSubject("Testing Subject");
        message.setText("PFA");

        MimeBodyPart messageBodyPart = new MimeBodyPart();

        Multipart multipart = new MimeMultipart();

        messageBodyPart = new MimeBodyPart();
        String file = "snap1.jpg";
        String fileName = "attachmentName";
        DataSource source = new FileDataSource(file);
        messageBodyPart.setDataHandler(new DataHandler(source));
        messageBodyPart.setFileName(fileName);
        multipart.addBodyPart(messageBodyPart);

        message.setContent(multipart);

        System.out.println("Sending");

        Transport.send(message);

        System.out.println("Done");

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

Error message in eclipse console

Sending
com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. of6sm3574020lbb.11 - gsmtp
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1388)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:959)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:583)
at javax.mail.Transport.send0(Transport.java:169)
at javax.mail.Transport.send(Transport.java:98)
at me.screenful.screenshot.ScreenShotTaker.SendEmailMain.main(SendEmailMain.java:70)

Answer

SayedRakib picture SayedRakib · Dec 3, 2014

I have found why it was not working. Because I copied the code from google to my project. In the code

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

value true was not in inverted comma. It should be like this

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