2

By sending an email I got the following exception:

org.springframework.mail.MailSendException: Failed messages: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. b10sm22671312wmi.34 - gsmtp

Here is the code that I'm using for sending email:

MailRequest mailRequest = new MailRequest();
mailRequest.setSubject(messageByLocale.getMessage("mail.subject.forgetpassword"));
mailRequest.setTemplateName(messageByLocale.getMessage("mail.template.forgetpassword"));
mailRequest.setToEmail(tbNstyleloyalty.getEmail());
Map<String, Object> map = new HashMap<>();
map.put("tbNstyleloyalty", tbNstyleloyalty);
mailingConfig.sendEmail(mailRequest, map);

And my sendEmail method is:

@Async
public void sendEmail(MailRequest mailRequest, Map<String, Object> model) {
    MimeMessagePreparator preparator = new MimeMessagePreparator() 
    {
        @Override
        public void prepare(MimeMessage mimeMessage) throws Exception {
                MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
                message.setTo(mailRequest.getToEmail());
                message.setSubject(mailRequest.getSubject());
                message.setText(VelocityEngineUtils.mergeTemplateIntoString(velocityEngine,templatePath + mailRequest.getTemplateName() + ".vm", ApplicationConstants.CHARSET_UTF8, model),true);
         }
    };
    this.javaMailSender.send(preparator);
}

Please, help me to overcome from this issue.

Thank you!

Tiago Mussi
  • 801
  • 3
  • 13
  • 20
ANucool Mittal
  • 205
  • 4
  • 13

4 Answers4

4

Thank you guys for your answers. All answers given by you were correct but I was unable to understand where I should add this property.

Finally I add:

spring.mail.properties.mail.smtp.starttls.enable = true

to my property file and then I got success.

Tiago Mussi
  • 801
  • 3
  • 13
  • 20
ANucool Mittal
  • 205
  • 4
  • 13
1

Probably you are missing either of the 2 points:

  1. Set the following property in your email request.

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

  1. You are probably attempting to use mail server on port 25 to deliver mail to a third party over an unauthenticated connection.You will need to ask your SMTP client to connect to an authenticated connection. (look if your port number is for TLS connection)
mhasan
  • 3,703
  • 1
  • 18
  • 37
0

I guess that your mail server uses STARTSSL for authentication (as the exception message implies).

Maybe this post helps: JavaMail smtp properties (for STARTTLS)

Community
  • 1
  • 1
0

i think below code is simple enough to be used for mailing functionality

//headers
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
//code
    Session mailSession = new SmtpImpl().getSession();
    Message simpleMessage = new MimeMessage(mailSession);
    MimeMultipart multipart = new MimeMultipart("related");
    BodyPart messageBodyPart = new MimeBodyPart();
    try {
        simpleMessage.setFrom(new InternetAddress("from address")));
        simpleMessage.addRecipient(RecipientType.TO, new InternetAddress("to address"));

        String SENDCC_GET =  "person1@gmail.com,person2.gmail.com";
        String [] SENDCC = SENDCC_GET.split(",");
        InternetAddress[] addressCC = new InternetAddress[SENDCC.length];
        for (int i = 0; i < SENDCC.length; i++) {
            addressCC[i] = new InternetAddress(SENDCC[i]);
        }
        //for bcc
        simpleMessage.setRecipients(Message.RecipientType.BCC, addressCC);
        //for cc
        //simpleMessage.setRecipients(Message.RecipientType.CC, addressCC);

        String message = null;
        String subject = null;

        subject = "email subject";
        simpleMessage.setSubject(subject);
        message = "message body";
        messageBodyPart.setContent(message, "text/html; charset=utf-8");
        multipart.addBodyPart(messageBodyPart);
        simpleMessage.setContent(multipart);
        Transport.send(simpleMessage);

    } catch (AddressException e) {
        LOGGER.error("******Error while sending - Email: Address Exception::: " + e);
    } catch (MessagingException e) {
        LOGGER.error("******Error while sending - Email: Messaging Exception::: " + e);
    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
    }


    //SmtpImpl.java

    public Session getSession(){
        Properties props = new Properties();
        props.put("mail.smtp.host", EmailUtilityParam.getSmtpHostName());
        props.put("mail.smtp.port", EmailUtilityParam.getSmtpPort());
        props.put("mail.smtp.user", EmailUtilityParam.getAuthenticationId());
        props.put("mail.smtp.password", EmailUtilityParam.getAuthenticationPassword());
        props.put("mail.debug", "false");
        Session mailSession = Session.getDefaultInstance(props,
                new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(EmailUtilityParam
                        .getAuthenticationId(), EmailUtilityParam
                        .getAuthenticationPassword());
            }
        });
        return mailSession;
    }

SMTP dev tool can be used for development and keep it in default values for testing whether it works

HOST = localhost
PORT = 25
USER(authentication id) = //empty string
PASSWORD(authentication password) = //empty string
divine
  • 4,746
  • 3
  • 27
  • 38