Search code examples
javajakarta-mail

Java Mail Error In Sending Mails


I am using a program to send emails. The code works when I use some other mail server. but I need to use my company's email account to send email. And the email account is provided by gmail [email protected]. When I change the mail host to `stmp.gmail.com, I encounter the following error:

com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command           first. st6sm11092256pbc.58

at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1515)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1054)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:634)
at javax.mail.Transport.send0(Transport.java:189)
at javax.mail.Transport.send(Transport.java:118)
at Mail.sendMail(Mail.java:48)
at Test.main(Test.java:6)

The code is as follows

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

public class Email_Autherticator extends Authenticator {
    String username = "xxxx@gmail";
    String password = "xxxxx";

    public Email_Autherticator() {
        super();
    }
    public Email_Autherticator(String user,String pwd){
        super();
        username = user;
        password = pwd;
    }

    public PasswordAuthentication getPasswordAuthentication(){
        return new PasswordAuthentication(username,password);
    }
} 


import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.SendFailedException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Mail {
    private String host = "smtp.gmail.com";
    private String mail_head_name = "this is head of this mail";
    private String mail_head_value = "this is head of this mail";
    private String mail_to = "[email protected]";
    private String mail_from = "[email protected]";//using gmail server
    private String mail_subject = "this is the subject of this test mail";
    private String mail_body = "this is mail_body of this test mail";
    private String personalName = "xxx";

    public void sendMail() throws SendFailedException{
        try {
            Properties props = new Properties();
            Authenticator auth = new Email_Autherticator();
            props.put("mail.smtp.host", host);
            props.put("mail.smtp.auth", "true");
            System.out.println(props);
            Session session = Session.getDefaultInstance(props,auth);

            MimeMessage message = new MimeMessage(session);
            message.setContent("Hello","text/plain");
            message.setSubject(mail_subject);
            message.setText(mail_body);
            message.setHeader(mail_head_name, mail_head_value);
            message.setSentDate(new Date());
            Address address = new InternetAddress(mail_from,personalName);
            message.setFrom(address);
            Address toaddress = new InternetAddress(mail_to);
            message.addRecipient(Message.RecipientType.TO,toaddress);
            System.out.println(message);
            Transport.send(message);
            System.out.println("Send Mail Ok!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        //return flag;
    }
}

Solution

  • You almost certainly just need to rework your code to add the properties defined in the JavaMail API - Sending email via Gmail SMTP example example.

    You can probably get away with setting your props to this:

    Properties properties = new Properties();
    
    properties.setProperty("mail.smtp.auth", "true");
    properties.setProperty("mail.smtp.starttls.enable", "true");
    properties.setProperty("mail.smtp.host", "smtp.gmail.com");
    properties.setProperty("mail.smtp.port", "587");
    properties.setProperty("mail.smtp.user", gmailUsername);
    properties.setProperty("mail.smtp.password", gmailPassword);
    

    As this seems to be for work--if you can--I suggest using Spring. It makes it a lot cleaner and easier to use. I just recently did something similar to this with Spring and Gmail SMTP.