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!

4
  • Which Spring version are you using? Do you use SpringBoot? Can you provide some context like dependencies and application.properties? Commented Mar 10, 2017 at 12:43
  • yes i'm using spring boot , do you want properties and dependency for mailing ? Commented Mar 10, 2017 at 12:50
  • Save time and go with Email Tools library Commented Mar 10, 2017 at 12:53
  • 1
    Great Thank you !! Commented Mar 10, 2017 at 12:56

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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)

1 Comment

Thank you for ans i 'm using 587 port for sending mail
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)

Comments

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 =  "[email protected],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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.